【问题标题】:Elasticsearch bool queryElasticsearch 布尔查询
【发布时间】:2017-09-18 03:33:32
【问题描述】:

我在这里遵循https://dzone.com/articles/23-useful-elasticsearch-example-queries 的指南,下面的布尔查询让我感到困惑:

{
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "title": "Elasticsearch" }},
                      { "match": { "title": "Solr" }} ] }
            },
            "must": { "match": { "authors": "clinton gormely" }},
            "must_not": { "match": {"authors": "radu gheorge" }}
        }
    }
}

根据教程,查询的解释是:

搜索标题中带有“Elasticsearch”或“Solr”字样的书, AND 由“clinton gormley”撰写,但不是由“radu”撰写 乔治”

我的问题是,bool 查询中有 3 个条件但也有 3 个逻辑运算符(必须、必须、must_not)而不是 2。我的理解是 3 个条件应该只有 2 个逻辑运算符,如 COND1 AND COND2 AND !COND3

这里有什么我遗漏的吗?

【问题讨论】:

    标签: elasticsearch boolean


    【解决方案1】:

    那个查询是错误的,不会起作用,不可能有两个bool/must子句,它们必须像这样合并:

    {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "title": "Elasticsearch"
                    }
                  },
                  {
                    "match": {
                      "title": "Solr"
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "authors": "clinton gormley"
              }
            }
          ],
          "must_not": {
            "match": {
              "authors": "radu gheorge"
            }
          }
        }
      }
    }
    

    bool/should 子句被埋在bool/must 中的原因之一是赋予authors 上的匹配与title 上的两个匹配相同的权重。

    【讨论】:

    • 但是我尝试了这些示例并且效果很好。知道为什么吗? ES 5.x 顺便说一句
    • 您是如何尝试的?通过 Kibana?上面的查询 AS IS 不可能产生任何结果。
    • 不幸的是,他们做到了。我正在使用 curl。
    • 我不知道该告诉你什么,除了同一个bool 中的两个must 不正确。确实存在文档和文章错误 :-)
    • 您的查询对我来说非常有意义,它只是让我感到困惑,为什么教程链接上的查询即使在最新的 ES 5.3 上也有效。顺便说一句,它对您的 ES 安装有用吗?
    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2023-03-10
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2017-05-22
    相关资源
    最近更新 更多