弹性-优先排序值 [英] Elastic - Sorting value with priority
问题描述
因此,我有1个字段,我们可以将其称为timeInt.类型值是整数.该值从0开始计数.我想按优先级对它进行排序.0/1将放在顶部,然后将较大的值放在值0/1之后.例如:
So, i have 1 field, we can call it timeInt. the type value is integer. The value is counted from 0. And i want to sort it with priority condition. 0 / 1 will be placed on the top then the bigger value is placed after value 0 / 1. Ex:
01个1009998等等...
0 1 100 99 98 etc...
我不知道该怎么实现,我已经在google上搜索了,仍然被卡住,我只是尝试像这样查询排序:
I don't know how can i achieve that, i already search on google, and still stucked, i just try query sort like this:
sort:[{{timeInt:{'order':'asc'}}]
sort:[{timeInt:{'order':'asc'}}]
但这只是升序
let newQuery = {
index: `${config.get('/indexElastic').needAction}`,
type: 'notification',
body: {
from: from, size: size,
query: match_all: {},
sort:[{timeInt:{'order':'asc'}}]
}
};
预期结果:
01个1009998等等...
0 1 100 99 98 etc...
当前结果:
01个2个34等等...
0 1 2 3 4 etc...
推荐答案
您可以应用应该,然后通过将 _score
字段包括为如下所示.
You can apply boost to the values 0
and 1
in a should and then modify your sort query by including _score
field as shown below.
还要查看您的预期结果, timeInt
的排序顺序将是 desc
,而不是 asc
.
Also looking at your expected result, the sorting order for timeInt
would be desc
and not asc
.
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"match_all": {}
},
{
"match": {
"timeInt": {
"query": 0,
"boost": 3
}
}
},
{
"match": {
"timeInt": {
"query": 1,
"boost": 2
}
}
}
]
}
},
"sort": [
{ "_score": "desc"},
{
"timeInt": {
"order": "desc"
}
}
]
}
下面是结果的显示方式:
Below is how your results would appear:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "myintindex",
"_type" : "_doc",
"_id" : "2",
"_score" : 4.0,
"_source" : {
"timeInt" : 0
},
"sort" : [
4.0,
0
]
},
{
"_index" : "myintindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 3.0,
"_source" : {
"timeInt" : 1
},
"sort" : [
3.0,
1
]
},
{
"_index" : "myintindex",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"timeInt" : 99
},
"sort" : [
1.0,
99
]
},
{
"_index" : "myintindex",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"timeInt" : 98
},
"sort" : [
1.0,
98
]
},
{
"_index" : "myintindex",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"timeInt" : 97
},
"sort" : [
1.0,
97
]
}
]
}
}
Note: All the other documents have the _score
of 1
, because of the match_all
query.
让我知道这是否有帮助!
Let me know if this helps!
这篇关于弹性-优先排序值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!