今天有小伙伴问,Elasticsearch 索引里面的mapping 设置 dynamic = true是什么意思?
一:ES mapping dynamic是做什么用的
-
Mapping中的字段类型一旦设定后,禁止直接修改。原因是:Lucene实现的倒排索引生成后不允许修改
-
只能新建一个索引,然后reindex数据
-
默认允许新增字段
通过dynamic参数来控制字段的新增:
- true:默认,允许自动新增字段,但是mapping不显示,查询返回JSON有
- false:不允许自动新增字段,但是文档可以正常写入,但无法对新增字段进行查询等操作
- strict:文档不能写入,报错
二:实例演示
1:约束策略 true
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": true, //开启 —— 遇到陌生字段时, 进行动态映射
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
put my_index/_doc/1
{
"name": "John Smith",
"first_name": "John",
"last_name": "Smith"
}
2:约束策略 false
delete my_index
{}
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": false, // 关闭 —— 忽略遇到的陌生字段
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
3:约束策略 strict
delete my_index
{}
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": "strict", // 严格控制策略
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
put my_index/_doc/1
{
"name": "John Smith",
"first_name": "John",
"last_name": "Smith"
}
小伙伴你清楚了吗?
以上案例均使用:Elasticsearch 6.5.3 版本演示。
转自:https://blog.csdn.net/yexiaomodemo/article/details/108884663