Lucene .NET 搜索不返回任何结果
Posted
技术标签:
【中文标题】Lucene .NET 搜索不返回任何结果【英文标题】:Lucene .NET Search returns no Results 【发布时间】:2021-12-02 15:10:57 【问题描述】:我一直试图让 Lucene .NET 在我的 API 中工作,但它总是返回 0 结果。 我在这里写的所有东西都封装在一个单例服务中,我的 API 控制器使用它。
我如何创建索引:
public async Task<bool> CreateSearchArtefacts()
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
using (var dir = FSDirectory.Open(IndexPath))
using (IndexWriter writer = new IndexWriter(dir, indexConfig))
foreach (var element in this.data)
Document doc = new Document
new TextField("Title", element.Title, Field.Store.YES),
new StringField("Author", element.Author, Field.Store.YES),
new TextField("Description", element.Description, Field.Store.NO)
;
writer.AddDocument(doc);
this.logger.LogInformation(element.Title + " added.");
this.logger.LogInformation("All Books added.");
writer.Commit();
;
;
return true;
这就是我搜索它的方式:
public async Task<SearchResultsCollection> SearchSingleTerms(string term, string search, int count)
this.logger.LogInformation("Searching Term "+ term + " for " + search);
var phrase = new TermQuery(new Term(term, search));
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
var sendList = new List<SearchResult>();
using (var dir = FSDirectory.Open(IndexPath))
using (var writer = new IndexWriter(dir, indexConfig))
var reader = writer.GetReader(true);
var searcher = new IndexSearcher(reader);
var hits = searcher.Search(phrase, count /* top 20 */).ScoreDocs;
this.logger.LogInformation("Results found: " + hits.Length);
foreach (var item in hits)
var foundDoc = searcher.Doc(item.Doc);
sendList.Add(new SearchResult()
Author = foundDoc.Get("Author"),
Title = foundDoc.Get("Title"),
Description = foundDoc.Get("Description")
);
writer.Flush(triggerMerge: false, applyAllDeletes: false);
;
;
return new SearchResultsCollection() Count = count, Results = sendList.ToArray() ;
我将 Lucene.Net 4.8.0-beta00014 与 .NET Core 3.1 一起使用
任何帮助表示赞赏!
【问题讨论】:
【参考方案1】:找到了。 我必须将短语的字符串参数转换为字符串值。
public async Task<SearchResultsCollection> SearchSingleTerms(string term, string search, int count)
this.logger.LogInformation("Searching Term "+ term + " for " + search);
var phrase = new TermQuery(new Term(term.ToString(), search.ToString()));
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
var sendList = new List<SearchResult>();
using (var dir = FSDirectory.Open(IndexPath))
using (var writer = new IndexWriter(dir, indexConfig))
var reader = writer.GetReader(true);
var searcher = new IndexSearcher(reader);
var hits = searcher.Search(phrase, count /* top 20 */).ScoreDocs;
this.logger.LogInformation("Results found: " + hits.Length);
foreach (var item in hits)
var foundDoc = searcher.Doc(item.Doc);
sendList.Add(new SearchResult()
Author = foundDoc.Get("Author"),
Title = foundDoc.Get("Title"),
Description = foundDoc.Get("Description")
);
writer.Flush(triggerMerge: false, applyAllDeletes: false);
;
;
return new SearchResultsCollection() Count = count, Results = sendList.ToArray() ;
【讨论】:
以上是关于Lucene .NET 搜索不返回任何结果的主要内容,如果未能解决你的问题,请参考以下文章