lucene.net IndexSearcher查不出来数据。大虾们指点下撒。谢谢了。

qq624585943 2010-07-17 11:24:51
刚开始学lucene.net。不是很会。希望大虾们指导下,是不是我的查询写错了。

string strPath = @"E:\web";

IndexWriter writer = new IndexWriter(FSDirectory.GetDirectory(strPath, true), new StandardAnalyzer(), true);
Document doc1 = new Document();
doc1.Add(new Field("name", "name1", Field.Store.YES, Field.Index.NO));
doc1.Add(new Field("value", "value1", Field.Store.YES, Field.Index.NO));
Document doc2 = new Document();
doc2.Add(new Field("name", "name2", Field.Store.YES, Field.Index.NO));
doc2.Add(new Field("value", "value2", Field.Store.YES, Field.Index.NO));
writer.AddDocument(doc1);
writer.AddDocument(doc2);
writer.Optimize();
writer.Close();


IndexSearcher searcher = new IndexSearcher(strPath);
QueryParser qp = new QueryParser("name", new StandardAnalyzer());
Query query = qp.Parse("name:name1");
Hits hits = searcher.Search(query);

Response.Write(hits.Length().ToString());
...全文
251 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq624585943 2010-07-20
  • 打赏
  • 举报
回复
谢谢两位。搜索我都做出来了。嘻嘻。
幻想的天涯 2010-07-20
  • 打赏
  • 举报
回复
楼主给我发份看看!邮箱402127440@qq.com
vip__888 2010-07-17
  • 打赏
  • 举报
回复
多看看资料吧。接触过,不过忘记了
周睿 2010-07-17
  • 打赏
  • 举报
回复
#region 搜索引擎
public DataTable Search(int CurrentPage, int Pagesize, string Key)
{
Analyzer analyzer = new StandardAnalyzer();
DataTable mytab = new DataTable();
string INDEX_STORE_PATH = Server.MapPath("seacherIndex"); //Index 为索引存储目录
string keyword = Key;
IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);//索引查询器
try
{
QueryParser q = new QueryParser("SearchAll", new StandardAnalyzer());
Query query = q.Parse(keyword);
QueryScorer scorer = new QueryScorer(query);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color=red>", "</font>");
Highlighter highlighter = new Highlighter(formatter, scorer);
Hits myhit = mysea.Search(query);

if (myhit != null)
{
DataRow myrow;
mytab.Columns.Add("productId", typeof(Int32));
mytab.Columns.Add("productName", typeof(string));
mytab.Columns.Add("Price", typeof(decimal));
mytab.Columns.Add("pInfo", typeof(string));
mytab.Columns.Add("pClassId", typeof(Int32));

mytab.Columns.Add("pTitle", typeof(string));
mytab.Columns.Add("pKey", typeof(string));
mytab.Columns.Add("pDescription", typeof(string));
mytab.Columns.Add("pButtom", typeof(string));

mytab.Columns.Add("pDate", typeof(DateTime));
mytab.Columns.Add("eDate", typeof(DateTime));
mytab.Columns.Add("pSourse", typeof(string));
mytab.Columns.Add("ptag", typeof(string));
mytab.Columns.Add("pSort", typeof(Int32));


mytab.Columns.Add("SearchAll", typeof(string));
mytab.Clear();
int StartIndex = CurrentPage * Pagesize - Pagesize;
int count = CurrentPage * Pagesize > myhit.Length() ? myhit.Length() : CurrentPage * Pagesize;
ViewState["count"] = myhit.Length();
for (int i = StartIndex; i < count; i++)
{
Document doc = myhit.Doc(i);
myrow = mytab.NewRow();
myrow[0] = doc.Get("productId");
myrow[1] = (highlighter.GetBestFragment(new StandardAnalyzer(), "", doc.Get("productName")));
if (myrow[1].ToString() =="")
{
myrow[1] = doc.Get("productName");
}
myrow[2] = doc.Get("Price");
myrow[3] = doc.Get("pInfo");
myrow[4] = doc.Get("pClassId");

myrow[5] = doc.Get("pTitle");
myrow[6] = doc.Get("pKey");
myrow[7] = doc.Get("pDescription");
myrow[8] = doc.Get("pButtom");
myrow[9] = doc.Get("pDate");
myrow[10] = doc.Get("eDate");
myrow[11] = doc.Get("pSourse");
myrow[12] = doc.Get("ptag");
myrow[13] = doc.Get("pSort");
myrow[14] = doc.Get("SearchAll");

mytab.Rows.Add(myrow);
myrow.AcceptChanges();

}
this.ProductDetails1.Key = Server.UrlEncode(Key);
}
mysea.Close();

}
catch
{

mysea.Close();
}
return mytab;
}
#endregion
qq624585943 2010-07-17
  • 打赏
  • 举报
回复
查了些资料的。RAMDirectory内存中可以读取。FSDirectory就读取不出来了。不知道什么问题。公司催的紧啊。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧