##索引數據,dynamic mapping 會不斷加入新增字段 PUT cookie_service/_doc/1 { "url":"www.google.com", "cookies":{ "username":"tom", "age":32 } } PUT cookie_service/_doc/2 { "url":"www.amazon.com", "cookies":{ "login":"2019-01-01", "email":"xyz@abc.com" } }
咱們的cookie字段使用了Dynamic=true
默認值,因此隨着寫入的數據愈來愈多,若是不對cookies字段的子字段進行限制的話,字段數會愈來愈多,會影響性能,cookie
#使用 Nested 對象,增長key/value PUT cookie_service { "mappings": { "dynamic": "strict", "properties": { "cookies": { "type": "nested", "properties": { "name": { "type": "keyword" }, "dateValue": { "type": "date" }, "keywordValue": { "type": "keyword" }, "IntValue": { "type": "integer" } } }, "url": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
須要說明幾點的是:app
"dynamic": "strict"
阻止其餘字段加入正則查詢的性能不夠好,前綴查詢屬於Term查詢性能
文檔中某個字段包含了Elasticsearch的版本信息,例如version: "7.1.0",如今咱們須要查詢朱版本是7,次要版本是2的文檔,不要使用正則查詢google
PUT softwares/ { "mappings": { "_meta": { "software_version_mapping": "1.1" }, "properties": { "version": { "properties": { "display_name": { "type": "keyword" }, "hot_fix": { "type": "byte" }, "marjor": { "type": "byte" }, "minor": { "type": "byte" } } } } } }
而後咱們再使用查詢url
POST softwares/_search { "query": { "bool": { "filter": [ { "match":{ "version.marjor":7 } }, { "match":{ "version.minor":2 } } ] } } }
PUT ratings/_doc/1 { "rating":5 } PUT ratings/_doc/2 { "rating":null } POST ratings/_search { "size": 0, "aggs": { "avg": { "avg": { "field": "rating" } } } } # 查詢結果 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "avg" : { "value" : 5.0 } } }
很明顯,咱們查到了兩條數據,可是平均值是5,這個很難以理解,spa
PUT ratings { "mappings": { "properties": { "rating": { "type": "float", "null_value": 0 } } } }
再次插入上面的數據,咱們獲得下面的結果設計
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "avg" : { "value" : 2.5 } } }
這樣就比較對了,固然null_value
的值是能夠本身根據業務需求本身設定的3d