Lucene.Net:如何匹配一个或多个搜索字段?
我目前已经开始使用 Lucene.Net 来搜索一些 txt 文件。我已经实现了单字段搜索并且效果很棒,但我陷入了多字段部分。似乎该术语应该匹配所有字段才能获得命中,但我希望能够匹配一个或多个。
例如,如果有字段:id:10、name:exampleName、brand:exampleBrand 和 term =“exampleName”,那么它应该会很受欢迎。
这是我尝试过的:
public IEnumerable<Document> Search(string searchTerm, string[] searchFields, int limit)
{
DirectoryReader ireader = DirectoryReader.Open(_indexDirectory);
var searcher = new IndexSearcher(ireader);
var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, searchFields, _analyzer);
var query = parser.Parse(searchTerm);
var hits = searcher.Search(query, limit).ScoreDocs;
var documents = new List<Document>();
foreach (var hit in hits)
{
documents.Add(searcher.Doc(hit.Doc));
}
return documents;
}
但就我而言,我没有得到点击,而且总是得到 0 个结果。
编辑: 我的错,看来我的代码正常工作。
I've started currently to use Lucene.Net to search across some txt files. I've implemented the single field search and it works awesome, but I'm stuck at the multiple fields part. It seems that the term should match all the fields to get a Hit, but I want to be able to match one or more.
For example if would have the Fields: id:10, name:exampleName, brand:exampleBrand and the term = "exampleName", then it should be a hit.
Here is what I tried:
public IEnumerable<Document> Search(string searchTerm, string[] searchFields, int limit)
{
DirectoryReader ireader = DirectoryReader.Open(_indexDirectory);
var searcher = new IndexSearcher(ireader);
var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, searchFields, _analyzer);
var query = parser.Parse(searchTerm);
var hits = searcher.Search(query, limit).ScoreDocs;
var documents = new List<Document>();
foreach (var hit in hits)
{
documents.Add(searcher.Doc(hit.Doc));
}
return documents;
}
but in my case I don't get a hit and I always get 0 results.
Edit:
My fault, it seems like my code is working like it should do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论