首页 文章

通过在elasticsearch中正确排序单词来评分结果

提问于
浏览
1

我们有一个弹性搜索索引,具有以下配置:

PUT phonebook
{
   "settings":{
      "index":{
         "number_of_shards":8,
         "number_of_replicas":1
      }
   },
   "mappings":{
      "person":{
         "_all":{
            "enabled":false
         },
         "_source":{
            "enabled":true
         },
         "properties":{
            "id":{
               "type":"long"
            },
            "name":{
               "type":"text",
               "index_options":"positions"
            },
            "number":{
               "type":"long"
            }
         }
      }
   }
}

它基本上是一个拥有数十亿条记录的庞大电话簿 . 我正在使用以下查询搜索此索引:

GET /contacts/contact/_search
{
   "size":0,
   "query":{
      "match":{
         "name":{
            "fuzziness":1,
            "query":"george bush",
            "operator":"and"
         }
      }
   },
   "aggs":{
      "by_number":{
         "terms":{
            "field":"number",
            "size":10,
            "order":{
               "max_score":"desc"
            }
         },
         "aggs":{
            "max_score":{
               "max":{
                  "script":"_score"
               }
            },
            "sample":{
               "top_hits":{
                  "size":1
               }
            }
         }
      }
   }
}

结果按字段“数字”分组,每个数字的最佳匹配以这种方式返回 . 但我需要的是根据结果中单词顺序的正确性对结果进行自定义评分/排序 . 因此,对于“乔治布什”的询问,“乔治布什”总是应该比“布什乔治”得分更好 . match_phrase搜索不适合我,因为我在搜索中使用模糊性 .

1 回答

  • 1

    这样的事情怎么样:

    "query":{
        "simple_query_string": {
          "query": "\"barack~ obama~\"~3",
          "fields": ["name"]
        }    
      },
    

    令牌之后的尾随 ~ 用于模糊方面,而 ~3 跟在短语句柄 slop 之后,这是我认为您正在寻找短语查询的概念 . 我认为结果会得分"Barack Obama"得分高于"Obama Barack" . 你可以想出一个自定义的 bool 查询来模仿这个,其中should子句处理模糊和slop方面 .

    一些资源:

相关问题