简体   繁体   English

elasticsearch-如何聚合和按多行过滤

[英]elasticsearch - how to aggregate and filter by multiple rows

I have the following data. 我有以下数据。

在此处输入图片说明

How can I query all countries that have currency 1 and 2? 如何查询所有具有货币1和2的国家?

The country can have multiple rows with the same currency (so count, can't help) 该国家/地区可以使用相同的货币显示多行(因此计数,无济于事)

You should filter queries by desired currencies and aggregate them with a term aggregation. 您应按所需货币过滤查询,并用术语汇总对其进行汇总。
The tricky part is force Elasticsearch to get all possible countries. 棘手的部分是迫使Elasticsearch获得所有可能的国家。 This can be done using size=0 parameter in terms aggregation 这可以通过在聚合中使用size = 0参数来完成

currencies_criteria = [1,2]

elasticsearch_query = {
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must" : [
                        {
                            "term": {"currency" : currencies_criteria}
                        }
                    ]

                }
            }
        }
    },
    "aggs" : {
        "country_count" : {
            "terms" : {
                "field" : "country",
                #https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-approximate-counts
                "size"  : 0
            }
        }
    }
}

You can see the sample execution of this answer in this notebook 您可以在此笔记本中看到此答案的示例执行

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

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