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策略

   

1date_detection

   

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

   

PUT /my_index/_mapping/my_type

{

"date_detection": false

}

   

2、定制dynamic mapping templatetype 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 templateindex level

PUT /my_index

{

"mappings": {

"_default_": {

"_all": { "enabled": false }

},

"blog": {

"_all": { "enabled": true }

}

}

}

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