Search code examples
elasticsearchelasticsearch-aggregation

Using a Missing aggregation for a Term, as opposed to a field


I'm hoping this is the right place to ask this. I'm trying to get an aggregation of the items that don't contain a specific field. They're items with a nested field, as follow:

             "customFields": [
                 {
                     "key": "firstType",
                     "keywordValue": "1"
                 },
                 {
                     "key": "secondType",
                     "keywordValue": "A"
                 }
             ]

I've been trying to use a missing aggregation, if I was trying to find all the items that don't contain a key with the value firstType, but haven't had much luck. The query I've made is:

 "aggregations": {
       "custom.firstType": {
               "missing": {
                 "field": "firstType"
               },
               "aggregations": {
                 "values": {
                   "reverse_nested": {}
                 }
               }
             }
           }

Unfortunately, this hasn't been much luck for me. What am I over looking?

Thanks!


Solution

  • Missing aggregation is used to find documents which don’t contain a given field(not value)

    {
       "product_id": "p1",
       "price":100
    }
    {
       "product_id":"P2"
    }
    

    Second document does not contain field price. To find all products which don’t have price you will use missing aggregation.

    In your case you want aggregation on documents which don’t have a particular value, you need to use nested aggregation with filter sub aggregation

    Mappings:

    PUT index19
    {
      "mappings": {
        "properties": {
          "customFields": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                }
              },
              "keywordValue":{
                "type":"integer"
              }
            }
          }
        }
      }
    }
    

    Query:

    GET index19/_search
    {
      "size": 0,
      "aggs": {
        "without_firsttype": {
          "nested": {
            "path": "customFields"
          },
          "aggs": {
            "key_check": {
              "filter": {
                "bool": {
                  "must_not": [
                    {
                      "term": {
                        "customFields.key.keyword": {
                          "value": "firstType"
                        }
                      }
                    }
                  ]
                }
              },
              "aggs": {
                "Document": {
                  "reverse_nested": {}
                }
              }
            }
          }
        }
      }
    }
    

    Result:

    "aggregations" : {
        "without_firsttype" : {
          "doc_count" : 4,
          "key_check" : {
            "doc_count" : 3,
            "Document" : {
              "doc_count" : 2
            }
          }
       }
    }