Find documents where same field value is found in any outer or nested fields

Elasticsearch has an index where _source is of schema

{
  ...
  "color" : "white",
  "items" : [
    {
      ...
      "color": "blue",
      "items": [...]
    },
    {
      ...
      "color": "red",
      "items": [...]
    },
    {
      ...
      "color": "green",
      "items": [...]
    }
  ]
},

The query needs to find the documents where {"color" : "red"} in any of the nested fields, it is not given that "color" is present in only "items", it could exits in any outer or deeply nested fields of the document.
so basically the query needs to find {"color" : "red"} anywhere in the document

Query I could come up with using the "nested" from the documentation

{
  "query": {
    "nested": {
      "path": "*",
      "query" : {
        "bool": {
          "must": [
            {"match": {"*.color": "red"}}
          ]
        }
      }
    }
  }  

}

and using query_string

{
  "query": {
    "nested": {
      "path": "*",
      "query": {
        "query_stirng": {
          "query": "red",
          "default_field": "color"
        }
      }
    }
  }
}

however these queries are yielding the desired result

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