这是我的查询。我正在尝试获取“men_fashion”和“men_shoes”类别中的所有产品(类别被用作术语/标签)。然后我想查询整个结果集并搜索其中包含“男靴黄色”的产品。
下面的查询工作得很好,但现在我没有得到正确的聚合结果。它给了我所有的品牌,因为我只对品牌感兴趣。
{
"size": 15,
"from": 0,
"query": {
"query_string": {
"query": "men boots yellow"
}
},
"filter": {
"bool": {
"must": [{
"match": {
"active": 1
}
}, {
"match": {
"category": "men_fashion"
}
}, {
"match": {
"category": "men_shoes"
}
}]
}
},
"aggs": {
"brands": {
"terms": {
"size": 100,
"field": "brand"
}
}
}
}
我认为这可能是由于我应用了过滤器,但如果这有点复杂,我可以使用一个简单的查询来实现这一点而无需过滤器。
最佳答案
您正在使用 post filter而不是普通的查询过滤器,请尝试这样:
{
"size": 15,
"from": 0,
"query": {
"bool": {
"must": {
"query_string": {
"query": "men boots yellow"
}
},
"filter": [
{
"match": {
"active": 1
}
},
{
"match": {
"category": "men_fashion"
}
},
{
"match": {
"category": "men_shoes"
}
}
]
}
},
"aggs": {
"brands": {
"terms": {
"size": 100,
"field": "brand"
}
}
}
}
关于Elasticsearch 聚合未应用于过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769153/