共计 3041 个字符,预计需要花费 8 分钟才能阅读完成。
以往我们在创建索引时会同时设置索引的mappings,然而对于elasticsearch来说,就算索引初次创建时未设置mappings,在用户直接插入数据时es会自动判断字段类型并设置mapping,从而达到一种动态设置mapping的效果,如果后续有新字段的引入,也会为新增字段增加mappIngs。
有个点不要搞混了,就是mappings中的字段一旦配置了,那么字段的mappings将不可再改变,比如某字段type以设置为keyword,则以后都不能更改为其他type,只能reindex来重建。和此处讲的动态mappings不是一个事!!!!此处动态仅仅是对字段自动新建相应的mappings。
动态创建mappings
# 向一个不存在的索引插入数据
PUT test_mappings/_doc/1
{
"message":"德玛西亚大宝剑"
}
查看自动创建的索引的配置信息
# 查看该索引配置
GET test_mappings
# 输出
{
"test_mappings" : {
"aliases" : { },
"mappings" : {
"properties" : {
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1593880162297",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "EjGl-PCBTqOhPv-vTgtOHQ",
"version" : {
"created" : "7070099"
},
"provided_name" : "test_mappings"
}
}
}
}
从上面我们可以看到该索引已经自动为message该字段设置了mappings,此时我们再插入第二条数据,同时增加一个字段
PUT test_mappings/_doc/2
{
"message":"啦啦啦德玛西亚",
"count": 23
}
# 再次查看该索引配置
{
"test_mappings" : {
"aliases" : { },
"mappings" : {
"properties" : {
"count" : {
"type" : "long"
},
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1593880162297",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "EjGl-PCBTqOhPv-vTgtOHQ",
"version" : {
"created" : "7070099"
},
"provided_name" : "test_mappings"
}
}
}
}
从上面的例子中可以看到elasticsearch会为新增的字段根据类型自动设置mappings,这是es的默认配置。我们可以通过dynamic
这个属性控制,其值有:
- true:缺省值,动态添加新字段映射
- false:忽略新字段映射创建,但是新字段数据依然能插入,只是根据该新字段查询是会查不到数据的,但是会在查询结果中显示(根据已存在mapping字段的查询结果)
- strict:遇到新字段,直接抛出异常
关闭动态mappings
# 创建索引时关闭动态创建索引
PUT test_mappings2
{
"mappings": {
"dynamic":false,
"properties": {
"message":{
"type": "text"
}
}
}
}
插入数据测试下
PUT test_mappings2/_doc/1
{
"message":"啦啦啦德玛西亚"
}
PUT test_mappings2/_doc/2
{
"message":"啦啦啦德玛西亚",
"count": 23
}
GET test_mappings2/_search
{
"query": {
"match": {
"count": "23"
}
}
}
# 获取到的输出
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
查询其他字段时
GET test_mappings2/_search
{
"query": {
"match": {
"message": "德玛"
}
}
}
# 输出
{
"took" : 101,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.21072102,
"hits" : [
{
"_index" : "test_mappings2",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.21072102,
"_source" : {
"message" : "啦啦啦德玛西亚",
"count" : 23
}
},
{
"_index" : "test_mappings2",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.21072102,
"_source" : {
"message" : "啦啦啦德玛西亚"
}
}
]
}
}
将mappings设置为严格模式
PUT test_mappings3
{
"mappings": {
"dynamic": "strict",
"properties": {
"message":{
"type": "text"
}
}
}
}
PUT test_mappings3/_doc/1
{
"message":"啦啦啦德玛西亚"
}
# 插入数据成功,输出:
{
"_index" : "test_mappings3",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
测试插入新字段数据
PUT test_mappings3/_doc/2
{
"message":"啦啦啦德玛西亚",
"count": 2333
}
# 输出
{
"error" : {
"root_cause" : [
{
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [count] within [_doc] is not allowed"
}
],
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [count] within [_doc] is not allowed"
},
"status" : 400
}
正文完