从elasticsearch中的字段获取唯一值

wpx232ag  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(407)

我有这样的文件结构:

[
{
   "uuid":1,
   "data":{....},
   "date":"2020-10-10T23:00:00"
},
{
   "uuid":1,
   "data":{....},
   "date":"2020-10-10T23:00:00"
},
{
   "uuid":1,
   "data":{....},
   "date":"2020-10-10T23:00:00"
},
{
   "uuid":2,
   "data":{....},
   "date":"2020-10-10T23:00:00"
},
{
   "uuid":2,
   "data":{....},
   "date":"2020-10-10T23:00:00"
}
]

如何编写返回uuid的查询。对于以上示例,我希望得到以下结果:

[1, 2]
9udxz4iz

9udxz4iz1#

聚合基于 uuid 字段:
一个有效的例子:
Map

PUT my_index
{
  "mappings": {
    "properties": {
      "uuid": {
        "type": "keyword"
      },
      "date": {
        "type": "date"
      }
    }
  }
}

插入一些文档:

POST my_index/_doc/1
{
   "uuid":1,
   "date":"2020-10-10T23:00:00"
}
POST my_index/_doc/2
{
   "uuid":1,
   "date":"2020-10-10T23:00:00"
}
POST my_index/_doc/3
{
   "uuid":2,
   "date":"2020-10-10T23:00:00"
}
POST my_index/_doc/4
{
   "uuid":2,
   "date":"2020-10-10T23:00:00"
}

搜索查询:

GET my_index/_search
{
  "size": 0, 
  "aggs": {
    "uuids": {
      "terms": {
        "field": "uuid",
        "size": 10
      }
    }
  }
}

结果

"aggregations" : {
    "uuids" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1",
          "doc_count" : 2
        },
        {
          "key" : "2",
          "doc_count" : 2
        }
      ]
    }
  }

这个 key 场内 bucketsuuid 价值观。

相关问题