主要知识点:
- 理解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:
The rules governing dynamic field detection.
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": "strict", # 把整个type指定为strict
"dynamic": "true" # 因为把整个type指定为strict,这个把address指定为true
"content": "this is my article",
执行结果如下,发现address并不会出错,但是content出错。
"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"
语法:GET /my_index/_mapping/my_type
PUT /my_index/_mapping/my_type
2、定制dynamic mapping template(type level)
"match_mapping_type": "string",
"title": "this is my first article"
"title_en": "this is my first article"
结果:title没有匹配到任何的dynamic模板,默认就是standard分词器,不会过滤停用词,is会进入倒排索引,用is来搜索是可以搜索到的
title_en匹配到了dynamic模板,就是english分词器,会过滤停用词,is这种停用词就会被过滤掉,用is来搜索就搜索不到了