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

**URL:** <https://discuss.elastic.co/t/find-documents-where-same-field-value-is-found-in-any-outer-or-nested-fields/355325>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [March 13, 2024, 4:00pm UTC](https://discuss.elastic.co/t/find-documents-where-same-field-value-is-found-in-any-outer-or-nested-fields/355325 "2024-03-13T16:00:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinesmovie](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@spinesmovie](https://discuss.elastic.co/u/spinesmovie)\
**Post date:** [March 13, 2024, 4:00pm UTC](https://discuss.elastic.co/t/find-documents-where-same-field-value-is-found-in-any-outer-or-nested-fields/355325/1 "2024-03-13T16:00:16Z")

</div>

Elasticsearch has an index where \_source is of schema

```auto
{
  ...
  "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

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

}

```

and using query\_string

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

```

however these queries are yielding the desired result

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 10, 2024, 4:01pm UTC](https://discuss.elastic.co/t/find-documents-where-same-field-value-is-found-in-any-outer-or-nested-fields/355325/2 "2024-04-10T16:01:05Z")

</div>

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