Home > Net >  ElasticSearch: Aggregations for the count of documents that has empty nested array
ElasticSearch: Aggregations for the count of documents that has empty nested array

Time:10-25

Question- I want a count of documents where the nested array MATCHES is empty like "MATCHES": [ ]. My document structure looks like this(shows two records for simplicity) -

{
  "hits": [
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": [
          {
            "MATCH_STATUS": "APPROVED",
            "UPDATED_ON": 1599171303000
          }
        ]
      }
    },
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": [ ]
      }
    }
  ]
}

Solution tried- I tried following different ways (workaround) of aggregation (empty-match-agg1,empty-match-agg2 ...) but none of these gave correct results. Please help!

 "aggs": {
    "sku": {
      "nested": {
        "path": "MATCHES"
      },
      "aggs": {
        "empty-match-agg1": {
          "missing": {
            "field": "MATCHES"
          }
        },
        "empty-match-agg2": {
          "terms": {
            "field": "MATCHES",
            "missing": "N/A"
          }
        },
        "empty-match-agg3": {
          "sum": {
            "script": {
              "lang": "painless",
              "source": "params['_source'].MATCHES"
            }
          }
        },
        "empty-match-agg4": {
          "filter": {
            "bool": {
              "must_not": {
                "nested": {
                  "query": {
                    "match_all": {}
                  },
                  "path": "MATCHES"
                }
              }
            }
          }
        },
        "empty-match-agg5": {
          "terms": {
            "field": "MATCHES"
          }
        }
      }
    }
  }

CodePudding user response:

Missing aggregation does not support nested field for now. There is open issue as of now.

To get count of empty matches, you can use a filter aggregation with the nested query wrapped into the must_not clause of the bool query.

{
  "aggs": {
    "missing_matches_agg": {
      "filter": {
        "bool": {
          "must_not": {
            "nested": {
              "query": {
                "match_all": {}
              },
              "path": "MATCHES"
            }
          }
        }
      }
    }
  }
}
  • Related