在Elasticsearch中输出唯一值的列表

时间:2019-07-19 08:36:32

标签: python elasticsearch

我的Elasticsearch索引在25个不同的组中发布了将近700.000社交媒体消息。每封邮件都是JSON,其中包含chat.id键。

我需要构建一个查询以在我的Python脚本中使用,以便仅打印chat.id值一次。

简而言之,我的脚本应将数据库中的组输出。 如果我参加了25个小组,那么我预计会看到25个chat.id。

当前,我通过阅读每条社交媒体消息并提取每条消息的chat.id值来获取列表。但是随着索引索引数量的增加,它变得更长,耗时并且对CPU的要求也很高。

我找不到如何构建查询以同时实现此结果的方法。

我的文档的结构如下:

    {
      "_index": "indexname",
      "_type": "_doc",
      "_source": {
        "id": 372353,
        "audio": {},
        "author_signature": null,
        "caption": null,
        "channel_chat_created": null,
        "chat": {
           "id": 1011449296138,
           "type": "supergroup",
           "username": null,
          "first_name": null,
          "title": "chatname"

到目前为止,我使用的查询是这样:

    query= {
      "aggs": {
        "chatids": {
          "terms": {
            "field": "chat.id"
          }
        }
     }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用terms aggregation来获得不同的值。例如:

GET messages/_search
{
 "size":"0",
 "aggs" : {
  "group_ids" : {
   "terms" : { "field" : "group_id", "size" : 1000 }
   }
  }
}