65.dynamic mapping

主要知识点:

  • 理解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的处理不同分为三种策略

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 }

}

}

}

原文地址:https://www.cnblogs.com/liuqianli/p/8476886.html

时间: 2024-07-30 18:05:59

65.dynamic mapping的相关文章

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

目录 1 动态映射(dynamic mapping) 1.1 什么是动态映射 1.2 体验动态映射 1.3 搜索结果不一致的原因分析 2 开启dynamic mapping策略 2.1 约束策略 2.2 策略示例 3 定制dynamic mapping策略 3.1 date_detection - 日期识别策略 3.2 在type中自定义动态映射模板 3.3 [过期]在index中自定义默认映射模板 1 动态映射(dynamic mapping) 1.1 什么是动态映射 动态映射时Elastic

Elasticsearch - 自动检测及动态映射Dynamic Mapping

一.自动映射: ES通过查看定义某文档的json格式就能猜测到文档结构,我们称之为自动映射,在开发过程中需要注意这些特性. 字段自动检测 在某个字段第一次出现时,如果之前没有定义过映射,ES会自动检测它可能满足的类型,然后创建对应的映射. JSON数据 ES中的数据类型 null 不会添加字段 true or false boolean floating point number double integer long object object array 依赖于第一个非null得值 stri

Elasticsearch:Dynamic mapping

Elasticsearch最重要的功能之一是它试图摆脱你的方式,让你尽快开始探索你的数据. 要索引文档,您不必首先创建索引,定义映射类型和定义字段 - 您只需索引文档,那么index,type和field将自动生效.比如: PUT data/_doc/1 { "count": 5 } 上面的命令将自动帮我们生成一个叫做data的index,并同时生成一个叫做_doc的type及一个叫做count的field.count的数据类型是long.这个非常方便,我们不想传统的RDMS那样,先要

映射(mapping)

就像是在 Data in, data out中解释过的,index中的每个document都有type.每个type都有自己的mapping或者schema definition.在type中mapping定义filed,定义每个filed中的数据类型,定义ES怎么处理这个filed,mapping也用于配置与该类型相关联的元数据. 我们会在 Types and Mappings中详细的讨论mapping,在这个章节,我们就是能让你足够开始就行了. core simple field types

38.mapping小例子

主要知识点 初步了解mapping 一,准备数据 插入几条数据,让es自动为我们建立一个索引 PUT /website/article/1 { "post_date": "2017-01-01", "title": "my first article", "content": "this is my first article in this website", "author

es中对mapping的理解

(1)往es里面直接插入数据,es会自动建立索引,同时建立type以及对应的mapping (2)mapping中就自动定义了每个field的数据类型 (3)不同的数据类型(比如说text和date),可能有的是exact value,有的是full text (4)exact value,在建立倒排索引的时候,分词的时候,是将整个值一起作为一个关键词建立到倒排索引中去:full text,会经历各种各样的处理,分词,normaliztion(时态转换,同义词转换,大小写转换),才会建立到倒排索

Elasticsearch Reference【6.1】Mapping

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define: which string fields should be treated as full text fields. which fields contain numbers, dates, or geolocatio

elasticsearch篇之mapping

2018年05月17日 18:01:37 lyzkks 阅读数:444更多 个人分类: Elastic stack 版权声明:文章内容来自于网络和博主自身学习体会,转载请注明出处,欢迎留言大家一起讨论学习~~ https://blog.csdn.net/sinat_35930259/article/details/80354732 什么是mapping mapping是类似于数据库中的表结构定义,主要作用如下: 定义index下的字段名 定义字段类型,比如数值型.浮点型.布尔型等 定义倒排索引相

【ElasticSearch】:Mapping相关

Mapping 类似数据库中的表结构定义,主要作用如下: 定义Index下的字段名(Field Name). 定义字段类型,例如数值型.字符串型.布尔型等. 定义倒排索引相关配置,比如是否索引.记录position等. 查看一个Index的Mapping的API 例如查看index为test_index的mapping,使用_mapping,查询结果中: doc是这个index的type,忽略,ES6.X将废弃. 关注properties中的内容,每个字段及其字段类型(type) . 自定义一个