20.1 按照文档添加顺序排序
GET website/_search
GET website/_search
{
"query": {
"match_all": {}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
20.2 按照文档相关度评分排序
GET website/_search
{
"query": {
"term": {
"title": {
"value": "centos"
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
查询结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": 0.9227539,
"_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" } },
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": 0.2876821,
"_source": { "title": "CentOS升级gcc", "author": "程裕强", "postdate": "2016-12-25", "abstract": "CentOS升级gcc", "url": "http://url/53868915" } }
]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
20.3 指定字段排序
【例子】按照postdate日期降序排序
GET website/_search
{
"query": {
"match_all": {}
},
"sort": [
{"postdate":{"order":"desc"}}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
{
"took": 51,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": null,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "8",
"_score": null,
"_source": { "title": "es高亮", "author": "程裕强", "postdate": "2017-01-03", "abstract": "Elasticsearch查询关键字高亮", "url": "http://url/53991802" },
"sort": [ 1483401600000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "5",
"_score": null,
"_source": { "title": "libstdc++.so.6", "author": "程裕强", "postdate": "2016-12-30", "abstract": "libstdc++.so.6问题解决", "url": "http://url/53946911" },
"sort": [ 1483056000000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": null,
"_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" },
"sort": [ 1483056000000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "7",
"_score": null,
"_source": { "title": "搭建Ember开发环境", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS系统下搭建Ember开发环境", "url": "http://url/53947507" },
"sort": [ 1483056000000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "4",
"_score": null,
"_source": { "title": "vmware复制虚拟机", "author": "程裕强", "postdate": "2016-12-29", "abstract": "vmware复制虚拟机", "url": "http://url/53946664" },
"sort": [ 1482969600000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": null,
"_source": { "title": "CentOS升级gcc", "author": "程裕强", "postdate": "2016-12-25", "abstract": "CentOS升级gcc", "url": "http://url/53868915" },
"sort": [ 1482624000000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "2",
"_score": null,
"_source": { "title": "watchman源码编译", "author": "程裕强", "postdate": "2016-12-23", "abstract": "CentOS7.x的watchman源码编译", "url": "http://url.cn/53844169" },
"sort": [ 1482451200000 ] },
{
"_index": "website",
"_type": "blog",
"_id": "1",
"_score": null,
"_source": { "title": "Ambari源码编译", "author": "程裕强", "postdate": "2016-12-21", "abstract": "CentOS7.x下的Ambari2.4源码编译", "url": "http://url.cn/53788351" },
"sort": [ 1482278400000 ] }
]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
20.4 多字段排序
【例子】salary降序,age升序
(1)准备数据
PUT my-index
PUT my-index/persion/1
{
"name":"张三",
"age":20,
"salary":6000
}
PUT my-index/persion/2
{
"name":"李四",
"age":20,
"salary":8000
}
PUT my-index/persion/3
{
"name":"王五",
"age":21,
"salary":6000
}
PUT my-index/persion/4
{
"name":"刘六",
"age":21,
"salary":8000
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
(2)salary降序,age升序
GET my-index/_search
{
"sort": [
{"salary":{"order":"desc"}},
{"age":{"order":"asc"}}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "my-index",
"_type": "persion",
"_id": "2",
"_score": null,
"_source": { "name": "李四", "age": 20, "salary": 8000 },
"sort": [ 8000, 20 ] },
{
"_index": "my-index",
"_type": "persion",
"_id": "4",
"_score": null,
"_source": { "name": "刘六", "age": 21, "salary": 8000 },
"sort": [ 8000, 21 ] },
{
"_index": "my-index",
"_type": "persion",
"_id": "1",
"_score": null,
"_source": { "name": "张三", "age": 20, "salary": 6000 },
"sort": [ 6000, 20 ] },
{
"_index": "my-index",
"_type": "persion",
"_id": "3",
"_score": null,
"_source": { "name": "王五", "age": 21, "salary": 6000 },
"sort": [ 6000, 21 ] }
]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76