简体   繁体   中英

ElasticSearch Nested Aggregation with Cardinality on another field

I have following ElasticSearch Mapping. Mapping:

"cid": {
    "type": "long"
},
"crankings": {
    "type": "nested",
    "properties": {
          "rank": {
                 "type": "long"
           },
           "name": {
                 "type": "string",
                  "fields": {
                       "raw": {
                           "type": "string",
                            "index": "not_analyzed"
                        }
                   }
            }
     }
 }

I am trying to do aggregation on the nested field (crankings.rank.raw) and cardinality on cid .

I am forming the below aggregation query. Query:

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

But in the result, I am not getting the expected output.

Result:

"buckets": [
                {
                    "key": "xxxxxxxx",
                    "doc_count": 3223,
                    "cid": {
                        "value": 0
                    }
                },
                {
                    "key": "yyyyyy",
                    "doc_count": 1212,
                    "cid": {
                        "value": 0
                    }
                },
                ....

I am getting the cid = 0, which is not expected.

Let me know how can I model the query to get the expected result. ElasticSearch version 2.1.1

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 will look 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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

The output I get is as expected:

        "buckets": [
            {
                "key": "xxxxxxxx",
                "doc_count": 3223,
                "internal_cardinality": {
                    "doc_count": 3223,
                    "cid": {
                        "value": 60
                    }
                }
            },
            {
                "key": "yyyyyy",
                "doc_count": 1212,
                "internal_cardinality": {
                    "doc_count": 1212,
                    "cid": {
                        "value": 50
                    }
                }
            },
            ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM