对于查询搜索,minimum_should_match百分比实际上有什么作用?

时间:2019-08-23 07:16:46

标签: elasticsearch kibana elk

我想进一步了解 elasticsearch 中用于查询搜索的minimum_should_match如何工作

GET /customers/_search
{
  "query": {
     "bool": {
        "must":[
           {
           "query_string":{
              "query": "大月亮",
              "default_field":"fullName",
              "minimum_should_match": "70%" ------> experimented with this value
           }
        }
      ]
    }
  }
}

我对查询中的百分比进行了实验,可以看到我得到的中文结果不同吗?

我尝试阅读文档,但是不清楚是否可以使用此选项?

1 个答案:

答案 0 :(得分:2)

minimum_should_match参数适用于“布尔”查询中的“应”子句。使用此参数,您可以指定文档中要匹配查询的应当匹配多少个应当子句。

考虑以下查询:

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } },
        { "term" : { "tag" : "stackoverflow" } }
      ],
      "minimum_should_match" : 2,
      "boost" : 1.0
    }
  }
}

在这里,仅当最少2个子句匹配时,文档才是匹配项。这意味着,如果在“标签”字段中同时具有“ stackoverflow”和“ wow”的文档将匹配,但是在标签字段中仅具有“ elasticsearch”的文档将不被视为匹配。

使用百分比时,可以指定应匹配的应当子句的百分比。因此,如果您有4个should子句,并且将minimum_should_match设置为50%,则如果其中两个should子句匹配,则该文档将被视为匹配项。

可以在the documentation中找到有关minimum_should_match的更多信息。在那里,您可以阅读它的“可选子句”,这是“布尔”查询中的“应该”。