elasticsearch - 如何对 Elasticsearch 聚合存储桶数据应用数学运算

标签 elasticsearch elasticsearch-painless

我正在使用 flex 搜索5.5.1。
我的要求是对以下数据集应用以下公式:(doc_count * 100/10):

{
  "took": 30,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_botId": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1,
          "doc_count": 9
        },
        {
          "key": 3,
          "doc_count": 4
        }
      ]
    }
  }
}

我应该能够为每个存储桶项目找到解决方案。我不想使用Java SDK来解决此问题,因为要求仅使用 flex 搜索REST API。

最佳答案

您可以使用 bucket_script 聚合来为每个存储桶运行脚本:

  "aggs": {
    "group_by_botId": {
      "terms": {
        "field": "botId"
      },
      "aggs": {
        "count": {
          "bucket_script": {
            "buckets_path": {
              "doc_count" : "_count"
            },
            "script": "params.doc_count * 100 / 10"
          }
        }
      }
    }
  }

关于elasticsearch - 如何对 Elasticsearch 聚合存储桶数据应用数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054997/

相关文章:

elasticsearch - ElasticSearch在无痛脚本中舍入浮点值

node.js - 如何在Node.js中添加多行无痛代码

java - 从 Elasticsearch 响应中删除元数据

django - 将 Django 应用程序部署到 Amazon AWS Elastic Beanstalk 时出现问题

python - 大型数据集上的ElasticSearch聚合

elasticsearch - ElasticSearch-如何在文档数组中编辑字段

python - ElasticSearch,将值附加到字段,但检查字段是否首先存在,如果不存在,则附加字段

elasticsearch - 仅删除所有文档中符合我的条件的字段

elasticsearch - 如何使用存储的文档中的属性运行查询?

elasticsearch - 将_routing与脚本中文档的另一个字段进行比较