Warm tip: This article is reproduced from serverfault.com, please click

c#-Lucene .NET 搜索不返回任何结果

(c# - Lucene .NET Search returns no Results)

发布于 2021-10-14 08:19:59

我一直想让 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 一起使用

任何帮助表示赞赏!

Questioner
user3315635
Viewed
0
user3315635 2021-10-14 17:05:43

找到了。我必须将短语的字符串参数转换为字符串值。

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() };
}