Elasticsearch 5 结合 bool 术语和嵌套术语过滤器

Elasticsearch 5 combine bool terms and nested term filters

有嵌套查询术语过滤器的文档https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html and bool term filter https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-bool-query.html

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

我的查询如下:

{
  "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]"

有什么解决方案可以组合 parent/nested 术语过滤器吗?官方文档没有帮助。

我的 Elastic 版本是 5.4

谢谢。

您快完成了,您的 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
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}