使用elasticsearch计算不同的值

Dev*_*per 11 facet aggregation elasticsearch

我正在学习弹性搜索,并希望计算不同的值.到目前为止,我可以计算值,但不是很明显.

以下是示例数据:

curl http://localhost:9200/store/item/ -XPOST -d '{
  "RestaurantId": 2,
  "RestaurantName": "Restaurant Brian",
  "DateTime": "2013-08-16T15:13:47.4833748+01:00"
}'

curl http://localhost:9200/store/item/ -XPOST -d '{
  "RestaurantId": 1,
  "RestaurantName": "Restaurant Cecil",
  "DateTime": "2013-08-16T15:13:47.4833748+01:00"
}'

curl http://localhost:9200/store/item/ -XPOST -d '{
  "RestaurantId": 1,
  "RestaurantName": "Restaurant Cecil",
  "DateTime": "2013-08-16T15:13:47.4833748+01:00"
}'
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试了什么:

curl -XPOST "http://localhost:9200/store/item/_search" -d '{
  "size": 0,
  "aggs": {
    "item": {
      "terms": {
        "field": "RestaurantName"
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "item": {
      "buckets": [
        {
          "key": "restaurant",
          "doc_count": 3
        },
        {
          "key": "cecil",
          "doc_count": 2
        },
        {
          "key": "brian",
          "doc_count": 1
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何计算cecil为1而不是2

c24*_*24b 6

您必须使用@coder提到的基数选项,您可以在文档中找到该选项

$ curl -XGET "http://localhost:9200/store/item/_search" -d'
{
"aggs" : {
    "restaurant_count" : {
        "cardinality" : {
            "field" : "RestaurantName",
            "precision_threshold": 100, 
            "rehash": false 
            }
          }
         }
}'
Run Code Online (Sandbox Code Playgroud)

这对我有用......

  • 使用 rehash 选项时出现此错误:`[7:23] [基数] rehash 不支持类型值:VALUE_BOOLEAN` (4认同)
  • 正如 @c24b 正确指出的那样,“基数”在这里起到了作用,但我想在这里指出一些事情: 1.“基数”聚合是一种基于 [HyperLogLog++ (HLL)][http: //static.googleusercontent.com/media/research.google.com/en//pubs/archive/40671.pdf] 算法。引用文档:“HLL 的工作原理是对输入进行哈希处理,并使用哈希中的位对基数进行概率估计”。“精度”和“内存”之间需要权衡。 (3认同)