Elasticsearch:精确匹配的排名如何? [英] Elasticsearch: how to rank-high the exact match?

查看:321
本文介绍了Elasticsearch:精确匹配的排名如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个字段中搜索 vision项,但是通过使用DSL中的match / match_phrace / term,我只得到了 vision A, vision B, xx版本, vision等结果。

I want to search item "vision" for one field but I just got results like "vision A", "vision B", "xx version","vision" etc by using match/match_phrace/term in the DSL.

我想要的是完全匹配的视觉应该获得最高分,而包含视觉的项目应该排在完全匹配之后。排名应为:

What I want is the exact match "vision" should have the highest score and the items which contain "vision" should rank behind the exact match. The ranking should be:

vision > vision A > vision B > xx version

我检查了 Elasticsearch匹配精确项,其中将 index更改为 not_analyzed被识别为实现精确匹配。但是在我的情况下,不仅需要完全匹配,还需要包含匹配项。

I checked Elasticsearch match exact term in which change the "index" to be "not_analyzed" is recognized to realize exact match. But in my case not only exact match but also the containing match is needed.

在这种情况下我该怎么办?谢谢!

What can I do for this case? Thanks!

推荐答案

您可以做的是在q bool /中加入一些约束。应该查询以便控制排名。

What you can do to achieve this is to include a few constraints in q bool/should query in order to control the ranking.

{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "vision"                  <--- match on vision
        }
      },
      "should": [
        {
          "term": {
            "name.keyword": {               <--- boost exact matches on keyword field (i.e. "vision")
              "value": "vision",
              "boost": 3
            }
          }
        },
        {
          "prefix": {
            "name.keyword": {               <--- boost prefix matches on keyword field (i.e. "vision A" and "vision B"
              "value": "vision",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}




  • 第一个子句将匹配其名称字段中包含 vision 的所有文档。

  • 第二个子句将大大提高其 name.keyword 字段完全包含 vision 的文档。 name.keyword 通常是一个 keyword 字段(以前是 not_analyzed string 字段。)

  • 第三个子句将对名称为的文档提供更高的提升。 字段以 vision 开头。

    • The first clause will match all documents which contain vision in their name field.
    • The second clause will give a higher boost to documents whose name.keyword field contains exactly vision. name.keyword is usually a keyword field (formerly a not_analyzed string field).
    • The third clause will give a slightly higher boost to documents whose name.keyword field starts with vision.
    • 这篇关于Elasticsearch:精确匹配的排名如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆