公号:码农充电站pro
主页:https://codeshellme.github.iohtml
一般在使用 ES 构建数据模型时,须要考虑如下几点:git
对于不一样类型的数据,主要考虑下面几点:github
true
。对于搜索需求,主要考虑如下几点:shell
对于聚合与排序,主要考虑如下几点:网络
false
。false
。true
(能够达到利用缓冲的目的,提升性能)。将 store 设置为 true
(默认为 false
),能够存储字段的原始内容;通常在 _source 的 enabled 为 false
时使用。app
若是须要对一些图书信息进行建模,需求以下:elasticsearch
示例数据以下:ide
{ "title":"Mastering ElasticSearch 5.0", "description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users’ search experience with Elasticsearch’s functionalities and develop your own Elasticsearch plugins", "author":"Bharvi Dixit", "public_date":"2017", "cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg" }
若是不手动设置 mapping,那么每一个字段将被 ES 设置为以下类型:性能
{ "type" : "text", # text 类型 "fields" : { # 并添加一个 keyword 子字段 "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }
下面根据需求,手动设置 mapping:优化
PUT books { "mappings": { "properties": { "author": { "type": "keyword" }, "cover_url": { "type": "keyword", "index": false # 不须要支持搜索 }, "description": { "type": "text" }, "public_date": { "type": "date" }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 100 } } } } } }
若是如今须要添加一个字段 content
,用于存储图书的内容,所以该字段的信息量将很是大,这将致使 _source 的内容过大,致使过大的网络开销。
为了优化,能够将 _source 的 enabled 设置为 false
,而后将每一个字段的 store 设置为 true
(打开额外存储)。
以下:
PUT books { "mappings": { "_source": { "enabled": false # enabled 为 false }, "properties": { "author": { "type": "keyword", "store": true # store 为 true }, "cover_url": { "type": "keyword", "index": false, "store": true # store 为 true }, "description": { "type": "text", "store": true # store 为 true }, "content": { "type": "text", "store": true # store 为 true }, "public_date": { "type": "date", "store": true # store 为 true }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 100 } }, "store": true # store 为 true } } } }
将 _source 禁止掉以后,查询的结果中就没有了 _source 字段;若是须要哪些字段的内容,则须要设置 stored_fields,以下:
POST books/_search { "stored_fields": ["title","author","public_date"], "query": { "match": { "content": "searching" } } }
(本节完。)
推荐阅读:
欢迎关注做者公众号,获取更多技术干货。