概述

在使用 ES 的时,我们不需要事先定义好映射设置就可以直接向索引中导入文档。ES 可以自动实现每个字段的类型检测,并进行 mapping 设置,这个过程就叫动态映射(dynamic mapping)。

动态映射可以通过以下设置来关闭。

PUT /_settings
{
"index.mapper.dynamic":false
}

动态映射的规则也可以自定义,有以下几种我们可以自定义规则的应用场景:

  1. 默认映射(_default_ mapping)
  2. 字段动态映射(dynamic field mapping)
  3. 动态模板(dynamic template)
  4. 索引模板(index template)

其中,前 3 个条件中都是针对特定 index 下的 type 进行设置,而条件 4 是针对所有满足条件的 index 进行设置。

默认映射

默认映射通过把 mapping type 设置为 _default_ 来定义。

默认映射将会应用到该 index 下的任意新增 type 中。

默认映射可以在添加 index 时候设置,也可以创建 index 之后再通过 PUT mapping 接口进行设置。

PUT my_index
{
"mappings": {
"_default_": {
"_all": {
"enabled": false //默认映射禁用掉所有新增 type 的 _all 元数据字段
}
},
"user": {},
"blogpost": {
"_all": {
"enabled": true //覆盖 _default_ 的设置,启用 _all 字段
}
}
}
}

字段动态映射

默认情况,发现新的字段,ES 自动检测其 datatype 并将其加入到 mapping type 中。

通过一些设置,我们可以控制字段动态映射的方式,包括:日期类型检测、数值类型检测、自定义日期类型的格式等。

PUT my_index         //禁用日期类型检测
{
"mappings": {
"my_type": {
"date_detection": false
}
}
}
PUT my_index //自定义日期类型的格式
{
"mappings": {
"my_type": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
}
PUT my_index //启用数值类型检测
{
"mappings": {
"my_type": {
"numeric_detection": true
}
}
}

动态模板

动态模板将会根据条件判断,应用到满足条件的新增字段上去。

应用条件包括:

  1. 用 match_mapping_type 来检测新增字段的数据类型是否满足某种条件
  2. 用 match、unmatch 和 match_pattern 来判断新增字段的字段名是否满足某种条件
  3. 用 path_match 和 path_unmatch 来判断新增字段的完整路径是否匹配某条件

动态模板以数组的形式给出,数组的每一个元素就是一个模板。每个模板都有各自的应用条件,一旦新增的字段满足某个模板,模板内容就会应用到该字段上。

有两个特殊的变量,在模板中可以运用:{name}、{dynamic_type}。前者表示原字段的字段名,后者标识原字段被 ES 自动识别出来的数据类型。

"dynamic_templates": [                 //数组,每个元素都是一个动态模板
{
"my_template_name": { //动态模板名称
... match conditions ... //应用条件判断
"mapping": { ... } //映射设置
}
},
... //多个数组元素标识多个动态模板
]
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"named_analyzers": {
"match_mapping_type": "string",
"match": "*",
"mapping": {
"type": "string",
"analyzer": "{name}"
}
}
},
{
"no_doc_values": {
"match_mapping_type":"*",
"mapping": {
"type": "{dynamic_type}",
"doc_values": false
}
}
}
]
}
}
}

动态模板示例

索引模板

索引模板根据条件来判断新建的索引(只应用到新建索引上)是否满足某条件,并对其进行映射设置。

索引模板包含一些对索引的设置和映射设置。

在索引模板中有一个特殊变量可以运用:{index}。表示匹配上条件的原索引名称。

PUT /_template/template_1
{
"template": "te*", //判断条件,判断哪些索引将应用该模板
"settings": { //索引设置
"number_of_shards":
},
"mappings": { //映射设置
"type1": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "string",
"index": "not_analyzed"
},
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z YYYY"
}
}
}
}
}

mapping 详解5(dynamic mapping)的更多相关文章

  1. mapping 详解1(mapping type)

    映射(mapping) 映射是定义一个文档以及其所包含的字段如何被存储和索引的方法. 例如,用映射来定义以下内容: 哪些 string 类型的 field 应当被当成当成 full-text 字段 哪 ...

  2. mapping 详解4(mapping setting)

    mapping type 映射设置一般发生在: 1. 增加新的 index 的时候,添加 mapping type,对 fields 的映射进行设置 PUT twitter { "mappi ...

  3. mapping 详解2(field datatypes)

    基本类型 1. 字符串 字符串类型被分为两种情况:full-text 和 keywords. full-text 表示字段内容会被分析,而 keywords 表示字段值只能作为一个精确值查询. 参数: ...

  4. ElasticSearch7.3 学习之Mapping核心数据类型及dynamic mapping

    1.mapping的核心数据类型以及dynamic mapping 1.1 核心的数据类型 string :text and keyword,byte,short,integer,long,float ...

  5. mapping 详解3(Meta-Fields)

    文档标识相关元数据字段 _index 当执行多索引查询时,可能需要添加特定的一些与文档有关联的索引的子句. _index 字段可以用在 term.terms 查询,聚合(aggregations)操作 ...

  6. Elasticsearch5.X Mapping详解

    0.引言 在关系型数据库如Mysql中,设计库表需要注意的是: 1)需要几个表: 2)每个表有哪些字段: 3)表的主键及外键的设定——便于有效关联. 表的设计遵守范式约束,考虑表的可扩展性,避免开发后 ...

  7. Pytest 系列(29)- 详解 allure.dynamic 动态生成功能

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 @allure.title  ...

  8. 65.dynamic mapping

    主要知识点: 理解dynamic mapping 定制dynamic mapping 更改default dynamic mapping     一.理解dynamic mapping 1.基本概念 ...

  9. ES 12 - 配置使用Elasticsearch的动态映射 (dynamic mapping)

    目录 1 动态映射(dynamic mapping) 1.1 什么是动态映射 1.2 体验动态映射 1.3 搜索结果不一致的原因分析 2 开启dynamic mapping策略 2.1 约束策略 2. ...

随机推荐

  1. linu、C语言、计算机基础教程

    Linux操作系统入门教程:http://see.xidian.edu.cn/cpp/linux/ 鸟哥的linux私房菜:http://vbird.dic.ksu.edu.tw/ 计算机操作系统教程 ...

  2. 【原】Storm 消息处理保障机制

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  3. wuzhicms页面报错 Notice 错误,如何关闭错误显示!

    错误类型类似: PHP Notice: Use of undefined constant E_DEPRECATED - assumed 'E_DEPRECATED' in D:\freehost\3 ...

  4. ZOJ-3593 One Person Game 概率DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 带环的概率DP一般的做法是求出转移方程,然后高斯消元解方程 ...

  5. Django中的Model(字段)

    Model Django中的model是用来操作数据库的,Model是一个ORM框架,我们只需要关心model的操作,而不需要关心到底是哪一种数据库. 一.基本知识: 数据库引擎: Django中自带 ...

  6. 转载 SharePoint Foundation和SharePoint Server的区别

    SharePoint Server 2010用来取代MOSS 2007,它有标准版和企业版两个版本,使用SQL Server数据库: 早期版本中的STS或WSS在2010中更名为SharePoint ...

  7. ASP.NET- 无刷新上传使用jQuery插件之ajaxFileUpload

    灰常好,我已经使用过里面的代码了,可以用,原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFil ...

  8. js-弹出一个新窗口 新窗口自动转接到一个页面然后自动关闭

    这个问题.好. 在百度问问找到的:他的做法是打开一个后通过实例对象,再进行一将跳转,最后再将JS里定时将实例对象关闭... 这个问题其实不需要两个页面,只要三行JS代码就能实现,除非你在2.php里面 ...

  9. [置顶] MapReduce 编程之 倒排索引

    本文调试环境: ubuntu 10.04 , hadoop-1.0.2 hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktra ...

  10. List集合

    集合类方便操作,增删查找容易.集合的超级接口collection:1.List: 1.ArrayList:是存在一个数组(Object[]),添加,删除元素很慢,查找很快,元素在内存中是有序的. 2. ...