【问题标题】:How to select fields after aggregation in Elastic Search 2.3如何在 Elastic Search 2.3 中聚合后选择字段
【发布时间】:2016-08-12 04:47:46
【问题描述】:

我有以下索引架构:-

 PUT
    "mappings": {
        "event": {
          "properties": {
            "@timestamp": { "type": "date",    "doc_values": true},
            "partner_id":  { "type": "integer", "doc_values": true},
            "event_id": { "type": "integer", "doc_values": true},
            "count": { "type": "integer", "doc_values": true, "index": "no" },
            "device_id": { "type": "string", "index":"not_analyzed","doc_values":true }
            "product_id": { "type": "integer", "doc_values": true},
          }
        }
      }

我需要与以下查询等效的结果:-

SELECT product_id, device_id, sum(count) FROM index WHERE partner_id=5 AND timestamp<=end_date AND timestamp>=start_date GROUP BY device_id,product_id having sum(count)>1;

我可以通过以下弹性查询来实现结果:-

GET
{
    "store": true,
    "size":0,
    "aggs":{
        "matching_events":{
            "filter":{
                "bool":{
                    "must":[
                        {
                            "term":{
                                "partner_id":5
                            }
                        },
                        {
                            "range":{
                                "@timestamp":{
                                    "from":1470904000,
                                    "to":1470904999
                                }
                            }
                        }
                    ]
                }
            },
            "aggs":{
                      "group_by_productid": {
                      "terms":{
                        "field":"product_id"
                    },
            "aggs":{
                "group_by_device_id":{
                    "terms":{
                        "field":"device_id"
                    },
                    "aggs":{
                        "total_count":{
                            "sum":{
                                "field":"count"
                            }
                        },
                        "sales_bucket_filter":{
                            "bucket_selector":{
                                "buckets_path":{
                                    "totalCount":"total_count"
                                },
                                "script": {"inline": "totalCount > 1"}
                            }
                        }
                    }
                    }}
                }
            }
        }
    }

}'

但是对于count is &lt;=1 查询返回空桶的情况,其键为product_id。现在在 4000 万组中,只有 100k 满足条件,所以我返回的结果集很大,其中大部分是无用的。聚合后如何仅选择特定字段?我试过这个但没有工作-`“fields”:[“aggregations.matching_events.group_by_productid.group_by_device_id.buckets.key”]

编辑:

我有以下一组数据:-

 device id                       Partner Id         Count 
db63te2bd38672921ffw27t82        367                   3
db63te2bd38672921ffw27t82        272                   1

我去这个输出:-

{

    "took":6,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "failed":0
    },
    "hits":{
        "total":7,
        "max_score":0.0,
        "hits":[
        ]
    },
    "aggregations":{
        "matching_events":{
            "doc_count":5,
            "group_by_productid":{
                "doc_count_error_upper_bound":0,
                "sum_other_doc_count":0,
                "buckets":[
                    {
                        "key":367,
                        "doc_count":3,
                        "group_by_device_id":{
                            "doc_count_error_upper_bound":0,
                            "sum_other_doc_count":0,
                            "buckets":[
                                {
                                    "key":"db63te2bd38672921ffw27t82",
                                    "doc_count":3,
                                    "total_count":{
                                        "value":3.0
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "key":272,
                        "doc_count":1,
                        "group_by_device_id":{
                            "doc_count_error_upper_bound":0,
                            "sum_other_doc_count":0,
                            "buckets":[
                            ]
                        }
                    }
                ]
            }
        }
    }

}

如您所见,键为 272 的存储桶是空的,这是有道理的,但不应该将这个存储桶从结果集中完全删除吗?

【问题讨论】:

  • 你试过top_hits聚合吗?
  • 不,但据我了解,top_hit 返回最佳匹配文档。对我来说,匹配是布尔值 - 是或否。如果有比赛,就不应该有任何得分。可能是我没完全理解!
  • 我想我不明白这个要求。您想过滤掉sales_bucket_filter 结果为空的group_by_device_id 存储桶(意味着子存储桶已被过滤)?
  • @AndreiStefan 感谢您的帮助。我在问题中添加了更多细节。
  • 是的,只显示我们有非空桶的聚合!

标签: elasticsearch


【解决方案1】:

我刚刚发现有一个相当新的问题和 PR 将 _bucket_count 路径添加到 buckets_path 选项,以便聚合可以根据另一个聚合具有的存储桶数量过滤父存储桶.换句话说,如果父 bucket_selector_bucket_count 为 0,则应删除存储桶。

这是 github 问题:https://github.com/elastic/elasticsearch/issues/19553

【讨论】:

  • 太棒了,但据我了解,这不是 2.3 版的一部分,对吧?
  • 是的,它将在 ES 5 中可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 2021-03-09
  • 1970-01-01
  • 2021-02-04
相关资源
最近更新 更多