如何通过从Elasticsearch查询中避免使用1个过滤器来获取所有聚合 [英] How to get all aggregations by avoiding 1 filter from the elasticsearch query

查看:141
本文介绍了如何通过从Elasticsearch查询中避免使用1个过滤器来获取所有聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Elasticsearch查询,该查询从查询​​结果中获取汇总。聚合效果很好,因为如果我选择从女士时装类别中获取所有颜色均为金色的礼服。

I have an elasticsearch query which gets aggregations from the query results. The aggregations work fine because if i select to get all dresses from "women fashion" category that are golden in color.

聚合工作正常,因为它只返回金色。但是从逻辑上讲,我们需要该聚合中的所有颜色。

The aggregations are working fine as it only returns the color golden. But logically we need all the colors in that aggregation.

在前端过滤器上,我们直接显示来自聚合请求的记录。现在,在前端,当有人选择金色颜色时,它会向下钻取并删除所有其他滤色器,只显示金色。

On the frontend filter we are directly showing records from the aggregations requests. Right now On the frontend when someone selects "golden" color, it drills down and removes all other color filters and only shows golden color.

我们需要以某种方式避免使用该颜色从聚合中显示所有结果。

We need to somehow avoid the color from the aggregations to show all the results.

{
    "size": 15,
    "from": 0,
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "match": {
                            "category": "women_fashion"
                        }
                    }, {
                        "nested": {
                            "path": "variations",
                            "query": {
                                "bool": {
                                    "must": [{
                                        "match": {
                                            "variations.color": "golden"
                                        }
                                    }]
                                }
                            }
                        }
                    }],
                    "should": null
                }
            }
        }
    },
    "aggs": {
        "brands": {
            "terms": {
                "size": 10,
                "field": "brand"
            }
        },
        "min_price": {
            "min": {
                "field": "price"
            }
        },
        "max_price": {
            "max": {
                "field": "price"
            }
        },
        "variations": {
            "nested": {
                "path": "variations"
            },
            "aggs": {
                "size": {
                    "terms": {
                        "size": 100,
                        "field": "variations.size"
                    }
                },
                "color": {
                    "terms": {
                        "size": 100,
                        "field": "variations.color"
                    }
                },
                "waist_size": {
                    "terms": {
                        "size": 100,
                        "field": "variations.waist_size"
                    }
                }
            }
        }
    }
}

我的地图:

{
    "mappings": {
        "products": {
            "properties": {
                "variations": {
                    "type": "nested"
                }
            }
        }
    }
}

示例文档注释:

{
    "title": "100% Cotton Unstitched Suit For Men",
    "slug": "100-cotton-unstitched-suit-for-men",
    "price": 200,
    "sale_price": 0,
    "vendor_id": 32,
    "featured": 0,
    "viewed": 20,
    "stock": 4,
    "sku": "XXX-B",
    "rating": 0,
    "active": 1,
    "vendor_name": "vendor_name",
    "category": [
        "men_fashion",
        "traditional_clothing",
        "unstitched_fabric"
    ],
    "image": "imagename.jpg",
    "variations": [
        {
            "variation_id": "34",
            "stock": 5,
            "price": 200,
            "variation_image": "",
            "sku": "XXX-C",
            "size": "m",
            "color": "red"
        },
        {
            "variation_id": "35",
            "stock": 5,
            "price": 200,
            "variation_image": "",
            "sku": "XXX-D",
            "size": "l",
            "color": "red"
        }
    ]
}

在浏览了许多文档之后,有一个叫做后置过滤器,可以通过某种方式解决此问题。我尝试使用它,但是有点复杂。有人有过这方面的经验吗?

After going through lots of documentation, there is something called a post-filter that can somehow solve this issue. I tried using it but its a bit complicated. Does anyone have any past experience with this.

MY更新了带有全局结团的查询:
我尝试使用全局汇总,但现在它显示了弹性搜索中的所有品牌,因为我想显示与该类别产品相关的品牌。

MY Updated Query with global aggrgations: I have tried using global aggregations, but it now shows all the brands in elasticsearch, where as i want to show brands associated with products in that category.

{
    "size": 15,
    "from": 0,
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "match": {
                            "category": "women_fashion"
                        }
                    }, {
                        "nested": {
                            "path": "variations",
                            "query": {
                                "bool": {
                                    "must": [{
                                        "match": {
                                            "variations.color": "golden"
                                        }
                                    }]
                                }
                            }
                        }
                    }],
                    "should": null
                }
            }
        }
    },
    "aggs": {
        "all_brands" : {
            "global" : {},
            "aggs" : { 
                "brands": {
                    "terms": {
                        "size": 10,
                        "field": "brand"
                    }
                }
            }
        },
        "brands": {
            "terms": {
                "size": 10,
                "field": "brand"
            }
        },
        "min_price": {
            "min": {
                "field": "price"
            }
        },
        "max_price": {
            "max": {
                "field": "price"
            }
        },
        "variations": {
            "nested": {
                "path": "variations"
            },
            "aggs": {
                "size": {
                    "terms": {
                        "size": 100,
                        "field": "variations.size"
                    }
                },
                "color": {
                    "terms": {
                        "size": 100,
                        "field": "variations.color"
                    }
                },
                "waist_size": {
                    "terms": {
                        "size": 100,
                        "field": "variations.waist_size"
                    }
                }
            }
        }
    }
}


推荐答案

来自 https://www.elastic.co/guide/zh/ elasticsearch / guide / current / _scoping_aggregations.html#_global_bucket

使用 global:{}

Use "global" : {} in aggregation seem do the job.

如果之后需要过滤器聚合,请使用 https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/search-aggregations- bucket-filter-aggregation.html

If you need filter aggregation after that, use https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html.

这篇关于如何通过从Elasticsearch查询中避免使用1个过滤器来获取所有聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆