我有以下用于獲取所有產品的查詢,我想要實作的是保持缺貨產品 IE 產品底部帶有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查詢協同作業?IE排序updated_at但也保持stock_sum = 0在底部?
uj5u.com熱心網友回復:
在同一子句中使用match_all和查詢并首先按by排序,然后按by排序應該適用于您的示例。這是一個示例查詢:constant_scoreshould_scoreascupdated_atdesc
{
"sort": [
{
"_score": {
"order": "asc"
}
},
{
"updated_at": {
"order": "desc"
}
}
]
"query": {
"bool": {
"should": [
{
"match_all": {}
},
{
"constant_score": {
"filter": {
"term": {
"stock_sum": 0
}
},
"boost": 10
}
}
]
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481742.html
