在弹性搜索中按 ID 自定义排序顺序作为数组
Custom Sort Order by Ids as array in elastic search
场景是这样的,我们发送数组中的产品 ID 作为 [101,102,103,.....等等]
但作为回应,我们没有以相同的顺序获得结果有没有什么办法可以按照我们在弹性搜索查询中发送的顺序获得记录
{
"my_videos": {
"mappings": {
"doc": {
"properties": {
"createdAt": {
"type": "date"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"duration": {
"type": "long"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastUpdatedByUserId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"status": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updatedAt": {
"type": "date"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
GET my_videos/doc/_search
{
"query": {
"bool": {
"should": [{
"match": {
"id.keyword": "01cm7kr0px0tmyzkmsjb55xd3a"
}
},
{
"match": {
"id.keyword": "01cktwwyfnyt9d2nqj9ycwxcme"
}
},
{
"match": {
"id.keyword": "01chyvzv678r1h0y0rx4e4bv8t"
}
}
],
"minimum_should_match": 1
}
},
"sort": [{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"inline": "if(params.scores.containsKey(doc['id.keyword'].value)) { return params.scores[doc['id.keyword'].value];} return 100000;",
"params": {
"scores": {
"01cm7kr0px0tmyzkmsjb55xd3a": 0,
"01cktwwyfnyt9d2nqj9ycwxcme": 1,
"01chyvzv678r1h0y0rx4e4bv8t": 2
}
}
},
"order": "asc"
}
}],
"from": 0,
"size": 10
}
场景是这样的,我们发送数组中的产品 ID 作为 [101,102,103,.....等等] 但作为回应,我们没有以相同的顺序获得结果有没有什么办法可以按照我们在弹性搜索查询中发送的顺序获得记录
{
"my_videos": {
"mappings": {
"doc": {
"properties": {
"createdAt": {
"type": "date"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"duration": {
"type": "long"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastUpdatedByUserId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"status": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updatedAt": {
"type": "date"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
GET my_videos/doc/_search
{
"query": {
"bool": {
"should": [{
"match": {
"id.keyword": "01cm7kr0px0tmyzkmsjb55xd3a"
}
},
{
"match": {
"id.keyword": "01cktwwyfnyt9d2nqj9ycwxcme"
}
},
{
"match": {
"id.keyword": "01chyvzv678r1h0y0rx4e4bv8t"
}
}
],
"minimum_should_match": 1
}
},
"sort": [{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"inline": "if(params.scores.containsKey(doc['id.keyword'].value)) { return params.scores[doc['id.keyword'].value];} return 100000;",
"params": {
"scores": {
"01cm7kr0px0tmyzkmsjb55xd3a": 0,
"01cktwwyfnyt9d2nqj9ycwxcme": 1,
"01chyvzv678r1h0y0rx4e4bv8t": 2
}
}
},
"order": "asc"
}
}],
"from": 0,
"size": 10
}