对 top_hits 聚合求和

2023-11-30

简而言之,问题是:如果我对每个存储桶的 top_hits 进行聚合,如何对结果结构中的特定值求和?

Details:

我有许多记录,其中包含每个商店的一定数量。我想获得每个商店所有最新记录的总和。

为了获取每个商店的最新记录,我创建了以下聚合:

"latest_quantity_per_store": {
    "aggs": {
        "latest_quantity": {
            "top_hits": {
                "sort": [
                    {
                        "datetime": "desc"
                    },
                    {
                        "quantity": "asc"
                    }
                ],
                "_source": {
                    "includes": [
                        "quantity"
                    ]
                },
                "size": 1
            }
        }
    },
    "terms": {
        "field": "store",
        "size": 10000
    }
}

假设我有两个商店,每个商店有两个数量,对应两个不同的时间戳。这是该聚合的结果:

"latest_quantity_per_store": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
        {
            "key": "01",
            "doc_count": 2,
            "latest_quantity": {
                "hits": {
                    "total": 2,
                    "max_score": null,
                    "hits": [
                        {
                            "_index": "inventory-local",
                            "_type": "doc",
                            "_id": "O6wFD2UBG8e7nvSU8dYg",
                            "_score": null,
                            "_source": {
                                "quantity": 6
                            },
                            "sort": [
                                1532476800000,
                                6
                            ]
                        }
                    ]
                }
            }
        },
        {
            "key": "02",
            "doc_count": 2,
            "latest_quantity": {
                "hits": {
                    "total": 2,
                    "max_score": null,
                    "hits": [
                        {
                            "_index": "inventory-local",
                            "_type": "doc",
                            "_id": "pLUFD2UBHBuSGcoH0ZT4",
                            "_score": null,
                            "_source": {
                                "quantity": 11
                            },
                            "sort": [
                                1532476800000,
                                11
                            ]
                        }
                    ]
                }
            }
        }
    ]
}

我现在希望在 ElasticSearch 中进行聚合,对这些存储桶求和。在示例数据中,总和超过 6 和 11。我尝试了以下聚合:

"latest_quantity": {
    "sum_bucket": {
        "buckets_path": "latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity"
    }
}

但这会导致以下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "No aggregation [hits] found for path [latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity]"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "inventory-local",
        "node": "3z5CqmmAQ-yT2sUCb69DzA",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "No aggregation [hits] found for path [latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity]"
        }
      }
    ]
  },
  "status": 400
}

以某种方式从 ElasticSearch 获取数字 17 的正确聚合是什么?

我对我拥有的另一个聚合做了类似的事情,即平均值而不是 top_hits 聚合。

"average_quantity": {
    "sum_bucket": {
        "buckets_path": "average_quantity_per_store>average_quantity"
    }
},
"average_quantity_per_store": {
    "aggs": {
        "average_quantity": {
            "avg": {
                "field": "quantity"
            }
        }
    },
    "terms": {
        "field": "store",
        "size": 10000
    }
}

这按预期工作,这是结果:

"average_quantity_per_store": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
        {
            "key": "01",
            "doc_count": 2,
            "average_quantity": {
                "value": 6
            }
        },
        {
            "key": "02",
            "doc_count": 2,
            "average_quantity": {
                "value": 11.5
            }
        }
    ]
},
"average_quantity": {
    "value": 17.5
}

有一种方法可以混合使用来解决这个问题scripted_metric聚合和sum_bucket管道聚合。脚本化指标聚合有点复杂,但主要思想是允许您提供自己的分桶算法并从中吐出单个指标数字。

就您而言,您想要做的是计算出每个商店的最新数量,然后将这些商店数量相加。解决方案如下所示,我将在下面解释一些细节:

POST inventory-local/_search
{
  "size": 0,
  "aggs": {
    "bystore": {
      "terms": {
        "field": "store.keyword",
        "size": 10000
      },
      "aggs": {
        "latest_quantity": {
          "scripted_metric": {
            "init_script": "params._agg.quantities = new TreeMap()",
            "map_script": "params._agg.quantities.put(doc.datetime.date, [doc.datetime.date.millis, doc.quantity.value])",
            "combine_script": "return params._agg.quantities.lastEntry().getValue()",
            "reduce_script": "def maxkey = 0; def qty = 0; for (a in params._aggs) {def currentKey = a[0]; if (currentKey > maxkey) {maxkey = currentKey; qty = a[1]} } return qty;"
          }
        }
      }
    },
    "sum_latest_quantities": {
      "sum_bucket": {
        "buckets_path": "bystore>latest_quantity.value"
      }
    }
  }
}

请注意,为了使其工作,您需要设置script.painless.regex.enabled: true在你的elasticsearch.yml配置文件。

The init_script创建一个TreeMap对于每个分片。 这map_script填充TreeMap在每个分片上都有日期/数量的映射。我们放入映射中的值包含单个字符串中的时间戳和数量。稍后我们将需要该时间戳reduce_script. The combine_script只需取最后一个值TreeMap因为这是给定分片的最新数量。 大部分工作位于reduce_script。我们迭代每个分片的所有最新数量并返回最新的数量。

此时,我们已经掌握了每个商店的最新数量。剩下要做的就是使用sum_bucket管道聚合,以便对每个商店数量进行求和。结果就是 17。

响应如下所示:

 "aggregations": {
    "bystore": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "01",
          "doc_count": 2,
          "latest_quantity": {
            "value": 6
          }
        },
        {
          "key": "02",
          "doc_count": 2,
          "latest_quantity": {
            "value": 11
          }
        }
      ]
    },
    "sum_latest_quantities": {
      "value": 17
    }
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对 top_hits 聚合求和 的相关文章

随机推荐

  • 为什么此邮件消息无法正确解码?

    我有这个代码 它来自于Zend 阅读邮件例子 message mail gt getMessage 1 output first text plain part foundPart null foreach new RecursiveIte
  • 查看 SML 完整列表

    有没有办法使用 SML 打印完整列表 通常在 SML 中发生的情况是 当我有太多元素时 它会打印前几个元素 并用 分隔 然后省略列表的其余部分 但我想查看完整的列表 有什么办法可以做到这一点吗 val a 1 2 3 4 5 6 7 8 9
  • 使用 java 创建 Windows 用户帐户

    是否可以使用 java 代码创建 删除 Windows 用户帐户并设置其权限以使其成为管理员帐户 简单用户帐户或来宾帐户 我问这个问题已经过去一年了 我忘了发布答案 对不起 要创建用户帐户 我们需要通过使用程序包装清单文件来获得管理权限 清
  • 如何为 Android SurfaceView 找到最佳的 PixelFormat

    我发现更改 SurfaceView 中的像素格式对帧速率有很大影响 但是 我似乎无法找到一种方法来根据每个设备选择最佳格式 Example Override public void surfaceCreated final SurfaceH
  • 如何生成数据来测试 Snowflake 处理数千列表的能力?

    Snowflake 可以处理数千列吗 有没有办法可以生成测试数据来测试 Snowflake 在处理 比方说 2000 列时的性能 使用此脚本 您可以创建一个包含 2000 或任意数量 列的表 并为每个列指定一个默认随机值 CREATE or
  • Octave/Windows:图中显示变音符号但未保存为图像

    我在Windows下使用octave 3 8 2 带有gnuplot 我想在绘图的轴标签中写入 特殊字符 变音符号 和特殊字符 显示在图中 但不会使用打印保存到图像文件中 部分地 我可以使用 TeX 命令 mu 代替 但对于变音符号 a 代
  • 如何在三个表上使用连接

    我有三张桌子 表1 表2 表3 表 1 具有列 ID Table2 具有列名 ID Name 表三具有列名称Name 现在我想从 Table2 中的 table1 中检索 ID 以便与表中的 ID 关联的名称应在表 3 中 表1 ID 表2
  • lua5.2的错误:检测到多个Lua VM

    我最近使用5 2学习 我想尝试这样的 第1步 为lua构建一个c模块 include lua h include lauxlib h include lualib h include
  • 在响应式环境中使用 rem 作为字体大小单位时,哪种后备方案最好?

    最近我想知道使用 rem 作为字体大小单位时哪种后备最好 像素似乎很合适 但如果您想更改特定媒体查询中的全局字体大小 则需要重新定义每个基于 px 的字体大小 这是一个例子 如果没有任何旧浏览器的后备 我们可以使用 Mobile Style
  • 将具有公共 id 的行压缩为一行[重复]

    这个问题在这里已经有答案了 我有一个问题尚未找到答案 有类似的问题 其解决方案在我的情况下不太有效 我有一个包含四列的数据集 如下例所示 Name Session Sequence Page Bob 001 001 home Bob 001
  • 在 python 中,如何比较两个数字字符串而不将它们转换为 int()?

    例如 在不使用 int 和 def 的情况下检查它们是否大于 小于或等于 num1 67 num2 1954 左补零 然后按字典顺序比较字符串 num1 67 num2 1954 if num1 zfill 10 lt num2 zfill
  • Java输出String和方法返回时,为什么方法返回先输出?

    在下面的代码中 如果字符串 Mult 出现在test1 4 方法调用 为什么方法输出在字符串之前 为什么它会从输出方法的第一部分跳出 然后离开方法输出字符串 然后返回到方法输出方法的返回值 code public class Scratch
  • 通过两个代理的 HttpWebRequest

    我最近建立了一个网站 它使用地理 DNS 将 DNS 解析为两个不同的 IP 具体取决于您的位置 然而 这意味着要监控网站 我需要确保该网站在两个地理位置都可用 为此 我在 net 中编写了一个小程序 不断尝试使用 HttpWebReque
  • 连接到特定 HID 配置文件蓝牙设备

    我将蓝牙条形码扫描仪连接到我的 Android 平板电脑 条码扫描仪与 Android 设备绑定作为输入设备 HID 配置文件 它在系统蓝牙管理器中显示为键盘或鼠标 我发现蓝牙配置文件输入设备类存在但被隐藏 class 和 btprofil
  • 使用 LibGDX 登录 Google

    我有问题 我正在用 LibGDX 制作游戏 现在我想实现Google登录 我到处寻找 但什么也没找到 我需要的是一个解析器来抽象特定平台的代码 但我不知道该怎么做 有人可以帮忙吗 EDIT 这是代码 这是我的 Android 解析器 pub
  • 用于在页面内创建 div 样式窗口的 JavaScript 库

    我试图找到一个好的 JavaScript 库 它可以在我网站的页面中创建一个漂亮的 内部窗口 弹出窗口 我不想担心屏幕定位 即不必计算窗口的大小是否会超出屏幕等 而只需制作一个包含内容的新弹出窗口 我将使用 NET 3 5 ASP NET
  • 使用 Plink 在另一台服务器(jumphost)后面的远程服务器上执行命令

    我正在尝试使用 Power Automate Desktop for PuTTY 进行自动化 我遇到了一个使用 cmd 来运行命令的解决方案plink 我使用了以下步骤 我将PuTTY添加到系统变量中 我使用了命令 在cmd中 plink
  • CRM 2013 中的富文本编辑器 (WYSIWYG)

    有时 CRM 界面中的 HTML 编辑器很有用 可以直接在 CRM 2013 中实现编辑器 作为编辑器 我们将使用 ckeditor 它允许在不安装在服务器上的情况下使用它 确定您想要使用富文本编辑器的字段 Create html 网络资源
  • 如何在同一时间 shell 中读取两个文件

    我有两个文件 A john 1 2 3 4 5 6 7 Ely 10 9 9 9 9 9 9 Maria 3 5 7 9 2 1 4 Rox 10 10 10 10 10 10 10 B john 7 5 Ely 4 5 Maria 3 7
  • 对 top_hits 聚合求和

    简而言之 问题是 如果我对每个存储桶的 top hits 进行聚合 如何对结果结构中的特定值求和 Details 我有许多记录 其中包含每个商店的一定数量 我想获得每个商店所有最新记录的总和 为了获取每个商店的最新记录 我创建了以下聚合 l