ElasticSearch在另一个字段上嵌套具有基数的聚合 [英] ElasticSearch Nested Aggregation with Cardinality on another field

查看:207
本文介绍了ElasticSearch在另一个字段上嵌套具有基数的聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ElasticSearch映射。
映射:

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

我正在尝试在嵌套字段(crankings.rank.raw)上进行汇总,并在cid上进行基数化。

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.

结果:

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

我得到的cid = 0,这是不期望的。

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

让我知道如何为查询建模得到预期的结果。 ElasticSearch版本2.1.1

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.

根据参考( https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/search-aggregations- bucket-reverse-nested-aggregation.html ),
应用嵌套聚合时,查询将针对嵌套文档运行。因此,要访问嵌套文档中父文档的任何字段,可以使用反向嵌套聚合。

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.

最终查询如下:

{
  "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
                    }
                }
            },
            ....

这篇关于ElasticSearch在另一个字段上嵌套具有基数的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆