ElasticSearch Nested Aggregation with Cardinality on another field


(Tushar Kanta) #1

Hi All,

I need some help on below problem. Let me know how can I form my query to get expected output. Details posted in the stack overflow page. Thanks


(Tushar Kanta) #2

I got the solution I was looking for. This can be achieved by using reverse nested aggregation.

As per the reference(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html),
When nested aggregation is applied, the query runs against the nested document. So to access any field of parent document inside nested doc, reverse nested aggregation can be used.

The final query looks like:

{
  "size": 0,
  "aggregations": {
    "crankings": {
      "nested": {
        "path": "crankings"
      },
      "aggregations": {
        "crankings": {
          "terms": {
            "field": "crankings.name.raw",
            "size": 0
          },
          "aggregations": {
            "internal_cardinality": {
              "reverse_nested": { },
              "aggregations": {
                "cid": {
                  "cardinality": {
                    "field": "cid"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

(system) #3

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