【问题标题】:Elasticsearch 5 combine bool terms and nested term filtersElasticsearch 5 结合了布尔项和嵌套项过滤器
【发布时间】:2017-12-22 13:21:25
【问题描述】:

有嵌套查询词过滤器https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html 和布尔词过滤器https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-bool-query.html 的文档

嵌套是对象数组。不只是反对。那就是我不能使用简单的布尔项过滤器。

我的查询如下:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "access_account.nid": 17,
            "destroyed_at": null
          }
        }
      ],
      "must": {
        "match_all": {}
      }
    },
    "nested": {
      "path": "categories",
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "categories.id": [
                  15, 17
                ]
              }
            }
          ]
        }
      }
    }
  }
}

过滤器是数组,因为我实际上有更多过滤器。

我收到了这个回复 reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"

是否有任何解决方案如何组合父项/嵌套项过滤器?官方文档没有帮助。

我的 Elastic 版本是 5.4

谢谢。

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    你快到了,你的nested查询只需要进入bool/filter子句:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "access_account.nid": 17,
                "destroyed_at": null
              }
            },
            {
              "nested": {
                "path": "categories",
                "query": {
                  "bool": {
                    "filter": [
                      {
                        "terms": {
                          "categories.id": [
                            15,
                            17
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 很高兴它有帮助!
    • 帮助很大。起初 Elasticsearch 查询模式看起来很混乱,但现在更加清晰了。
    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 2014-12-31
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多