您的位置:首页 > 移动开发

ElasticSearch53:索引管理_定制自己的dynamic mapping策略

2018-01-09 13:19 381 查看
1.定制dynamic策略

true:遇到陌生字段,就进行dynamic mapping

false:遇到陌生字段,就忽略

strict:遇到陌生字段,就报错

例子:
PUT /index0
{
"mappings":{
"my_type":{
"dynamic":"strict",
"properties": {
"title":{"type": "text"},
"address":{
"type": "object",
"dynamic":"true"
}
}
}
}
}


执行成功

{

  "acknowledged": true,

  "shards_acknowledged": true

}

测试加入一个陌生字段
PUT /index0/my_type/1
{
"title":"my title",
"content":"this is content",
"address":{
"province":"zhejiang",
"city":"hangzhou"
}
}


执行结果:提示了mapping设置了strict,my_type中的content不允许dynamic,即不允许动态添加
{
"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
}


例子2:上面的address的type是object,说明是对象类型,该类型的dynamic为true,所以如果address中遇到陌生字段,会进行dynamic mapping

PUT /index0/my_type/1

{

  "title":"my title",

  "address":{

    "province":"zhejiang",

    "city":"hangzhou"

  }

}

执行结果:
{
"_index": "index0",
"_type": "my_type",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}


获取数据
GET /index0/my_type/1

{
"_index": "index0",
"_type": "my_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"title": "my title",
"address": {
"province": "zhejiang",
"city": "hangzhou"
}
}
}


如果设置成dynamic:strict,那么就会拒绝添加

3.定制dynamic mapping策略

1)date_detection

默认会按照一定格式识别date,比如yyyy-MM-dd,但是如果某个field先过来一个2017-01-01,就会被自动dynamic mapping成date,后面如果再来一个hello world之类的值,就会报错。可以手动关闭某个type的date_detection,

如果需要,自己手动指定某个field为date类型。

新建的type

PUT /index0

{

  "mappings":{

    "my_type":{

      "date_detection":"false"

    }

  }

}

修改已存在的index中的type

PUT /index0/my_type/_mapping

{

    "date_detection":"false"

}

2)定制自己的dynamic mapping template(type level)

例子:如果index0中的my_type创建了某个字段field匹配到*_en,那么就会匹配下面定义的mapping

            "match_mapping_type":"string",

            "mappings":{

              "type":"string",

              "analyzer":"spanish"

            }

代码:
PUT /index0
{
"mappings": {
"my_type":{
"dynamic_templates":[
{
"en":{
"match":"*_en",
"match_mapping_type":"string",
"mapping":{
"type":"string",
"analyzer":"english"
}
}
}
]
}
}
}


执行成功:

{

  "acknowledged": true,

  "shards_acknowledged": true

}

此时插入两条数据测试

PUT /index0/my_type/1

{

  "title":"this is a new title"

}

PUT /index0/my_type/2

{

  "title_en":"this is a new title"

}

title没有匹配到任何的dynamic模板,默认就是standard,分词器,不会过滤停用词,is进入倒排索引中,用is可以查询到doc1

title_en匹配到了dynamic模板,就是english分词器,会过滤停用词,is这种停用词会被过滤了,用is查询的话,就查询不到

查询1:

GET /index0/my_type/_search

{

  "query": {

    "match": {

      "title": "is"

    }

  }

}

执行结果1:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2824934,
"hits": [
{
"_index": "index0",
"_type": "my_type",
"_id": "1",
"_score": 0.2824934,
"_source": {
"title": "this is a new title"
}
}
]
}
}


查询2:

GET /index0/my_type/_search

{

  "query": {

    "match": {

      "title_en": "a"

    }

  }

}

执行结果2:
{
"took": 97,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}


3.定制自己的default mapping template(index level)

PUT /index0

{

    "mappings":{

        "_default_":{

            "_all":{"enabled":false}

        },

        "blog":{

            "_all":{"enabled":true}

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch