弹性 - 优先排序值
Elastic - Sorting value with priority
所以,我有 1 个字段,我们可以称它为 timeInt。类型值为整数。该值从0开始计数。我想按优先级条件对其进行排序。 0 / 1 将放在顶部,然后较大的值放在值 0 / 1 之后。例如:
0
1个
100
99
98
等...
我不知道如何实现,我已经搜索了 google,但仍然卡住了,我只是尝试这样的查询排序:
排序:[{timeInt:{'order':'asc'}}]
但它只是升序排序
let newQuery = {
index: `${config.get('/indexElastic').needAction}`,
type: 'notification',
body: {
from: from, size: size,
query: match_all: {},
sort:[{timeInt:{'order':'asc'}}]
}
};
预期结果:
0
1个
100
99
98
等...
当前结果:
0
1个
2个
3个
4个
等...
您可以应用 boost to the values 0
and 1
in a should,然后通过包含 _score
字段来修改您的排序查询,如下所示。
同时查看您的预期结果,timeInt
的排序顺序为 desc
而不是 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"
}
}
]
}
下面是您的结果的显示方式:
{
"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
]
}
]
}
}
注意: 所有其他文档都有 1
的 _score
,因为 match_all
查询。
如果有帮助请告诉我!
所以,我有 1 个字段,我们可以称它为 timeInt。类型值为整数。该值从0开始计数。我想按优先级条件对其进行排序。 0 / 1 将放在顶部,然后较大的值放在值 0 / 1 之后。例如:
0 1个 100 99 98 等...
我不知道如何实现,我已经搜索了 google,但仍然卡住了,我只是尝试这样的查询排序:
排序:[{timeInt:{'order':'asc'}}]
但它只是升序排序
let newQuery = {
index: `${config.get('/indexElastic').needAction}`,
type: 'notification',
body: {
from: from, size: size,
query: match_all: {},
sort:[{timeInt:{'order':'asc'}}]
}
};
预期结果:
0 1个 100 99 98 等...
当前结果:
0 1个 2个 3个 4个 等...
您可以应用 boost to the values 0
and 1
in a should,然后通过包含 _score
字段来修改您的排序查询,如下所示。
同时查看您的预期结果,timeInt
的排序顺序为 desc
而不是 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"
}
}
]
}
下面是您的结果的显示方式:
{
"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
]
}
]
}
}
注意: 所有其他文档都有 1
的 _score
,因为 match_all
查询。
如果有帮助请告诉我!