5
我對lucene.net很新。我在C#中編寫了這個簡單的控制檯應用程序,它索引了一些假數據。然後,我希望能夠使用booleanquery在索引中搜索各種術語。Lucene.Net我在做什麼錯了?
我從來沒有得到任何結果。這是代碼。任何幫助將不勝感激。謝謝。
static void Main(string[] args)
{
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter("Test", analyzer, true);
Console.WriteLine("Creating index");
for (int i = 0; i < 1500; i++)
{
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Lucene.Net.Documents.Field("A", i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
doc.Add(new Lucene.Net.Documents.Field("B", "LALA" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
doc.Add(new Lucene.Net.Documents.Field("C", "DODO" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
doc.Add(new Lucene.Net.Documents.Field("D", i.ToString() + " MMMMM", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
BooleanQuery query = new BooleanQuery();
query.Add(new WildcardQuery(new Term("B", "lala*")), Lucene.Net.Search.BooleanClause.Occur.MUST);
query.Add(new WildcardQuery(new Term("C", "DoDo1*")), Lucene.Net.Search.BooleanClause.Occur.MUST);
IndexSearcher searcher = new IndexSearcher("Test");
Hits hits = searcher.Search(query);
if (hits.Length() > 0)
{
for (int i = 0; i < hits.Length(); i++)
{
Console.WriteLine("{0} - {1} - {2} - {3}",
hits.Doc(i).GetField("A").StringValue(),
hits.Doc(i).GetField("B").StringValue(),
hits.Doc(i).GetField("C").StringValue(),
hits.Doc(i).GetField("D").StringValue());
}
}
searcher.Close();
Console.WriteLine("Done");
Console.ReadLine();
}
然後我得到了它使用MultiFieldQueryParser像這樣的工作:
static void Main(string[] args)
{
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter("Test", analyzer, true);
Console.WriteLine("Creating index");
for (int i = 0; i < 1500; i++)
{
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Lucene.Net.Documents.Field("A", i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
doc.Add(new Lucene.Net.Documents.Field("B", "LALA" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
doc.Add(new Lucene.Net.Documents.Field("C", "DODO" + i.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
doc.Add(new Lucene.Net.Documents.Field("D", i.ToString() + " MMMMM", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
BooleanQuery.SetMaxClauseCount(5000);
Query query = MultiFieldQueryParser.Parse(new string[] { "LALA*", "DODO*" }, new string[] { "B", "C" }, analyzer);
IndexSearcher searcher = new IndexSearcher("Test");
Hits hits = searcher.Search(query);
if (hits.Length() > 0)
{
for (int i = 0; i < hits.Length(); i++)
{
Console.WriteLine("{0} - {1} - {2} - {3}",
hits.Doc(i).GetField("A").StringValue(),
hits.Doc(i).GetField("B").StringValue(),
hits.Doc(i).GetField("C").StringValue(),
hits.Doc(i).GetField("D").StringValue());
}
}
searcher.Close();
Console.WriteLine("Done");
Console.ReadLine();
}
這可能是我發現任何新的Lucene的開發者最好的文章:http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx
我標記了A字段並重新運行了該應用程序。它仍然沒有返回任何結果。 – dnoxs 2009-12-04 11:49:48
有沒有其他建議?感謝你的快速回復。 – dnoxs 2009-12-04 12:31:11
您只能在索引字段上執行搜索。所以你也必須索引「B」和「C」字段。 – 2009-12-04 12:38:42