elasticsearch - 应用多个过滤器,使用 elasticsearch 聚合到单个指标中

标签 elasticsearch

有没有办法(可能聚合聚合桶)在聚合中应用多个过滤器并让它返回单个指标值?

例如,这是一个聚合查询,我在其中获取 option_id = 16 和 category.id = 870 的所有产品的最低/最高价格。

{
    "aggregations": {
        "prices": {
            "filters": {
                "filters": {
                    "category": {
                        "terms": {
                            "category.id": [ 870 ]
                        }
                    },
                    "option": {
                        "terms": {
                            "option_id": [ 16 ]
                        }
                    }
                }
            },
            "aggregations": {
                "price_min": {
                    "min": {
                        "field": "variant.price_total"
                    }
                },
                "price_max": {
                    "max": {
                        "field": "variant.price_total"
                    }
                }
            }
        }
    }
}

不幸的是,这给了我两个不同的值:

{
    "doc_count": 1450674,
    "prices": {
        "buckets": {
            "category": {
                "doc_count": 42979,
                "price_min": {
                    "value": 0.49
                },
                "price_max": {
                    "value": 39998.99
                }
            },
            "store": {
                "doc_count": 100961,
                "price_min": {
                    "value": 0.06
                },
                "price_max": {
                    "value": 644000
                }
            }
        }
    }
}

有没有办法将两个过滤器一起应用到一个指标/桶中?

最佳答案

是的。您需要使用 filter聚合而不是 filters 聚合。查询如下所示:

{
    "aggregations": {
        "prices": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "category.id": [
                                    870
                                ]
                            }
                        },
                        {
                            "terms": {
                                "option_id": [
                                    16
                                ]
                            }
                        }
                    ]
                }
            },
            "aggregations": {
                "price_min": {
                    "min": {
                        "field": "variant.price_total"
                    }
                },
                "price_max": {
                    "max": {
                        "field": "variant.price_total"
                    }
                }
            }
        }
    }
}

这将为您提供 price_min 的单个指标和 price_max 的单个指标,同时应用了两个过滤器。

关于elasticsearch - 应用多个过滤器,使用 elasticsearch 聚合到单个指标中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096607/

相关文章:

r - R中的弹性包装:按版本> v5排序...不起作用

elasticsearch - 有什么方法可以从RAM中获取elasticsearch索引数据吗?

c# - NEST如何在子文档上查找字段

ajax - 使用Ajax进行Elasticsearch的故障过滤结果

elasticsearch - elasticsearch 2.1.1-无法建立集群

sql - 需要帮助来优化可能出错的空间 SQL 查询

jdbc - Elasticsearch JDBC River吞噬了整个内存

java - 为 Elastic 构建 PreBuiltTransportClient 时出现 NoSuchMethodError

elasticsearch - 有没有办法排除 Elasticsearch 查询中的字段

elasticsearch - 找不到elasticsearch edge_ngrams分析器