Ankur Lathwal
Ankur Lathwal

Reputation: 725

How to write a nested query in combination with filters in Elastic Search?

I need to do a nested query which tries to search within an array of objects. Apart from this, I have other parameters as well that I need to search upon. I am using filters for those. How can I combine the filters and nested query in an effective manner? Sorry I am not an expert on ES.

Here is the query I tried to execute in Kibana:

GET test-index/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isPublic": true
              }
            },
            {
              "term": {
                "isDeleted": false
              }
            }
          ]
        },
        "nested": {
          "path": "data.location.countries",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "data.location.countries.name": "United States"
                  }
                },
                {
                  "range": {
                    "data.location.countries.weight": {
                      "gt": 30
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "size": "60",
      "from": 0,
      "sort": [
        {
          "followers": {
            "order": "desc"
          }
        }
      ]
    }

It returned with an error:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 17,
        "col": 5
      }
    ],
    "type": "parsing_exception",
    "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 17,
    "col": 5
  },
  "status": 400
}

Can someone shed some knowledge on this?

Upvotes: 0

Views: 192

Answers (1)

Demi
Demi

Reputation: 51

Move nested query inside bool.filter:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "isPublic": true
          }
        },
        {
          "term": {
            "isDeleted": false
          }
        },
        {
          "nested": {
            "path": "data.location.countries",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "data.location.countries.name": "United States"
                    }
                  },
                  {
                    "range": {
                      "data.location.countries.weight": {
                        "gt": 30
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "size": "60",
  "from": 0,
  "sort": [
    {
      "followers": {
        "order": "desc"
      }
    }
  ]
}

You can't use more than 1 queries outside of a compound query afaik.

Upvotes: 1

Related Questions