简体   繁体   English

Lucene.NET 4.8搜索没有返回结果

[英]Lucene.NET 4.8 search not returning results

After an upgrade from Lucene 3.X to 4.8, a couple things had to be rewritten to make everything function again. 从Lucene 3.X升级到4.8之后,必须重新编写一些内容才能使所有内容再次运行。

I've tried multiple complete solutions (adjusted for our situation) from different tutorials, and many different tweaks and tests, but am unable to find what the actual problem is with the code below. 我已经尝试了不同教程的多个完整解决方案(针对我们的情况进行了调整),以及许多不同的调整和测试,但我无法找到下面代码的实际问题。

Starting off with the code 从代码开始

The code for adding the fields to a document now looks like this, after changing the fields from generic types to the specific String type 在将字段从泛型类型更改为特定的字符串类型之后,现在将字段添加到文档的代码如下所示

Document document = new Document
{
    new StringField("productName", product.Name, Field.Store.YES),
    new StringField("productDescription", product.Description, Field.Store.YES),
    new StringField("productCategory", product.Category, Field.Store.YES)
};

The search part of the code looks like this: 代码的搜索部分如下所示:

Analyzer analyzer = new StandardAnalyzer(Version);
IndexReader reader = DirectoryReader.Open(indexDirectory);
IndexSearcher searcher = new IndexSearcher(reader);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version,
    new[] { "productName", "productCategory", "productDescription" },
    analyzer,
    new Dictionary<string, float> {
        { "productName", 20 },
        { "productCategory", 5 },
        { "productDescription", 1 }
    }
); 

ScoreDoc[] hits = searcher.Search(parser.Parse(searchTerm))?.ScoreDocs;

The problem 问题

When searching with only a wildcard character the search correctly returns everything, so the indexing part seems to work fine. 当只使用通配符搜索时,搜索正确返回所有内容,因此索引部分似乎工作正常。 If I however try to find the following product with any search term, nothing is found at all. 但是,如果我尝试使用任何搜索词找到以下产品,则根本找不到任何内容。

Example product information 示例产品信息

  • Name: Tafelrok 姓名:Tafelrok
  • Description: Tafelrok 描述:Tafelrok
  • Category: Tafels & Stoelen 类别:Tafels&Stoelen

I've tried with 'Tafelrok', 'tafelrok', 'Tafel', 'tafel', 'afel', 'afe' etc. The last term should hit all 3 fields partially, while the first is a complete match against multiple fields. 我试过'Tafelrok','tafelrok','Tafel','tafel','afel','afe'等。最后一个术语应该部分击中所有3个领域,而第一个是完全匹配多个领域。

I've also tried changing the parser.Parse(searchTerm) bit to include wildcards (" " + searchTerm + " "), but nothing changes. 我也尝试将parser.Parse(searchTerm)位改为包含通配符(“ ”+ searchTerm +“ ”),但没有任何改变。

I'm clearly missing something here, any ideas why the search is broken? 我在这里显然遗漏了一些东西,为什么搜索被破坏了?

You need to configure your fields appropriately, choose right analyzers for indexing and searching and use correct query syntax. 您需要适当地配置字段,选择正确的分析器进行索引和搜索,并使用正确的查询语法。

Document StringField instances are sort of keywords, they are not analyzed, they indexed as is (in it's original case). 文档StringField实例是一些关键字,它们不被分析,它们按原样索引(在它的原始情况下)。 But StandardAnalyzer applies lower case filter to a query. 但是StandardAnalyzer将小写过滤器应用于查询。 You can fix this by using KeywordAnalyzer with your query parser. 您可以通过将KeywordAnalyzer与查询解析器一起使用来解决此问题。 In case when field need to be analyzed (description of the product for example) you can use TextField . 如果需要分析字段(例如产品说明),您可以使用TextField Finally, in order to match partial terms you need to use wildcards ( * or ? ). 最后,为了匹配部分术语,您需要使用通配符( *? )。

For more information check: 有关更多信息,请检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM