2
votes

There is documentation for for nested query terms filter 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

Nested is array of objects. Not just object. That is point i can't use simple bool term filter.

my query looks like:

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

Filters are array because i have in real more terms filters.

I got this response reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"

Is there any solution how to combine parent/nested term filters? Official documentation doesn't help.

My Elastic version is 5.4

Thanks.

1

1 Answers

4
votes

You're almost there, your nested query just needs to go inside the bool/filter clause:

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