ElasticSearch Group by Multi Field,多字段聚合

This is something that can already be done using scripts. For example, if you have two fields f and g, you can run a terms aggregation on the union of the values of these fields by running the following aggregation (it works with both groovy and mvel):


GET test/_search
{
  "aggs": {
    "t": {
      "terms": {
        "script": "doc['f'].values + doc['g'].values"
      }
    }
  }
}
It might not be very performant, so if you plan on running a terms aggregation on several fields on a regular basis, you might want to use the copy_to directive in your mappings in order to copy field values to a dedicated field at indexing time and use this field to run the aggregations:


PUT test
{
  "mappings": {
    "test": {
      "properties": {
        "f": {
          "type": "string",
          "index": "not_analyzed",
          "copy_to": "f_and_g"
        },
        "g": {
          "type": "string",
          "index": "not_analyzed",
          "copy_to": "f_and_g"
        },
        "f_and_g": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}


[Add data]


GET test/_search
{
  "aggs": {
    "t": {
      "terms": {
        "field": "f_and_g"
      }
    }
  }
}
Adrien Grandjpountz closed this on 12 Aug
Clinton Gormley
Owner
clintongormley commented on 12 Aug
The reason why we're not planning on supporting this directly is that it would be much slower and heavier than a normal terms aggregation. The terms agg uses global ordinals (rather than concrete values) for counting, but the global ordinals for two different fields are completely separate, so we would have to look up each concrete value independently, which would be a huge performance cost.


With the solutions that @jpountz has suggested, the performance cost is obvious to the user: either you pay the price at aggregation time (with a script) or at index time (with the copy_to) field. We'd rather make this cost obvious to the user, instead of providing functionality which performs poorly.

SOURCE URL:https://github.com/elasticsearch/elasticsearch/issues/5100

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值