首页 文章

ElasticSearch:具有edge_ngram和模糊性的部分/精确评分

提问于
浏览
2

在ElasticSearch中,我试图使用edge_ngram以及模糊来获得正确的评分 . 我希望完全匹配得分最高,子匹配得分较低 . 以下是我的设置和评分结果 .

settings: {
          number_of_shards: 1,
          analysis: {
             filter: {
                ngram_filter: {
                   type: 'edge_ngram',
                   min_gram: 2,
                   max_gram: 20
                }
             },
             analyzer: {
                ngram_analyzer: {
                   type: 'custom',
                   tokenizer: 'standard',
                   filter: [
                      'lowercase',
                      'ngram_filter'
                   ]
                }
             }
          }
       },
    mappings: [{
          name: 'voter',
          _all: {
                'type': 'string',
                'index_analyzer': 'ngram_analyzer',
                'search_analyzer': 'standard'
             },
             properties: {
                last: {
                   type: 'string',
                   required : true,
                   include_in_all: true,
                   term_vector: 'yes',
                   index_analyzer: 'ngram_analyzer',
                   search_analyzer: 'standard'
                },
                first: {
                   type: 'string',
                   required : true,
                   include_in_all: true,
                   term_vector: 'yes',
                   index_analyzer: 'ngram_analyzer',
                   search_analyzer: 'standard'
                },

             }

       }]

在使用名字“迈克尔”进行POST后,我按照以下更改“迈克尔”,“米歇”,“米哈”,“密歇根”,“麦克风”和“米”进行查询 .

GET voter/voter/_search
{
 "query": {
    "match": {
      "_all": {
        "query": "Michael",
        "fuzziness": 2,
        "prefix_length": 1
      }
    }
  }
}

我的分数结果是:

-"Michael": 0.19535106
-"Michae": 0.2242768
-"Micha": 0.24513611
-"Mich": 0.22340237
-"Mic": 0.21408978
-"Mi": 0.15438235

正如您所看到的,得分结果未达到预期效果 . 我希望“迈克尔”得分最高,“米”得分最低

任何帮助,将不胜感激!

1 回答

  • 0

    解决此问题的一种方法是在映射中添加原始版本的文本

    last: {
                           type: 'string',
                           required : true,
                           include_in_all: true,
                           term_vector: 'yes',
                           index_analyzer: 'ngram_analyzer',
                           search_analyzer: 'standard',
                           "fields": {
                                "raw": { 
                                   "type":  "string"  <--- index with standard analyzer
                                  }
                              }
                        },
                        first: {
                           type: 'string',
                           required : true,
                           include_in_all: true,
                           term_vector: 'yes',
                           index_analyzer: 'ngram_analyzer',
                           search_analyzer: 'standard',
                           "fields": {
                                "raw": { 
                                   "type":  "string"  <--- index with standard analyzer
                                  }
                              }
                        },
    

    您也可以使用 index : not_analyzed 进行 exact

    然后你可以像这样查询

    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "_all": {
                  "query": "Michael",
                  "fuzziness": 2,
                  "prefix_length": 1
                }
              }
            },
            {
              "match": {
                "last.raw": {
                  "query": "Michael",
                  "boost": 5
                }
              }
            },
            {
              "match": {
                "first.raw": {
                  "query": "Michael",
                  "boost": 5
                }
              }
            }
          ]
        }
      }
    }
    

    匹配更多子句的文档将得分更高 . 您可以根据您的要求指定 boost .

相关问题