elasticsearch,Lucene,elasticsearch" /> elasticsearch,Lucene,elasticsearch" />

Lucene 弹性搜索词干生成

Lucene 弹性搜索词干生成,lucene,elasticsearch,Lucene,elasticsearch,在一个字段上,我想设置一个自定义分析器,它有自定义的过滤器,重点是词干分析,所以“闪存卡”和“闪存卡”被词干分析到相同的根,所以返回相同的结果 当我运行以下查询时,我得到了命中率(很好),但“闪存卡”和“闪存卡”各自返回不同的结果: {"query_string": { "fields": ["description"], "query": query } } 但当我运行以下查询时,没有得到任何结果: {"query_string": { "fields

在一个字段上,我想设置一个自定义分析器,它有自定义的过滤器,重点是词干分析,所以“闪存卡”和“闪存卡”被词干分析到相同的根,所以返回相同的结果

当我运行以下查询时,我得到了命中率(很好),但“闪存卡”和“闪存卡”各自返回不同的结果:

{"query_string": {
     "fields": ["description"],
     "query": query
     }
}
但当我运行以下查询时,没有得到任何结果:

{"query_string": {
     "fields": ["description.analyzed"],
     "query": query
     }
}
查看下面的映射,我们看到
description.analysisted
description
具有相同的配置-因此每个字段的行为应该相同,并且应该进行词干分析

如何确保分析仪正在使用

我的索引映射:

{'mappings': {
    'file': { # doc_type
      'properties': { # properties for doc_type
        'description': { # field called description
          'type': 'multi_field', # to allow "sub fields" with different alalysers
          'fields': {
            'description': {'type': 'string', 'analyzer': 'my_analyser'},
            'analysed': {'type': 'string', 'analyzer': 'my_analyser'}
          }
        },
      }
     }
    },
    'settings': {
        'analysis': {
          'filter': { #declare my custin filters
            'filter_ngrams': {'max_gram': 5, 'min_gram': 1, 'type': 'edgeNGram'},
            'filter_stop':{'type':'stop', 'enable_position_increments': 'false'},
            'filter_shingle':{'type': 'shingle', 'max_shingle_size': 5, 'min_shingle_size': 2, 'output_unigrams':'true'},
            'filter_stemmer' : {'type': 'stemmer', 'name': 'english'}
          },
          'analyzer': { # declare custom analyzers
            'my_analyser': {
              'filter': ['standard', 'lowercase', 'asciifolding', 'filter_stop', 'filter_shingle', 'filter_stemmer'],
              'type': 'custom',
              'tokenizer': 'standard'
            },
          }
        }
      }
    }

在映射中,“description”和“analysis”的分析器都是“my_analyzer”,但我假设“description”分析器实际上应该是默认的分析器,或者没有这个问题的词干

无论如何,如果要对映射中的字段进行词干分析以编制索引,还需要对实际查询文本使用词干分析器。这就是为什么“闪存卡”和“闪存卡”会得到不同的结果——因为您并没有对查询字符串进行词干分析,而是在执行两种不同的搜索

我不确定这对复杂的查询字符串查询的效果如何,但您应该修改您的查询请求,使其看起来像:

{"query_string": {
    "fields": ["description.analyzed"],
    "query": query,
    "analyzer": "my_analyzer"}
或者类似的东西(确保您指定的分析器正在对查询进行词干分析)。我很确定ES不会像您预期的那样,试图找出您在搜索字段时使用的分析器来分析您的查询。相反,它将使用您设置为默认值的任何分析器

您还可以设置默认分析器(实际上,您可以为索引和搜索设置不同的默认值)-签出