【问题标题】:How Elastic Search boolean query works?Elastic Search 布尔查询如何工作?
【发布时间】:2014-11-16 07:43:46
【问题描述】:

能否请您解释一下 Elastic Search 布尔查询的工作原理? 我在这里阅读了文档: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html 但似乎它太简单了我无法理解。看看这个查询:

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        },
        "must_not" : {
            "range" : {
                "age" : { "from" : 10, "to" : 20 }
            }
        },
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}

我无法理解“should”和“minimum_should_match”的用法。请给我解释一下好吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在您提供的查询中应该会显示文档(意味着它们将 先来)如果他们满足 must 和 must_not 部分。如果满足 should 数组中提供的任何一个条件,则应该匹配(它将使用 OR 运算符加入 should)

    现在考虑这种情况

        {
              "bool": {
                "should": [
                  {
                    "term": {
                      "tag": "wow"
                    }
                  },
                  {
                    "term": {
                      "tag": "elasticsearch"
                    }
                  }
                ],
                "minimum_should_match": 1,
                "boost": 1
              }
            } 
    

    这里没有 must 和 must_not 那么它将匹配 should 数组中的所有条件。它将返回包含标签 wow 和 elasticsearch 的文档(将使用 AND 运算符加入 should 子句)并且在您的查询中(其中还包含 must 部分)它将加入应该使用 OR 运算符的子句。

    为了弄清楚 minimum_should_match 请参考这个

    http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html

    如果我能够澄清区别和功能,请告诉我..

    【讨论】:

    • 感谢您的回答好友。如果在我的查询中没有与 should 匹配的内容怎么办?
    • 在您的查询中它不会返回文档,因为您已经给出了 "minimum_should_match": 1
    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 2014-10-18
    • 2023-01-11
    • 1970-01-01
    • 2012-10-16
    • 2013-08-15
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多