天天看点

Elasticsearch实战(六)-mapping映射(下)dynamic template自定义Mapping的操作步骤索引模板

日期的自动识别可以自行配置日期格式,

默认是[ “strict_date_optional_time”,“yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”]

strict_date_optional_time 是 ISO datetime 的格式,完整格式类似下面:

YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)

dynamic_date_formats可以自定义日期类型

date_detection 可关闭日期自动识别的机制

dynamic template

允许根据es自动识别的数据类型、字段名等来动态设定字段类型,可以实现如下效果:

  • 所有字符串类型都设定为keyword类型,即默认不分词
  • 所有以message开头的字段都设定为text类型,即分词
  • 所有以long_开头的字段都设定为long类型
  • 所有自动匹配为double类型的都设定为float类型,以节省空间
  • Elasticsearch实战(六)-mapping映射(下)dynamic template自定义Mapping的操作步骤索引模板

匹配规则一般有如下几个参数:

  • match_ mapping _type 匹配 es 自动识别的字段类型,如boolean,long,string
  • match,unmatch 匹配字段名
  • path_ match,path_ unmatch 匹配路径

自定义Mapping的操作步骤

  1. 写一条文档到es的临时索引中,获取es自动生成的mapping
  2. 修改步骤1得到的mapping ,自定义相关配置
  3. 使用步骤2的mapping创建实际所需索引

索引模板

索引模板,英文为Index Template,主要用于在新建索引时自动应用预先设定的配置,简化索索引创建的操作步骤

可以设定索引的配置和mapping

可以有多个模板,根据order设置,order大的覆盖小的配置

索弓|模板API , endpoint为 _template ,如下所示:

Elasticsearch实战(六)-mapping映射(下)dynamic template自定义Mapping的操作步骤索引模板

获取与删除

Elasticsearch实战(六)-mapping映射(下)dynamic template自定义Mapping的操作步骤索引模板

参考

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

继续阅读