Mapping是用来定义doc及其fields(域、字段)如何被存储及索引的过程。Mapping可被用来定义: 1 哪些字符串fields需要被检索 2 哪些字段是数字,日期,地理位置 3 日期的格式 4 动态添加字段的映射规则
包括元数据(metadata fields)和fields: 元数据字段用于定义如何处理文档关联元数据,如_id、_source,_index。fields部分是文档包含的字段或属性,每个字段都有自己的类型。
Dynamic Mapping: 正是有动态映射,所以字段和映射类型使用之前不需要定义,我们可以直接创建索引,ES会自动添加新的字段及猜测并设置字段类型。
Explicit Mappings: 字段及字段类型和数据库表非常类似,只有业务开发人员更了解字段及其类型,so,声明式的mapping是必要的。
可以在创建索引时指定字段类型:
PUT teacher { "mappings": { "properties": { "name":{ "type": "text" }, "email":{ "type": "keyword" }, "age":{ "type": "integer" }, "entrytime":{ "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } }, "date_detection": true } }新增field到已存在的mapping
PUT teacher/_mapping { "properties":{ "t_id":{ "type":"keyword", "index":false } } } PUT teacher/_mapping { "properties":{ "desc":{ "type":"text", "analyzer":"ik_smart" } } }查看索引的mapping
GET teacher/_mapping