Elasticsearch使用建议 优化索引配置 区分text和keyword 在Elasticsearch中string字段被拆分成两种新的数据类型:text用于全文搜索的,而keyword用于关键词搜索。 对于不需要分词的字符串精确值字段,如标签或枚举,建议配置为keyword类型。 7.x之前版本 PUT myindex1 { "mappings": { "mytype": { "properties": { "tags": { "type": "keyword" }, "fullname": { "type": "text" } } } } } 7.x之后版本 PUT myindex1 { "mappings": { "properties": { "tags": { "type": "keyword" }, "fullname": { "type": "text" } } } } 基于text字段的聚合统计 分词字段的聚合统计不是一种常见的需求。在Elasticsearch对于分词字段的聚合统计需要用到fielddata,默认是禁用的,开启fielddata会带来较大的内存负担。 建议的做法是分词字符串进行多字段映射,映射为一个text字段用于全文检索,和一个keyword字段用于聚合统计。 7.x之前版本 PUT myindex2 { "mappings": { "mytype": { "properties": { "fullname": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } } } 7.x之后版本 PUT myindex2 { "mappings": { "properties": { "fullname": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } }