索引映射(mappings)管理及配置管理

elasticsearch中的文档等价于java中的对象,那么在java对象中有字段(比如string、int、long等),同理在elasticsearch索引中的具体字段也是有类型的。

如果没有指定字段类型,elasticsearch会默认指定。但最好是使用mapping的映射管理,提前指定字段的类型,防止后续的程序问题;

PUT school
{
  "mappings": {
    "logs" : {
      "properties": {"messages" : {"type": "text"}}
    }
  }

获取映射字段

语法:

GET /{index}/_mapping/{type}/field/{field}

GET /school/_mapping/logs/field/number

索引库配置管理

索引库配置

所谓的settings就是用来修改索引分片和副本数的;

比如有的重要索引,副本数很少甚至没有副本,那么我们可以通过setting来添加副本数

DELETE document
PUT document
{
  "mappings": {
    "article" : {
      "properties":
      {
        "title" : {"type": "text"} , 
        "author" : {"type": "text"} , 
        "titleScore" : {"type": "double"} 
        
      }
    }
  }
}
GET /document/_settings

为了提高容错性,我们可以把副本数改成2:

PUT /document/_settings
{
  "number_of_replicas": 2
}

副本可以改,分片不能改

零停机重新索引数据

对于文档的操作,偶尔会遇到这种问题:

某一个字段的类型不符合后期的业务了,但是当前的索引已经创建了,我们知道es在字段的mapping建立后就不可再次修改mapping的值。

1 新建索引库articles1,并添加数据

DELETE articles1
PUT articles1
{  
    "settings":{  
         "number_of_shards":3,  
         "number_of_replicas":1  
    },  
    "mappings":{  
         "article":{  
             "dynamic":"strict",  
             "properties":{  
                 "id":{"type": "text", "store": true},  
                 "title":{"type": "text","store": true}, 
                 "readCounts":{"type": "integer","store": true},  
                 "times": {"type": "text", "index": false}
             }  
         }  
    }  
}


PUT articles1/article/1
{
  "id" : "1",
  "title" : "世界1",
  "readCounts" : 2 , 
  "times" : "2018-05-01"
}

get articles1/article/1

2 新建索引库articles2

DELETE articles2
PUT articles2
{  
    "settings":{  
         "number_of_shards":5,  
         "number_of_replicas":1  
    },  
    "mappings":{  
         "article":{  
             "dynamic":"strict",  
             "properties":{  
                 "id":{"type": "text", "store": true},  
                 "title":{"type": "text","store": true}, 
                 "readCounts":{"type": "integer","store": true},  
                 "times": {"type": "date", "index": false}
             }  
         }  
    }  
}  
}


GET articles2/article/1

3 拷贝数据并验证

POST _reindex
{
  "source": {
    "index": "articles1"
  },
  "dest": {
    "index": "articles2"
  }
}

GET articles2/article/1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值