我有一个弹性搜索查询,它从查询结果中获取聚合。聚合工作正常,因为如果我选择从“女性时尚”类别获得所有颜色为金色的礼服。
聚合工作正常,因为它只返回黄金色。但从逻辑上讲,我们需要聚合中的所有颜色。
在前端过滤器上,我们直接显示聚合请求中的记录。现在在有人选择“黄金”颜色的前端,它向下钻取并移除所有其他颜色过滤器,只显示金色。
我们需要以某种方式避免聚合中的颜色以显示所有结果。
{
"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"
}
]
}
经过大量文档后,有一种称为后置过滤器的东西可以某种方式解决这个问题。我试过用它但有点复杂。有没有人有过这方面的经验。
我使用全局聚合更新了查询: 我尝试过使用全球聚合,但它现在显示了elasticsearch中的所有品牌,我想展示与该类别产品相关的品牌。
{
"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"
}
}
}
}
}
}
答案 0 :(得分:0)