【问题标题】:ElasticSearch aggregation functionElasticSearch 聚合函数
【发布时间】:2014-02-22 21:40:20
【问题描述】:

是否可以在弹性搜索中定义聚合函数?

例如数据:

author weekday status
me     monday  ok
me     tuesday ok
me     moday  bad

我想获得基于作者和工作日的聚合,并且作为值我想获得状态字段的连接:

agg1 agg2    value
me   monday  ok,bad
me   tuesday ok

我知道你可以做计数,但是可以定义另一个用于聚合的函数吗?

编辑/回答:看起来 ES 中没有多行聚合支持,因此我们必须在最后一个字段上使用子聚合(参见 Akshay 的示例)。如果您需要更复杂的聚合函数,则按 id 聚合(注意,您将无法使用 _id,因此您必须在其他字段中复制它) - 这样您就可以进行高级操作聚合每个桶中的单个项目。

【问题讨论】:

  • 一个方面应该可以做到这一点

标签: elasticsearch aggregate-functions


【解决方案1】:

您可以使用 1.0 中的sub aggregations 大致获得所需的内容。假设文档的结构为作者、工作日和状态,您可以使用以下聚合:

{
  "size": 0,
  "aggs": {
    "author": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "days": {
          "terms": {
            "field": "weekday"
          },
          "aggs": {
            "status": {
              "terms": {
                "field": "status"
              }
            }
          }
        }
      }
    }
  }
}

这会给你以下结果:

{
   ...
   "aggregations": {
      "author": {
         "buckets": [
            {
               "key": "me",
               "doc_count": 3,
               "days": {
                  "buckets": [
                     {
                        "key": "monday",
                        "doc_count": 2,
                        "status": {
                           "buckets": [
                              {
                                 "key": "bad",
                                 "doc_count": 1
                              },
                              {
                                 "key": "ok",
                                 "doc_count": 1
                              }
                           ]
                        }
                     },
                     {
                        "key": "tuesday",
                        "doc_count": 1,
                        "status": {
                           "buckets": [
                              {
                                 "key": "ok",
                                 "doc_count": 1
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

【讨论】:

  • 是的,这几乎就是我所做的。该解决方案仍然需要在客户端处理存储桶的最后一部分。
猜你喜欢
  • 2023-04-02
  • 2017-09-24
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多