源于弹性搜索

时间:2013-10-01 00:10:14

标签: lucene elasticsearch

在一个字段上,我想设置一个自定义分析器,它具有自定义过滤器 - 专注于词干 - 所以“闪存卡”和“闪存卡”被截取到相同的根,因此返回相同的结果

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

{"query_string": {
     "fields": ["description"],
     "query": query
     }
}

但是当我运行以下查询时,我得不到任何结果:

{"query_string": {
     "fields": ["description.analyzed"],
     "query": query
     }
}

查看下面的我的地图,我们看到description.analyzeddescription具有相同的配置 - 因此每个字段应该表现相同,并且会发生阻塞?

我如何确定正在使用分析仪?

我对索引的映射:

{'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'
            },
          }
        }
      }
    }

1 个答案:

答案 0 :(得分:2)

在您的映射中,您将“描述”和“已分析”的分析器都设置为“my_analyser”,但我假设“描述”分析器实际上应该是默认的分析器或者没有产生这个问题的东西

无论如何,如果你在索引的映射中阻塞了一个字段,你还需要在实际的查询文本中使用一个词干分析器。这就是为什么你为“闪存卡”和“闪存卡”获得不同结果的原因 - 因为你没有阻止你的查询字符串,你实际上是在执行两种不同的搜索。

我不确定这对复杂的query_string查询有多好,但您应该将查询请求修改为:

{"query_string": {
    "fields": ["description.analyzed"],
    "query": query,
    "analyzer": "my_analyzer"}

或类似的东西(确保您指定的分析器阻止您的查询)。我很确定ES不会试图找出你在搜索的字段上使用哪个分析器来分析你的查询,就像你期望的那样。相反,它将使用您设置的任何分析器作为默认值。

您也可以设置默认分析器(实际上您可以为索引和搜索设置不同的默认值) - 请查看http://www.elasticsearch.org/guide/reference/index-modules/analysis/