【问题标题】:ElasticSearch Nested Aggregation with Cardinality on another field另一个字段上具有基数的 ElasticSearch 嵌套聚合
【发布时间】:2018-07-22 01:52:42
【问题描述】:

我有以下 ElasticSearch 映射。 映射:

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

我正在尝试对嵌套字段 (crankings.rank.raw) 和 cid 上的基数进行聚合。

我正在形成以下聚合查询。 查询:

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

但在结果中,我没有得到预期的输出。

结果:

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

我得到 cid = 0,这是意料之外的。

让我知道如何对查询进行建模以获得预期结果。 ElasticSearch 2.1.1 版

【问题讨论】:

    标签: java elasticsearch aggregation


    【解决方案1】:

    我得到了我正在寻找的解决方案。这可以通过使用反向嵌套聚合来实现。

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

    最终查询将如下所示:

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

    我得到的输出符合预期:

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

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 2017-09-13
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多