Tôi rất mới với lucene.net. Tôi đã viết ứng dụng giao diện điều khiển đơn giản này trong C# để lập chỉ mục một số dữ liệu giả mạo. Sau đó tôi muốn có thể tìm kiếm chỉ mục cho các cụm từ khác nhau bằng cách sử dụng booleanquery.Lucene.Net tôi đang làm gì sai?
Tôi chưa bao giờ nhận được bất kỳ kết quả nào. Đây là mã. Mọi sự trợ giúp sẽ rất được trân trọng. Cảm ơn.
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();
}
sau đó tôi đã nhận nó để làm việc bằng cách sử dụng MultiFieldQueryParser Giống như vậy:
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();
}
Đây có thể là bài viết tốt nhất mà tôi đã tìm thấy đối với bất kỳ nhà phát triển Lucene mới: http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx
Tôi đã mã hóa trường A và chạy lại ứng dụng. Nó vẫn không trả lại bất kỳ kết quả nào. – dnoxs
Bất kỳ đề xuất nào khác? Cám ơn bạn đã phản ứng nhanh chóng của bạn. – dnoxs
Bạn chỉ có thể thực hiện tìm kiếm trên các trường được lập chỉ mục. Vì vậy, bạn cũng phải lập chỉ mục các trường "B" và "C". –