elasticsearch aggs 返回错误的计数数字

Dan*_*y M 2 aggregation elasticsearch

我正在尝试进行一些聚合查询并遇到一些问题。

GET /my_index/_search
{
"size" : 0,
"aggs":{
   "group_by":{
       "terms": {
            "field" : "category"
       }
   }
  }
  }
Run Code Online (Sandbox Code Playgroud)

这让我回来了:

"hits": {
  "total": 180,
  "max_score": 0,
  "hits": []
 },
"aggregations": {
  "group_by": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 1,
     "buckets": [
        {
           "key": "pf_rd_m",
           "doc_count": 139
        },
        {
           "key": "other",
           "doc_count": 13
        },
        {
           "key": "_encoding",
           "doc_count": 12
        },
        {
           "key": "ie",
           "doc_count": 10
        },
        {
           "key": "cadeaux",
           "doc_count": 2
        },
        {
           "key": "cartes",
           "doc_count": 2
        },
        {
           "key": "cheques",
           "doc_count": 2
        },
        {
           "key": "home",
           "doc_count": 2
        },
        {
           "key": "nav_logo",
           "doc_count": 1
        },
        {
           "key": "ref",
           "doc_count": 1
        }
     ]
  }
Run Code Online (Sandbox Code Playgroud)

}

如您所见,这告诉我有 180 个文档,但是如果我计算存储桶中每个键的 doc_count 总和,我会发现更多元素...

这当然是对 elasticsearch 标记化机制(https://www.elastic.co/guide/en/elasticsearch/guide/current/aggregations-and-analysis.html

所以我在这篇es post中尝试了解决方案,但仍然无法正常工作。这是我的映射

"properties":{
                            "status":{
                              "type":"integer",
                              "index":"analyzed"
                            },
                            "category":{
                              "type":"string",
                              "fields": {
                                "raw" : {
                                  "type": "string",
                                  "index": "not_analyzed"
                                }
                              }
                            },
                            "dynamic_templates": [
                                { "notanalyzed": {
                                      "match":              "*",
                                      "match_mapping_type": "string",
                                      "mapping": {
                                          "type":        "string",
                                          "index":       "not_analyzed"
                                      }
                                   }
                                }
                              ]
                          }
Run Code Online (Sandbox Code Playgroud)

如您所见,我有一个名为“类别”的字段。并将“原始”添加为 not_analyzed 字符串,但仍然返回错误的数字。

当我尝试这个时:

GET /my_index/_search
{
"size" : 0,
"aggs":{
   "group_by":{
       "terms": {
            "field" : "category.raw"
         }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

这返回:

"hits": {
  "total": 180,
  "max_score": 0,
  "hits": []
},
"aggregations": {
  "group_by": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": []
  }
}
Run Code Online (Sandbox Code Playgroud)

这很奇怪。有什么帮助吗?

Hkn*_*ntn 5

文档中所述,

术语聚合中的文档计数(以及任何子聚合的结果)并不总是准确的。这是因为每个分片都提供了自己对术语有序列表应该是什么的看法,并将这些结合起来给出最终的看法

为了以牺牲资源为代价克服这个问题,可以使用分片大小参数。
同样,来自文档:
Shard Size

请求的大小越大,结果就越准确,但计算最终结果的成本也越高(这都是由于在分片级别上管理的优先级队列更大,也由于在分片之间进行了更大的数据传输)节点和客户端)。该shard_size参数可用于最大程度地减少因请求大小而带来的额外工作。定义后,它将确定协调节点将从每个分片请求多少项。一旦所有分片都响应,协调节点将根据大小参数将它们减少到最终结果 - 这样,可以提高返回术语的准确性并避免将大量存储桶流回的开销给客户。如果设置为0shard_size则将设置为Integer.MAX_VALUE

如果将分片大小参数添加到查询中:

GET /my_index/_search
{
"size" : 0,
"aggs":{
   "group_by":{
       "terms": {
            "field" : "category.raw",
            "shard_size" : 0
         }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

  • ^ 这是由于 ES5,在撰写本文时还不存在。ES5 不再允许大多数设置为 0,因为大多数用户没有意识到由此可能出现的问题 (2认同)