gpt4 book ai didi

javascript - Elastic - 优先排序值

转载 作者:行者123 更新时间:2023-12-02 23:07:30 26 4
gpt4 key购买 nike

所以,我有 1 个字段,我们可以将其称为 timeInt。类型值是整数。该值是从0开始计数的。我想按优先级条件对其进行排序。 0/1 将放置在顶部,然后将较大的值放置在值 0/1 之后。例如:

011009998等等...

我不知道如何实现这一点,我已经在谷歌上搜索,但仍然卡住了,我只是尝试像这样的查询排序:

排序:[{timeInt:{'order':'asc'}}]

但它只是升序排列

let newQuery = {
index: `${config.get('/indexElastic').needAction}`,
type: 'notification',
body: {
from: from, size: size,
query: match_all: {},
sort:[{timeInt:{'order':'asc'}}]
}
};

预期结果:

011009998等等...

当前结果:

01234等等...

最佳答案

您可以申请boostshould 中的值 01然后通过包含 _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
]
}
]
}
}

注意:所有其他文档的 _score 均为 1,因为 match_all询问。

请告诉我这是否有帮助!

关于javascript - Elastic - 优先排序值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com