0

两个聚合中 PARTY_ID 的计数应该相同。在一种情况下,它是 3000,另一种情况下,它是所有不相等的值 (2675 + 244 + 41 + 6 + 2 = 2950 ) 的总和。可能是什么原因?

GET /test/data/_search
{
   "size": 0,
   "aggs": {
      "ASSET_CLASS": {
         "terms": {
            "field": "ASSET_CLASS_WORST"
         },
         "aggs": {
            "ASSET_CLASS": {
               "cardinality": {
                  "field": "PARTY_ID"
               }
            }
         }
      },
      "Total count": {
         "cardinality": {
            "field": "PARTY_ID"
         }
      }
   }
}

结果 :

{
   "took": 9,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 51891,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "Total count": {
         "value": 3000
      },
      "ASSET_CLASS": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "NPA",
               "doc_count": 49252,
               "ASSET_CLASS": {
                  "value": 2675
               }
            },
            {
               "key": "RESTRUCTURED",
               "doc_count": 2275,
               "ASSET_CLASS": {
                  "value": 244
               }
            },
            {
               "key": "SMA2",
               "doc_count": 308,
               "ASSET_CLASS": {
                  "value": 41
               }
            },
            {
               "key": "SMA1",
               "doc_count": 42,
               "ASSET_CLASS": {
                  "value": 6
               }
            },
            {
               "key": "SMA0",
               "doc_count": 14,
               "ASSET_CLASS": {
                  "value": 2
               }
            }
         ]
      }
   }
}
4

1 回答 1

1

基数聚合文档的第一行内容如下:

计算不同值的近似 计数的单值指标聚合。

(强调我的)

3000 次中有 10 次的误差远低于 1%,所以这是意料之中的。

基数聚合使用HyperLogLog演算的增强版本,它具有一些有趣的特性,例如恒定的内存复杂度和 O(N) 时间复杂度。

如果您需要更精确的结果,请尝试更高的precision_threshold参数设置。

GET /test/data/_search
{
   "size": 0,
   "aggs": {
      "ASSET_CLASS": {
         "terms": {
            "field": "ASSET_CLASS_WORST"
         },
         "aggs": {
            "ASSET_CLASS": {
               "cardinality": {
                  "field": "PARTY_ID",
                  "precision_threshold": 10000
               }
            }
         }
      },
      "Total count": {
         "cardinality": {
            "field": "PARTY_ID",
            "precision_threshold": 10000
         }
      }
   }
}
于 2016-10-18T14:56:45.873 回答