#dynamic mapping,推断字段的类型
PUT mapping_test/_doc/1
{
"uid" : "123",
"isVip" : false,
"isAdmin": "true",
"age":19,
"heigh":180
}
#####返回结果
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
#查看 mapping
GET mapping_test/_mapping
#####查询结果
{
"mapping_test" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"heigh" : {
"type" : "long"
},
"isAdmin" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"isVip" : {
"type" : "boolean"
},
"uid" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
#默认Mapping支持dynamic,写入的文档中加入新的字段
PUT mapping_test/_doc/1
{
"newField":"someValue"
}
#####返回结果
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
#查看 mapping, newField字段已被添加到mapping
GET mapping_test/_mapping
#该字段可以被搜索,数据也在_source中出现
POST mapping_test/_search
{
"query":{
"match":{
"newField":"someValue"
}
}
}
#####返回结果
{
"took" : 480,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"newField" : "someValue"
}
}
]
}
}
#修改为dynamic false
PUT mapping_test/_mapping
{
"dynamic": false
}
#新增 anotherField
PUT mapping_test/_doc/10
{
"anotherField":"someValue"
}
#####返回结果
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "10",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
#该字段不可以被搜索,因为dynamic已经被设置为false
POST mapping_test/_search
{
"query":{
"match":{
"anotherField":"someValue"
}
}
}
#####返回结果
{
"took" : 303,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
#只能通过id获得文档_source看到内容
GET mapping_test/_doc/10
#####返回结果
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "10",
"_version" : 1,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"anotherField" : "someValue"
}
}
#修改为strict
PUT mapping_test/_mapping
{
"dynamic": "strict"
}
#写入数据出错,HTTP Code 400
PUT mapping_test/_doc/12
{
"lastField":"value"
}
#####返回结果
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
},
"status": 400
}
DELETE mapping_test