简体   繁体   English

将查询添加到具有date_histogram和聚合的查询中的过滤器

[英]Add a filter to a query with a date_histogram and aggregation in elasticsearch

I need to display a table of units sold this year by month by category. 我需要按类别显示今年销售的单位表。
For example: 例如:

2015      Jan  Feb  Mar  Apr May  Jun  Jul  Aug
category1  19   25   30   51  12   10    0    0
category2 105  110  125  131 209  233  310   80
category3  33   35   37    0   0    0    0    0

Here's my query. 这是我的查询。 Note that I'm missing the "this year" requirement. 请注意,我缺少“今年”要求。 How do I add it? 如何添加? Adding a filter as the first child of "doc" gives an error: Found two aggregation type definitions in [doc] 将过滤器添加为“ doc”的第一个子项会产生错误:在[doc]中找到了两个聚合类型定义

{
    "doc": {
        "date_histogram": {
            "field": "sale_createdDateTime",
            "interval": "month",
            "format": "yyyy-MM-dd",
            "min_doc_count": 0,
            "extended_bounds": {
                 "min": "2015-01-01",
                 "max": "2015-12-31"
             }
        },
        "aggs": {
            "per_category": {
                "terms": {
                    "field": "product_category"
                }
            }
        }
    }
}

You need to wrap your current aggregation within the filter Aggregation . 您需要将当前的聚合包装在过滤器Aggregation中 So the filter is not the first child of doc , but the first child of aggs . 因此,筛选器不是doc的第一个子项,而是aggs的第一个子aggs then, you have a nested aggs with your current aggregation. 然后,您的当前聚合有一个嵌套的aggs The result should be somewhat similar to : 结果应该类似于:

{
    "aggs": {
        "per_category": {
            "filtered_agg": {
                "filter": {
                    /*YEAR FILTER HERE*/
                },
                "aggs": {
                    "nested_terms_agg": {
                        "terms": {
                            "field": "product_category"
                        }
                    }
                }
            }
        }
    }
}

( NOTE: filtered_agg and nested_terms_agg are just names I chose) 注意: filtered_aggnested_terms_agg只是我选择的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM