📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

更新一个或多个索引的映射定义,添加新字段或修改现有字段配置。

API #

PUT /{index}/_mapping
POST /{index}/_mapping

API 的作用 #

该 API 用于更新索引的映射(mapping)定义,支持以下操作:

  • 添加新字段:为索引添加新的字段及其类型定义
  • 更新字段配置:修改某些字段的可更新属性
  • 合并映射:新的映射定义会与现有映射合并

映射更新的限制 #

操作是否支持说明
添加新字段可以随时添加
修改字段类型已有字段不能修改类型
修改字段名称不能重命名字段
删除字段不能删除已有字段
更新分析器有限某些分析器设置可以更新
添加别名可以添加字段别名

API 的参数 #

路由参数 #

参数类型是否必填描述
{index}字符串索引名称列表,逗号分隔。支持通配符、_all(所有索引)

Query String 参数 #

参数类型是否必填默认值描述
timeout时间值30s显式操作超时时间
master_timeout时间值30s连接到主节点的超时时间
include_type_name布尔值false是否在请求体中包含类型名称(已弃用)
ignore_unavailable布尔值false是否忽略不可用的索引
allow_no_indices布尔值false是否允许通配符解析为空索引
expand_wildcards枚举值open通配符展开选项。可选值:openclosedhiddennoneall
write_index_only布尔值false是否仅应用于别名的写索引

请求体参数(必需) #

请求体必须包含映射定义。支持两种格式:

无类型模式(推荐) #

{
  "properties": {
    "field1": {
      "type": "text",
      "analyzer": "standard"
    },
    "field2": {
      "type": "keyword"
    }
  }
}

有类型模式(已弃用) #

include_type_name=true 时使用:

{
  "type_name": {
    "properties": {
      "field1": { "type": "text" }
    }
  }
}

常见字段类型 #

类型描述示例
text全文搜索文本"type": "text"
keyword精确值(ID、标签等)"type": "keyword"
date日期时间"type": "date"
long64位整数"type": "long"
double64位浮点数"type": "double"
boolean布尔值"type": "boolean"
objectJSON 对象"type": "object"
nested嵌套对象"type": "nested"
ipIP 地址"type": "ip"

示例 #

添加新字段 #

PUT /my_index/_mapping
{
  "properties": {
    "email": {
      "type": "keyword"
    },
    "created_at": {
      "type": "date"
    }
  }
}

响应示例:

{
  "acknowledged": true
}

更新多个索引的映射 #

PUT /index1,index2/_mapping
{
  "properties": {
    "status": {
      "type": "keyword"
    }
  }
}

使用通配符更新 #

PUT /logs-*/_mapping
{
  "properties": {
    "log_level": {
      "type": "keyword"
    }
  }
}

添加带分析器的文本字段 #

PUT /my_index/_mapping
{
  "properties": {
    "content": {
      "type": "text",
      "analyzer": "standard",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

添加对象字段 #

PUT /my_index/_mapping
{
  "properties": {
    "user": {
      "properties": {
        "name": { "type": "text" },
        "email": { "type": "keyword" }
      }
    }
  }
}

添加嵌套字段 #

PUT /my_index/_mapping
{
  "properties": {
    "comments": {
      "type": "nested",
      "properties": {
        "user": { "type": "keyword" },
        "message": { "type": "text" }
      }
    }
  }
}

设置动态模板 #

PUT /my_index/_mapping
{
  "dynamic_templates": [
    {
      "strings_as_keywords": {
        "match_mapping_type": "string",
        "match": "*_id",
        "mapping": {
          "type": "keyword"
        }
      }
    }
  ]
}

更新动态映射设置 #

PUT /my_index/_mapping
{
  "dynamic": "strict",
  "properties": {
    "new_field": { "type": "text" }
  }
}

只更新打开的索引(默认) #

PUT /*/_mapping?expand_wildcards=open
{
  "properties": {
    "timestamp": { "type": "date" }
  }
}

只应用于写索引 #

PUT /logs_write/_mapping?write_index_only=true
{
  "properties": {
    "tag": { "type": "keyword" }
  }
}

字段属性 #

通用属性 #

属性类型描述
type字符串字段数据类型
doc_values布尔值是否使用 doc_values(默认多数类型为 true)
ignore_above整数超过此长度的值不索引(keyword 类型)
copy_to字符串/数组复制字段值到目标字段

文本字段属性 #

属性类型描述
analyzer字符串索引分析器
search_analyzer字符串搜索分析器
norms布尔值是否启用评分 norms
index布尔值是否字段可搜索
store布尔值是否单独存储

数值字段属性 #

属性类型描述
coerce布尔值是否强制类型转换
ignore_malformed布尔值是否忽略格式错误

错误处理 #

映射冲突 #

{
  "error": {
    "type": "mapper_parsing_exception",
    "reason": "failed to parse mapping [field1]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "cannot change parameter [type] from [text] to [keyword]"
    }
  }
}

索引不存在 #

{
  "error": {
    "type": "index_not_found_exception",
    "reason": "no such index [my_index]"
  }
}

使用场景 #

场景 1:逐步构建映射 #

# 创建索引时定义基本映射
PUT /my_index
{
  "mappings": {
    "properties": {
      "title": { "type": "text" }
    }
  }
}

# 后续添加新字段
PUT /my_index/_mapping
{
  "properties": {
    "category": { "type": "keyword" },
    "tags": { "type": "keyword" }
  }
}

场景 2:添加多字段 #

PUT /products/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "raw": { "type": "keyword" },
        "english": { "type": "text", "analyzer": "english" }
      }
    }
  }
}

场景 3:批量更新多个索引 #

PUT /logs-2024-*,logs-2025-*/_mapping
{
  "properties": {
    "hostname": { "type": "keyword" }
  }
}

注意事项 #

  1. 类型不可变:已有字段的数据类型不能修改
  2. 已索引数据:映射更新不影响已索引的数据
  3. 动态映射:禁用动态映射(dynamic: strict)时只能通过 API 添加字段
  4. 性能影响:更新映射会触发索引重建某些数据结构
  5. 测试优先:建议在测试环境验证映射变更后再应用到生产