Nested query with filters

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?

Your bool and your nested query are at the same level, you should put your nested query alongside the term query in the array of "must2 clauses which all must be satisfied or a match.

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

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.