【问题标题】:Output list of unique values in ElasticsearchElasticsearch 中唯一值的输出列表
【发布时间】:2019-11-28 05:18:13
【问题描述】:

我的 Elasticsearch 索引中有近 700.000 条社交媒体消息发布在 25 个不同的群组中。每条消息都是一个 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"
          }
        }
     }
    }

【问题讨论】:

标签: python elasticsearch


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我编辑了添加当前查询的问题。我试过你的,但最终迭代了所有文档以提取单个 chat.id 值。此外,您建议的查询似乎与我的非常相似。迭代所有数据集确实需要很长时间。
  • 目前没有解决方案:(
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 2015-12-11
  • 2012-02-25
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多