ElasticSearch Nested Aggregation with Cardinality on another field

只谈情不闲聊 提交于 2019-12-24 20:23:08

问题


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


回答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
                    }
                }
            },
            ....


来源:https://stackoverflow.com/questions/48737246/elasticsearch-nested-aggregation-with-cardinality-on-another-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!