ElasticSearch53:索引管理_定制自己的dynamic mapping策略
2018-01-09 13:19
381 查看
1.定制dynamic策略
true:遇到陌生字段,就进行dynamic mapping
false:遇到陌生字段,就忽略
strict:遇到陌生字段,就报错
例子:
执行成功
{
"acknowledged": true,
"shards_acknowledged": true
}
测试加入一个陌生字段
执行结果:提示了mapping设置了strict,my_type中的content不允许dynamic,即不允许动态添加
例子2:上面的address的type是object,说明是对象类型,该类型的dynamic为true,所以如果address中遇到陌生字段,会进行dynamic mapping
PUT /index0/my_type/1
{
"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"
}
代码:
执行成功:
{
"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:
查询2:
GET /index0/my_type/_search
{
"query": {
"match": {
"title_en": "a"
}
}
}
执行结果2:
3.定制自己的default mapping template(index level)
PUT /index0
{
"mappings":{
"_default_":{
"_all":{"enabled":false}
},
"blog":{
"_all":{"enabled":true}
}
}
}
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] 索引管理 (五) - 默认映射,重索引,索引别名
- [ElasticSearch2.x]Java API 之 索引管理
- Elasticsearch中的索引管理和搜索常用命令总结
- 高效管理 Elasticsearch 中基于时间的索引——本质是在利用滚动模式做数据的冷热分离,热索引可以用ssd
- ElasticSearch初步学习,管理索引(入门二)
- ElasticSearch核心基础之索引管理
- ElasticSearch49:索引管理_快速上机动手实战创建、修改以及删除索引
- Django中定制自己的User和Group管理模块(类似对admin的二次开发)
- 模块管理常规功能自己定义系统的设计与实现(53--演示程序和视频解说 )
- [Elasticsearch] 向已存在的索引中加入自己定义filter/analyzer
- [Elasticsearch] 索引管理 (五) - 默认映射,重索引,索引别名
- testlink case的便捷管理,如何从excel中导入到testlink,定制导出自己想要的格式
- elasticsearch 索引 Warmer 管理
- elasticsearch索引管理
- [Elasticsearch] 索引管理 (四) - 动态映射
- [Elasticsearch] 索引管理 (一)
- Elasticsearch中的document数据格式,简单的集群管理,商品的索引的CRUD操作(学习资料记录)
- [Elasticsearch] 索引管理 (三) - 根对象(Root Object)
- Elasticsearch索引管理
- [Elasticsearch] 索引管理