ElasticSearch - 返回唯一值 [英] ElasticSearch - Return Unique Values

查看:32
本文介绍了ElasticSearch - 返回唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从记录中获取所有 languages 的值并使它们唯一.

How would I get the values of all the languages from the records and make them unique.

记录

PUT items/1
{ "language" : 10 }

PUT items/2
{ "language" : 11 }

PUT items/3
{ "language" : 10 }

查询

GET items/_search
{ ... }

# => Expected Response
[10, 11]

任何帮助都会很棒.

推荐答案

您可以使用 术语聚合.

{
"size": 0,
"aggs" : {
    "langs" : {
        "terms" : { "field" : "language",  "size" : 500 }
    }
}}

聚合中的 size 参数指定要包含在聚合结果中的最大术语数.如果您需要所有结果,请将其设置为大于数据中唯一字词数量的值.

The size parameter within the aggregation specifies the maximum number of terms to include in the aggregation result. If you need all results, set this to a value that is larger than the number of unique terms in your data.

搜索将返回如下内容:

{
"took" : 16,
"timed_out" : false,
"_shards" : {
  "total" : 2,
  "successful" : 2,
  "failed" : 0
},
"hits" : {
"total" : 1000000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
  "langs" : {
    "buckets" : [ {
      "key" : "10",
      "doc_count" : 244812
    }, {
      "key" : "11",
      "doc_count" : 136794
 
    }, {
      "key" : "12",
      "doc_count" : 32312
       } ]
    }
  }
}

这篇关于ElasticSearch - 返回唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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