主要知识点:

  • 理解dynamic mapping
  • 定制dynamic mapping
  • 更改default dynamic mapping

 
 

一、理解dynamic mapping

1、基本概念

One of the most important features of Elasticsearch is that it tries to get out of your way and let you start exploring your data as quickly as possible. To index a document, you don't have to first create an index, define a mapping type, and define your fields--you can just index a document and the index, type, and fields will spring to life automatically:

PUT data/_doc/1

{ "count": 5 }

  

Creates the data index, the _doc mapping type, and a field called count with datatype long.

The automatic detection and addition of new fields is called dynamic mapping.(这种自己检测和新增fields被称为动态映射) The dynamic mapping rules can be customised to suit your purposes with:

Dynamic field mappings

The rules governing dynamic field detection.

Dynamic templates

Custom rules to configure the mapping for dynamically added fields.

 
 

Index templates allow you to configure the default mappings, settings and aliases for new indices, whether created automatically or explicitly.

总结如下:dynamic mapping就是es为各个document的fields自动映射到不同的类型,这种动态映射可以由es自动完成,所以我们可以在没有建立index时就插入数据,也程序员指定,程序员指定时是在新创建index时指定,指定方式中按es对新的fields的处理不同分为三种策略

  • true:遇到陌生字段,就进行dynamic mapping
  • false:遇到陌生字段,就忽略
  • strict:遇到陌生字段,就报错

 
 

2、示例一,

PUT /my_index

{

"mappings": {

"my_type": {

"dynamic": "strict", # 把整个type指定为strict

"properties": {

"title": {

"type": "text"

},

"address": {

"type": "object",

"dynamic": "true" # 因为把整个type指定为strict,这个把address指定为true

}

}

}

}

}

 
 

插入数据,测试新建index

PUT /my_index/my_type/1

{

"title": "my article",

"content": "this is my article",

"address": {

"province": "guangdong",

"city": "guangzhou"

}

}

执行结果如下,发现address并不会出错,但是content出错。

{

"error": {

"root_cause": [

{

"type": "strict_dynamic_mapping_exception",

"reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"

}

],

"type": "strict_dynamic_mapping_exception",

"reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"

},

"status": 400

}

 
 

再次按原新建index的要求插入数据,发现不会出错。

PUT /my_index/my_type/1

{

"title": "my article",

"address": {

"province": "guangdong",

"city": "guangzhou"

}

}

 
 

3、查看_mapping的结果

语法:GET /my_index/_mapping/my_type

执行结果如下:

{

"my_index": {

"mappings": {

"my_type": {

"dynamic": "strict",

"properties": {

"address": {

"dynamic": "true",

"properties": {

"city": {

"type": "text",

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

}

},

"province": {

"type": "text",

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

}

}

}

},

"title": {

"type": "text"

}

}

}

}

}

}

 
 

二、定制dynamic mapping策略

 
 

1、date_detection

 
 

默认会按照一定格式识别date,比如yyyy-MM-dd。但是如果某个field定义了一个2017-01-01的值,就会被自动dynamic mapping成date,后面这个field值如果是"hello world"之类的值,就会报错。可以手动关闭某个type的date_detection,如果有需要,自己手动指定某个field为date类型。

 
 

PUT /my_index/_mapping/my_type

{

"date_detection": false

}

 
 

2、定制dynamic mapping template(type level)

 
 

PUT /my_index

{

"mappings": {

"my_type": {

"dynamic_templates": [

{ "en": {

"match": "*_en",

"match_mapping_type": "string",

"mapping": {

"type": "string",

"analyzer": "english"

}

}}

]

}}}

 
 

插入两条数据

PUT /my_index/my_type/1

{

"title": "this is my first article"

}

 
 

PUT /my_index/my_type/2

{

"title_en": "this is my first article"

}

 
 

结果:title没有匹配到任何的dynamic模板,默认就是standard分词器,不会过滤停用词,is会进入倒排索引,用is来搜索是可以搜索到的

title_en匹配到了dynamic模板,就是english分词器,会过滤停用词,is这种停用词就会被过滤掉,用is来搜索就搜索不到了

 
 

3、更改default mapping template(index level)

PUT /my_index

{

"mappings": {

"_default_": {

"_all": { "enabled": false }

},

"blog": {

"_all": { "enabled": true }

}

}

}

65.dynamic mapping的更多相关文章

  1. mapping 详解5(dynamic mapping)

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

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

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

  3. Dynamic Mapping和常见字段类型

    原文:Dynamic Mapping和常见字段类型 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn. ...

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

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

  5. ElasticSearch7.3 学习之定制动态映射(dynamic mapping)

    1.dynamic mapping ElasticSearch中有一个非常重要的特性--动态映射,即索引文档前不需要创建索引.类型等信息,在索引的同时会自动完成索引.类型.映射的创建. 当ES在文档中 ...

  6. Elasticsearch:Dynamic mapping

    Elasticsearch最重要的功能之一是它试图摆脱你的方式,让你尽快开始探索你的数据. 要索引文档,您不必首先创建索引,定义映射类型和定义字段 - 您只需索引文档,那么index,type和fie ...

  7. elastic 查询案例Query与Filter + CRUD简单理解 + dynamic mapping + keyword

    1.增 PUT mytest01/external/ { "name": "xiaowei" } curl -XPUT '192.168.1.49:9200/m ...

  8. mapping 详解1(mapping type)

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

  9. elasticsearch篇之mapping

    2018年05月17日 18:01:37 lyzkks 阅读数:444更多 个人分类: Elastic stack   版权声明:文章内容来自于网络和博主自身学习体会,转载请注明出处,欢迎留言大家一起 ...

随机推荐

  1. CA certificate

    1 什么是CA certificate CA证书本质上是一把公钥. 2 为什么需要CA证书 是为了避免黑客冒充服务器,服务器通过CA证书证明自己是真的服务器,而不是黑客. 就是说,一旦客户端有了一个服 ...

  2. XPath Nodes

    教程 https://www.w3schools.com/xml/xpath_nodes.asp 节点之间的关系 Parent,Children,Siblings,Ancestors,Descenda ...

  3. java问题解读,String类为什么是final的

    一.理解final 望文生义,final意为“最终的,最后的”,我理解为“不能被改变的”,它可以修饰类.变量和方法. 所以我是否可以理解为被它所修饰的类.变量和方法都不能被改变呢?答案是”是“,因为有 ...

  4. 【BZOJ 2457】 双端队列

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2457 [算法] 贪心 [代码] #include<bits/stdc++.h& ...

  5. JDK8中函数式流编程推荐

    强烈推荐使用Java8中函数流API库来处理集合相关的数据,今天又看来一个项目代码里面用到来很多这样的处理,基本上可以解决大部分遍历问题.并且代码简洁清晰. JAVA8与lambda表达式 JDK8  ...

  6. 36.面板Ext.Panel使用

    转自:https://www.cnblogs.com/linjiqin/archive/2011/06/22/2086620.html 面板Ext.Panel使用 概要 1.Ext.Panel概述 2 ...

  7. js:正则表达

    一:正则表达对象方法 1:compile()方法   //编译正则表达式 实例:在字符串中全局搜索“man”,并用“person”替换,然后通过compile()方法,改变正则表达式,用person替 ...

  8. 基于行为树的AI 与 Behavior Designer插件

    优点:    0.行为逻辑和状态数据分离,任何节点都可以反复利用.    1.高度模块化状态,去掉状态中的跳转逻辑,使得状态变成一个"行为".    2."行为" ...

  9. [Swift通天遁地]九、拔剑吧-(2)在项目中使用大量美观的图标

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. akka设计模式系列

    由于本人爱好Scala,顺便也就爱好Akka,但目前网上对Akka的介绍大多都是概念上或技术方向上的介绍,基本没有Akka设计模式或者Actor模型设计模式的资料.这对于Akka的普及非常不利,因为即 ...