Elasticsearch的dynamic策略

本文探讨了Elasticsearch中dynamic策略的应用,包括strict、true和false的不同行为,以及如何通过dynamic_templates和default_mapping_template定制映射策略,确保数据的一致性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、定制dynamic策略

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

PUT /my_index
{
  "mappings": {
    "my_type": {
      "dynamic": "strict",            //设置了strict,添加字段报错
      "properties": {
        "title": {
          "type": "text"
        },
        "address": {
          "type": "object",
          "dynamic": "true"           //设置为true可以添加字段
        }
      }
    }
  }
}

PUT /my_index/my_type/1
{
  "title": "my article",
  "content": "this is my article",        //添加新的字段
  "address": {
    "province": "guangdong",
    "city": "guangzhou"
  }
}

//报错
{
  "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
}


PUT /my_index/my_type/1
{
  "title": "my article",
  "address": {
    "province": "guangdong",
    "city": "guangzhou"        //添加新的字段
  }
}

//不报错
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"
          }
        }
      }
    }
  }
}

2、定制dynamic mapping策略

(1)date_detection

默认会按照一定格式识别date,比如yyyy-MM-dd。但是如果某个field先过来一个2017-01-01的值,就会被自动dynamic mapping成date,后面如果再来一个"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  }
        }
    }
}

 

### 动态映射配置与使用 动态映射允许 Elasticsearch 自动推断新字段的数据类型并创建索引模式。当文档被索引入不存在的字段时,Elasticsearch 将自动检测该字段的内容,并为其分配适当的数据类型。 #### 禁用动态映射 有时可能不希望启用此功能来防止意外添加未定义结构的新数据到现有索引中。可以通过设置 `dynamic` 参数为 `false` 来实现这一点: ```json PUT /my-index-000001 { "mappings": { "_doc": { "dynamic": false, "properties": {} } } } ``` 这会阻止任何未知字段进入 `_doc` 类型下的索引[^1]。 #### 设置自定义默认行为 如果既不想完全禁用也不总是依赖于默认机制,则可以指定更精细控制的方式处理新的字段名。例如,通过设定 `"runtime"` 或者特定类型的模板来自定义响应策略: ```json PUT /my-index-000002 { "mappings": { "_doc": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ], "dynamic": "strict", "properties": { } } } } ``` 上述例子展示了如何强制字符串作为关键字存储而不是文本形式;同时将整体动态属性设为了严格模式,意味着只有匹配预定义规则集内的项目才会被接受[^2]。 #### 使用场景说明 对于那些需要灵活应对不断变化的数据源的应用程序来说,动态映射是一个非常有用的特性。然而,在生产环境中通常建议明确定义好所有的映射关系以提高性能和可维护性。此外,合理利用动态模板可以帮助简化复杂对象模型的设计过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值