# Find elements in array of nested dictionaries

**URL:** <https://discuss.elastic.co/t/find-elements-in-array-of-nested-dictionaries/286431>\
**Category:** Elasticsearch\
**Created:** [October 12, 2021, 4:21am UTC](https://discuss.elastic.co/t/find-elements-in-array-of-nested-dictionaries/286431 "2021-10-12T04:21:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![niato](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@niato](https://discuss.elastic.co/u/niato)\
**Post date:** [October 12, 2021, 4:21am UTC](https://discuss.elastic.co/t/find-elements-in-array-of-nested-dictionaries/286431/1 "2021-10-12T04:21:02Z")

</div>

I'm struggling to find a way to get documents meeting specific criteria in an index that looks like this:

```auto
{
  "products": [
    {
      "name": "product 1",
      "defects": {
        "packaging": {
          "desc": "..."
        },
        "labeling": {
          "desc": "..."
        },
        "other": {
          "desc": "..."
        }
      }
    },
    {
      "name": "product 2",
      "defects": {
        "packaging": {
          "desc": "..."
        }
      }
    },
    {
      "name": "product 3",
      "defects": {
        "labeling": {
          "desc": "..."
        },
        "other": {
          "desc": "..."
        }
      }
    }
  ]
}

```

I need to get all the documents with one or more types of **defects**. A product can have the same **defect** multiple times with different **desc** descriptions. However, that's not up to the user; the _descriptions_ are hardcoded in the system.  
For instance, I want to find all the documents with **packaging** defects; or all the documents with _packaging_ and **labeling** defects.

As far as I understand, `nested` or `flattened` types can help with this case. However, I couldn't make it work with any of them.

The following is the template I tried for nested types:

```auto
{
    "template": "product_defects-*",
    ...
    "mappings": {
        "properties": { 
            "products": {
                "type": "nested",
                "include_in_root": true,
                "properties": {
                  "defects": {
                      "type": "nested",
                      "include_in_root": true
                  }
                }
            }
        }
    }
}

```

Also, given the structure of my index above, I would like to know which one will be more appropriate to avoid a mapping explosion.

---

<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:** [November 9, 2021, 4:21am UTC](https://discuss.elastic.co/t/find-elements-in-array-of-nested-dictionaries/286431/2 "2021-11-09T04:21:47Z")

</div>

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