Find elements in array of nested dictionaries

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

{
  "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:

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

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