1、准備數據
PUT /lib { "settings":{ "number_of_shards":3, "number_of_replicas":0 }, "mappings":{ "user":{ "properties":{ "name":{"type":"text"}, "address":{"type":"text"}, "age":{"type":"integer"}, "interests":{"type":"text"}, "birthday":{"type":"date"} } } } }
put /lib/user/1 { "name":"zhaoliu", "address":"hei long jiang sheng tie ling shi", "age":50, "birthday":"1970-12-12", "interests":"xi huang hejiu,duanlian,lvyou" } put /lib/user/2 { "name":"zhaoming", "address":"bei jing hai dian qu qing he zhen", "age":20, "birthday":"1998-10-12", "interests":"xi huan hejiu,duanlian,changge" } put /lib/user/3 { "name":"lisi", "address":"bei jing hai dian qu qing he zhen", "age":23, "birthday":"1998-10-12", "interests":"xi huan hejiu,duanlian,changge" } put /lib/user/4 { "name":"wangwu", "address":"bei jing hai dian qu qing he zhen", "age":26, "birthday":"1998-10-12", "interests":"xi huan biancheng,tingyinyue,lvyou" } put /lib/user/5 { "name":"zhangsan", "address":"bei jing chao yang qu", "age":29, "birthday":"1988-10-12", "interests":"xi huan tingyinyue,changge,tiaowu" }
2、操作演示
1)按照年齡降序排序
GET lib/user/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] }
查詢結果
{ "took": 42, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 5, "max_score": null, "hits": [ { "_index": "lib", "_type": "user", "_id": "1", "_score": null, "_source": { "name": "zhaoliu", "address": "hei long jiang sheng tie ling shi", "age": 50, "birthday": "1970-12-12", "interests": "xi huang hejiu,duanlian,lvyou" }, "sort": [ 50 ] }, { "_index": "lib", "_type": "user", "_id": "5", "_score": null, "_source": { "name": "zhangsan", "address": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,tiaowu" }, "sort": [ 29 ] }, { "_index": "lib", "_type": "user", "_id": "4", "_score": null, "_source": { "name": "wangwu", "address": "bei jing hai dian qu qing he zhen", "age": 26, "birthday": "1998-10-12", "interests": "xi huan biancheng,tingyinyue,lvyou" }, "sort": [ 26 ] }, { "_index": "lib", "_type": "user", "_id": "3", "_score": null, "_source": { "name": "lisi", "address": "bei jing hai dian qu qing he zhen", "age": 23, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "sort": [ 23 ] }, { "_index": "lib", "_type": "user", "_id": "2", "_score": null, "_source": { "name": "zhaoming", "address": "bei jing hai dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "sort": [ 20 ] } ] } }
因為age是數值類型的,所以可以排序,默認字符串類型的不能排序,因為字符串類型的已經分詞了。如下根據interests排序會報錯:
GET lib/user/_search { "query": { "match_all": {} }, "sort": [ { "interests": { "order": "desc" } } ] }
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] 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": "lib", "node": "AJ3x6yc8TfKj6_zx6VRm0g", "reason": { "type": "illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] 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 }
3、如何對字符串類型的排序
對一個字符串類型的字段進行排序通常不准確,因為已經被分詞成多個詞條了。
解決方式:對字段索引兩次,一次索引分詞(用於搜索),一次索引不分詞(用於排序)
1)刪掉上面的lib索引,重新創建,修改 interests 字段的創建方式,並重新添加如上數據
PUT /lib { "settings":{ "number_of_shards":3, "number_of_replicas":0 }, "mappings":{ "user":{ "properties":{ "name":{"type":"text"}, "address":{"type":"text"}, "age":{"type":"integer"}, "interests":{ "type":"text", "fields": { "raw":{ "type": "keyword" } }, "fielddata": true }, "birthday":{"type":"date"} } } } }
字符串有兩種類型,一種是text,默認分詞,一種是keyword不分詞,interests創建兩種類型的索引,一種text(倒排索引),一個keyword(正排索引)。
重新執行操作,返回正確結果:
GET lib/user/_search { "query": { "match_all": {} }, "sort": [ { "interests.raw": { "order": "desc" } } ] }
{ "took": 6, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 5, "max_score": null, "hits": [ { "_index": "lib", "_type": "user", "_id": "1", "_score": null, "_source": { "name": "zhaoliu", "address": "hei long jiang sheng tie ling shi", "age": 50, "birthday": "1970-12-12", "interests": "xi huang hejiu,duanlian,lvyou" }, "sort": [ "xi huang hejiu,duanlian,lvyou" ] }, { "_index": "lib", "_type": "user", "_id": "5", "_score": null, "_source": { "name": "zhangsan", "address": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,tiaowu" }, "sort": [ "xi huan tingyinyue,changge,tiaowu" ] }, { "_index": "lib", "_type": "user", "_id": "2", "_score": null, "_source": { "name": "zhaoming", "address": "bei jing hai dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "sort": [ "xi huan hejiu,duanlian,changge" ] }, { "_index": "lib", "_type": "user", "_id": "3", "_score": null, "_source": { "name": "lisi", "address": "bei jing hai dian qu qing he zhen", "age": 23, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "sort": [ "xi huan hejiu,duanlian,changge" ] }, { "_index": "lib", "_type": "user", "_id": "4", "_score": null, "_source": { "name": "wangwu", "address": "bei jing hai dian qu qing he zhen", "age": 26, "birthday": "1998-10-12", "interests": "xi huan biancheng,tingyinyue,lvyou" }, "sort": [ "xi huan biancheng,tingyinyue,lvyou" ] } ] } }