一、创建索引
PUT /hotel // hotel为索引名称
{
"settings" : {
"number_of_shards" : 15, //指定主分片个数
"number_of_replicas" : 2 //指定副分片个数
},
"mappings":{
"properties":{ … }
}
}
二、删除索引
DELETE /hotel
三、关闭索引
POST /hotel/_close
索引关闭后进行搜索和写入都会报错,错误:
{
"error" : {
"root_cause" : [
{
"type" : "index_closed_exception", //提示异常类型为索引已经关闭
"reason" : "closed",
"index_uuid" : "TIYkM2N5SCKlnmCOXuJPmg",
"index" : "hotel" //当前索引名称
}
],
"type" : "index_closed_exception",
"reason" : "closed",
"index_uuid" : "TIYkM2N5SCKlnmCOXuJPmg",
"index" : "hotel" },
"status" : 400
}
四、打开索引
POST /hotel/_open
五、索引建立别名
别名可以一个别名对应一个索引或一个别名对应多个索引,当一个别名对应多个索引时,可以起到聚合的作用查询别名时,相当于分别查询对应索引,并把数据聚合,但是写入时这个别名不能指向多个索引,写入报错:
{ "error" : { "root_cause" : [ { //无法写入数据的报错信息 "type" : "illegal_argument_exception", "reason" : "no write index is defined for alias [last_three_month]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index" } ], //无法写入数据的报错信息 "type" : "illegal_argument_exception", "reason" : "no write index is defined for alias [last_three_month]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index" }, "status" : 400 //返回状态码 }
如果一个别名对应多个索引需要根据别名写入,可以在设置别名时加入is_write_index属性设为true
POST /_aliases { "actions": [ { "add": { //设置index_name为索引别名aliase的数据写入转发对象 "index": "index_name", "alias": "aliase", "is_write_index":true } } ] }
别名的另一个作用是:索引创建后一些参数是不能修改了(如:主分片的个数),随着业务发展,索引数据增多,需要修改索引参数进行优化,如果需要平滑的解决问题可以使用别名,再建一个索引指向这个别名,然后删除旧的索引。
六 查看映射
GET /houtel/_mapping
ES的返回结果如下:
{ "hotel" : { "mappings" : { "properties" : { "city" : { //定义city字段类型为keyword "type" : "keyword" }, "price" : { //定义price字段类型为double "type" : "double" }, "title" : { //定义title字段类型为text "type" : "text" } } } } }
七、扩展映射
ES的mapping映射中的字段是不能修改的,但是字段可以扩展,最常见的是增加字段和为Object类型的数据新增属性。
POST /hotel/_mapping
{
"properties": {
"tag":{
"type":"keyword"
}
}
}
再查看映射
{ "hotel" : { "mappings" : { "properties" : { "city" : { //原有的city字段,类型为keyword "type" : "keyword" }, "price" : { //原有的price字段,类型为double "type" : "double" }, "tag" : { //新增tag字段,类型为keyword "type" : "keyword" }, "title" : { //原有的title字段,类型为text "type" : "text" } } } } }