对于获取所有产品,我有以下查询。我试图实现的是保持缺货产品,即stock_sum = 0在底部的产品:
{
"sort": [
{
"updated_at": {
"order": "desc"
}
}
],
"size": 10,
"from": 0,
"query": {
"bool": {
"should": [
{
"range": {
"stock_sum": {
"gte": 1,
"boost": 5
}
}
}
]
}
}
}但是使用上面的查询,sort似乎完全覆盖了should,我猜这就是它的行为方式。我尝试过的几件事是将should更改为must --在本例中,脱销的产品被完全排除在外(这不是我想要的,我仍然想要底部的缺货产品)。
另一种方法是删除排序,然后应该查询似乎有效果,但我仍然需要排序。因此,我的问题是如何使sort和bool => should查询协同工作?即按updated_at排序,但也将stock_sum = 0保持在底部?
发布于 2022-05-26 09:25:57
在相同的match_all子句中使用constant_score和should查询,并先按_score按asc排序,然后由updated_at按desc进行排序,这对于您的示例应该适用。下面是一个示例查询:
{
"sort": [
{
"_score": {
"order": "asc"
}
},
{
"updated_at": {
"order": "desc"
}
}
]
"query": {
"bool": {
"should": [
{
"match_all": {}
},
{
"constant_score": {
"filter": {
"term": {
"stock_sum": 0
}
},
"boost": 10
}
}
]
}
}
}https://stackoverflow.com/questions/72389149
复制相似问题