首页 新闻 会员 周边 捐助

请教关于Lucene.net的问题

0
悬赏园豆:100 [待解决问题]

请教关于Lucene.net的问题

创建索引
private static void CreateIndex()
  {
  string path = @"d:\search\";
  IndexWriter writer = new IndexWriter(path, new StandardAnalyzer());
  Document doc = new Document();

  doc.Add(new Field("title", "AC米兰队", Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(new Field("title", "曼彻斯特联队", Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(new Field("title", "曼彻斯特城队", Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(new Field("title", "国际米兰队", Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(new Field("title", "尤文图斯队", Field.Store.YES, Field.Index.TOKENIZED));

  writer.AddDocument(doc);
  writer.Optimize();
  writer.Close();
  }

搜索
  private static void Search(string words)
  {
  Console.WriteLine(words);
  string path = @"d:\search\";
  IndexSearcher searcher = new IndexSearcher(path);
  Query query = new QueryParser("title", new StandardAnalyzer()).Parse(words);
  Hits hits = searcher.Search(query);
  Console.WriteLine(hits.Length());
  for (int i = 0; i < hits.Length(); i++ )
  System.Console.WriteLine(hits.Doc(i).GetField("title").StringValue());
  searcher.Close();
  }

现在我运行Search("城")
hits.Length() = 1;
只搜索出来了“AC米兰队”
请问是哪里出了问题,请指教,谢谢。

EthanWang的主页 EthanWang | 初学一级 | 园豆:107
提问于:2012-05-28 15:56
< >
分享
所有回答(4)
0

Term pre2 = new Term("title", words);
PrefixQuery query3 = new PrefixQuery(pre2);

 

试试 这个,你用的是模糊查询

留云 | 园豆:212 (菜鸟二级) | 2012-05-28 17:21
0

你对Lucene的Document理解有误

Document相当于数据库中的一条记录,Field相当于数据库表中的一个字段属性

所以是你创建索引的地方出问题了,应该这样:

            IndexWriter writer = new IndexWriter(path, new StandardAnalyzer());

            string[] strArray = new string[] { "AC米兰队", "曼彻斯特联队", "曼彻斯特城队", "国际米兰队", "尤文图斯队" };
            for (int i = 0; i < strArray.Length; i++)
            {
                Document doc = new Document();
                doc.Add(new Field("title", strArray[i], Field.Store.YES, Field.Index.TOKENIZED));
                writer.AddDocument(doc);
            }

            writer.Optimize();
            writer.Close();
Sanco | 园豆:236 (菜鸟二级) | 2012-05-28 17:50
0

不知所云,本就没有你所說的東西。

無限遐想 | 园豆:3740 (老鸟四级) | 2012-05-28 18:02
0

我帮你调试了一下,这样写,不过我的Lucene.net的版本比你高:

StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
FSDirectory fsDirectory = FSDirectory.Open(new DirectoryInfo(@"d:\search\"), new NoLockFactory());
IndexWriter indexWriter = new IndexWriter(fsDirectory, standardAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);

Document document = new Document();
document.Add(new Field("title", "AC米兰城队", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
document = new Document();
document.Add(new Field("title", "曼彻斯特联城队", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
document = new Document();
document.Add(new Field("title", "曼彻斯特城队", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
document = new Document();
document.Add(new Field("title", "国际米兰城队", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
document = new Document();
document.Add(new Field("title", "尤文图斯城队", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
indexWriter.Optimize();
indexWriter.Close();

IndexSearcher indexSearcher = new IndexSearcher(fsDirectory, true);
Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", standardAnalyzer).Parse("城");
TopDocs TopDocs = indexSearcher.Search(query, 1);
Hits hits = indexSearcher.Search(query);
System.Console.WriteLine(hits.Doc(0).GetField("title").StringValue());
System.Console.WriteLine(hits.Doc(1).GetField("title").StringValue());
System.Console.WriteLine(hits.Doc(2).GetField("title").StringValue());
System.Console.WriteLine(hits.Doc(3).GetField("title").StringValue());
System.Console.WriteLine(hits.Doc(4).GetField("title").StringValue());

你的程序问题:

(1)只有一个带“城”的

(2)Document需要像我这样,每天加一个Field都需要AddDocument

哥是技术人 | 园豆:204 (菜鸟二级) | 2012-05-29 09:44

2.9版本的Lucene.Net这么干:

IndexSearcher indexSearcher = new IndexSearcher(fsDirectory, true);
Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", standardAnalyzer).Parse("城");
TopDocs topDocs = indexSearcher.Search(query, 10);
for (int i = 0;i < topDocs.TotalHits;++i)
{
Document temp = indexSearcher.Doc(topDocs.ScoreDocs[i].doc);
Console.WriteLine(temp.GetField("title").StringValue());
}

支持(0) 反对(0) 哥是技术人 | 园豆:204 (菜鸟二级) | 2012-05-29 09:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册