1、DocValues說明
DocValues其實是Lucene在構建倒排索引時,會額外建立一個有序的正排索引(基於document=>field value的映射列表)。年齡、日期等非字符型的可以排序,就是因為建立了倒排索引,也建立了正排索引。
DocValues說白了就是正排索引,默認對字符串類型的不起作用,即:默認對不分詞的字段是開啟的,對分詞字段無效(需要把fielddata設置為true才可以)。
DocValues存儲在磁盤上,節省內存,對排序,分組和一些聚合操作能夠大大提升性能。
DocValues映射表如下:json文檔字符串在磁盤上對應的是文檔doc1和文檔doc2,每一個字段都會列出
{"birthday":"1985-11-11",age:23}
{"birthday":"1989-11-11",age:29}
-----------------------------------------------------------
document age birthday
doc1 23 1985-11-11
doc2 29 1989-11-11
2、演示關閉age字段DocValues,執行排序搜索會報錯
准備數據:
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" }
現在沒有關閉age字段DocValues,執行排序搜索正常
GET lib/user/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] }
查詢結果:
{ "took": 9, "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 ] } ] } }
現在重新建立索引,並添加上面的數據、執行排序搜索報錯
PUT /lib { "settings":{ "number_of_shards":3, "number_of_replicas":0 }, "mappings":{ "user":{ "properties":{ "name":{"type":"text"}, "address":{"type":"text"}, "age":{ "type":"integer", "doc_values":false }, "interests":{"type":"text"}, "birthday":{"type":"date"} } } } }
執行查詢
GET lib/user/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] }
查詢結果,報錯
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values 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": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values instead." } } ] }, "status": 400 }
注意:
假如把age的doc values關閉,執行排序搜索會報錯