我这里有一个测试索引数据如下:
如果用普通的排序方法对_id中的数据进行排序的话:
GET _search
{
"query": {
"term": {
"_index": "test"
}
},
"sort": {
"_id": {
"order": "asc"
}
},
"size": 20
}
因为_id的数据为字符串类型,因此排序出来的结果其实并不是我们想要的:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 9,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 19,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : { },
"sort" : [
"1"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : { },
"sort" : [
"10"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : { },
"sort" : [
"11"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "12",
"_score" : null,
"_source" : { },
"sort" : [
"12"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "13",
"_score" : null,
"_source" : { },
"sort" : [
"13"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "14",
"_score" : null,
"_source" : { },
"sort" : [
"14"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "15",
"_score" : null,
"_source" : { },
"sort" : [
"15"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "16",
"_score" : null,
"_source" : { },
"sort" : [
"16"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "17",
"_score" : null,
"_source" : { },
"sort" : [
"17"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "18",
"_score" : null,
"_source" : { },
"sort" : [
"18"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "19",
"_score" : null,
"_source" : { },
"sort" : [
"19"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : { },
"sort" : [
"2"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : { },
"sort" : [
"3"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : { },
"sort" : [
"4"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : { },
"sort" : [
"5"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : { },
"sort" : [
"6"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : { },
"sort" : [
"7"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : { },
"sort" : [
"8"
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : null,
"_source" : { },
"sort" : [
"9"
]
}
]
}
}
他会先对字符串的第一个字符进行排序,在依次对后面的字符进行排序。
想要对 String 类型的 _id 进行数字顺序排序,就需要用到排序脚本:
因此可以这样写:
GET _search
{
"query": {
"term": {
"_index": "test"
}
},
"sort": {
"_script": {
"type": "number",
"script": "Integer.parseInt(doc['_id'].value)",
"order": "asc"
}
},
"size": 20
}
看看结果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 19,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : { },
"sort" : [
1.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : { },
"sort" : [
2.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : { },
"sort" : [
3.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : { },
"sort" : [
4.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : { },
"sort" : [
5.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : { },
"sort" : [
6.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : { },
"sort" : [
7.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : { },
"sort" : [
8.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "9",
"_score" : null,
"_source" : { },
"sort" : [
9.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : { },
"sort" : [
10.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : { },
"sort" : [
11.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "12",
"_score" : null,
"_source" : { },
"sort" : [
12.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "13",
"_score" : null,
"_source" : { },
"sort" : [
13.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "14",
"_score" : null,
"_source" : { },
"sort" : [
14.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "15",
"_score" : null,
"_source" : { },
"sort" : [
15.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "16",
"_score" : null,
"_source" : { },
"sort" : [
16.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "17",
"_score" : null,
"_source" : { },
"sort" : [
17.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "18",
"_score" : null,
"_source" : { },
"sort" : [
18.0
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "19",
"_score" : null,
"_source" : { },
"sort" : [
19.0
]
}
]
}
}
搞定!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43613793/article/details/121735292
内容来源于网络,如有侵权,请联系作者删除!