I’ve been racking my brains and combing through documentation to find an elasticsearch query that’ll help me resolve what feels like a simple problem, but I can’t seem to find a solution. I’m trying to get the count of all the items that don’t contain a nested value. For example, with an item like:

       "label": item1
        "customFields": [
             {
                 "key": "firstType",
                 "value": "foo"
             },
             {
                 "key": "secondType",
                 "value": "bar"
             }
         ]

I want to find all the items that don’t contain "key":"secondType". I’ve tried using missing, but since it’s a nested, value, that doesn’t quite match. I’ve used a must_not filter on a nested path aggregation, but unfortunately that returns the counts of all the KEYS that aren’t secondType, not a count of the items. Is there something I’m missing?

Thanks!

You don’t need aggregation, you should you a simple must_not query like :

{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "customFields.key": "secondType"
          }
        }
      ]
    }
  }
}