动态映射mapping
es字段和映射类型在使用之前不需要定义。通过动态映射,仅通过索引文档即可自动添加新的字段名称。新字段既可以添加到顶级映射类型,也可以添加到内部object 和nested字段。
可以配置自定义动态映射规则模块用于新字段的映射。
PUT data/_doc/1 { "count": 5 }
假如data 索引不存在 将自动创建, count 类型默认是long
es默认动态映射规则
es 默认识别日期的模板格式如下, 可以识别 "2015/09/02" , "2019/10/18 10:10:10" 这样的日期字符串
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
添加自定义日期识别模板
PUT my_index { "mappings": { "dynamic_date_formats": ["MM/dd/yyyy"] } } PUT my_index/_doc/1 { "create_date": "09/25/2015" }
禁止 自动识别
PUT my_index { "mappings": { "date_detection": false } }
es 默认的数字字符串检查时关闭的,下面选项将开启数字字符串检查,
PUT my_index { "mappings": { "numeric_detection": true } } PUT my_index/_doc/1 { "my_float": "1.0", "my_integer": "1" } my_float 映射类型为float , my_integer 映射结果为long
官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/dynamic-field-mapping.html
使用created index API显示创建mapping
PUT /my-index { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } } }
使用 put mapping API可以添加一个或者多个新的字段到一个存在的索引.
下面的例子添加了一个 employee-id 的字段, 设置index为false 表示只是存储这个字段,这个字段上不建立索引,所以这个字段不能搜索.
PUT /my-index/_mapping { "properties": { "employee-id": { "type": "keyword", "index": false } } }
修改映射条件
您不能更改现有字段的映射,但以下情况除外:
您可以将新属性添加到object字段。
您可以使用fieldmapping参数启用多字段。
您可以更改ignore_above映射参数的值。
更改现有字段的映射可能会使已经建立索引的数据无效。如果您需要更改字段映射,创建具有正确映射一个新的索引和重新索引的数据转换成指数。如果只想重命名字段,请考虑添加一个alias字段。
使用 get field APi 查看创建的mapping
GET /my-index/_mapping
{
"my-index" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"email" : {
"type" : "keyword"
},
"employee-id" : {
"type" : "keyword",
"index" : false
},
"name" : {
"type" : "text"
}
}
}
}
}
查看单个属性
GET /my-index/_mapping/field/employee-id
{
"my-index" : {
"mappings" : {
"employee-id" : {
"full_name" : "employee-id",
"mapping" : {
"employee-id" : {
"type" : "keyword",
"index" : false
}
}
}
}
}
}