Elasticsearch 聚合未应用于过滤器
Elasticsearch aggregation not being applied to filters
这是我的查询。我正在尝试获取 "men_fashion" 和 "men_shoes" 类别中的所有产品(类别被用作 terms/tags)。然后我想查询整个结果集并搜索其中包含"men boots yellow"的产品。
下面的查询工作得很好,但现在我没有得到正确的聚合结果。它为我提供了所有品牌,因为我只对这些品牌感兴趣。
{
"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"
}
}
}
}
这是我的查询。我正在尝试获取 "men_fashion" 和 "men_shoes" 类别中的所有产品(类别被用作 terms/tags)。然后我想查询整个结果集并搜索其中包含"men boots yellow"的产品。
下面的查询工作得很好,但现在我没有得到正确的聚合结果。它为我提供了所有品牌,因为我只对这些品牌感兴趣。
{
"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"
}
}
}
}