前言
我們之前學過幾種查詢方式了,但是結果順序都是elasticsearch決定的。我們來給查詢結果搞上我們定制的順序。
准備數據
PUT zhifou/doc/1
{
"name":"顧老二",
"age":30,
"from": "gu",
"desc": "皮膚黑、武器長、性格直",
"tags": ["黑", "長", "直"]
}
PUT zhifou/doc/2
{
"name":"大娘子",
"age":18,
"from":"sheng",
"desc":"膚白貌美,嬌憨可愛",
"tags":["白", "富","美"]
}
PUT zhifou/doc/3
{
"name":"龍套偏房",
"age":22,
"from":"gu",
"desc":"mmp,沒怎么看,不知道怎么形容",
"tags":["造數據", "真","難"]
}
PUT zhifou/doc/4
{
"name":"石頭",
"age":29,
"from":"gu",
"desc":"粗中有細,狐假虎威",
"tags":["粗", "大","猛"]
}
PUT zhifou/doc/5
{
"name":"魏行首",
"age":25,
"from":"廣雲台",
"desc":"仿佛兮若輕雲之蔽月,飄飄兮若流風之回雪,mmp,最后竟然沒有嫁給顧老二!",
"tags":["閉月","羞花"]
}
排序查詢:sort
降序:desc
想到排序,出現在腦海中的無非就是升(正)序和降(倒)序。比如我們查詢顧府都有哪些人,並根據age字段按照降序,並且,我只想看nmae
和age
字段:
GET zhifou/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
上例,在條件查詢的基礎上,我們又通過sort
來做排序,根據age
字段排序,是降序呢還是升序,由order
字段控制,desc
是降序。
結果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "顧老二",
"age" : 30,
"from" : "gu",
"desc" : "皮膚黑、武器長、性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"sort" : [
30
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : null,
"_source" : {
"name" : "石頭",
"age" : 29,
"from" : "gu",
"desc" : "粗中有細,狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
},
"sort" : [
29
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
},
"sort" : [
22
]
}
]
}
}
上例中,結果是以降序排列方式返回的。
升序:asc
那么想要升序怎么搞呢?
GET zhifou/doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
上例,想要以升序的方式排列,只需要將order
值換為asc
就可以了。
結果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : null,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,嬌憨可愛",
"tags" : [
"白",
"富",
"美"
]
},
"sort" : [
18
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
},
"sort" : [
22
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "魏行首",
"age" : 25,
"from" : "廣雲台",
"desc" : "仿佛兮若輕雲之蔽月,飄飄兮若流風之回雪,mmp,最后竟然沒有嫁給顧老二!",
"tags" : [
"閉月",
"羞花"
]
},
"sort" : [
25
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : null,
"_source" : {
"name" : "石頭",
"age" : 29,
"from" : "gu",
"desc" : "粗中有細,狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
},
"sort" : [
29
]
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "顧老二",
"age" : 30,
"from" : "gu",
"desc" : "皮膚黑、武器長、性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"sort" : [
30
]
}
]
}
}
上例,可以看到結果是以age
從小到大的順序返回結果。
不是什么數據類型都能排序
那么,你可能會問,除了age
,能不能以別的屬性作為排序條件啊?來試試:
GET zhifou/chengyuan/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"name": {
"order": "asc"
}
}
]
}
上例,我們以name
屬性來排序,來看結果:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "zhifou",
"node": "wrtr435jSgi7_naKq2Y_zQ",
"reason": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}
}
],
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}
}
},
"status": 400
}
結果跟我們想象的不一樣,報錯了!
注意:在排序的過程中,只能使用可排序的屬性進行排序。那么可以排序的屬性有哪些呢?
- 數字
- 日期
其他的都不行!
歡迎斧正,that's all