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"
}
}
}
}
解决方案
您可以使用术语聚合来获取不同的值。例如:
GET messages/_search
{
"size":"0",
"aggs" : {
"group_ids" : {
"terms" : { "field" : "group_id", "size" : 1000 }
}
}
}
推荐阅读
- c# - Visual Studio Formatting — 命名空间中的颜色对象
- python - 将变量从 python 脚本的函数导入另一个 python 脚本
- algorithm - 我无法找到我在采访中看到的代码的复杂性。请看我下面的代码
- java - Javafx - 在 intelliJ 上出现 gradle 错误:包 javafx.fxml 不存在导入 javafx.fxml.FXML
- javascript - 使用 Javascipt 获取元素的继承(级联)祖先 CSS 类
- python - 自使用 github3.py 添加最后一个标签以来,如何获取所有合并的拉取请求?
- angular - 如何在 Angular 中为 mat-chip-list 的输入字段添加水平滚动?
- node.js - 在 express 应用程序中实现 keycloak 连接
- css - 基于路由有条件地渲染 CSS (React)
- javascript - 等待 angularfire 存储上传