总结一些ES的操作方式及语法


 


查看健康状态


curl -XGET ​​http://localhost:9200/_cluster/health?pretty​


 


查看索引


curl -XGET ​​http://localhost:9200/_cat/indices?​


 


迁移索引


命令:curl _XPOST 'ES数据库请求地址:9200/_reindex' -d{"source":{"index":"old_index"},"dest":{"index":"new_index"}}


代码:


POST _reindex


{


"source": {


"index": "old_index"


},


"dest": {


"index": "new_index"


}


}


==================================================================================================================


索引别名


一个索引可以接受多个别名,而一个别名也可以映射到多个索引,当指定别名时,别名将自动扩展到添加的索引。别名也可以关联到 filter,然后自动应用到检索,和 routing value。别名不能与索引同名。


 


添加别名,移除别名示例:


POST /_aliases


{


"actions" : [


{ "add" : { "index" : "test1", "alias" : "alias1" } },


{ "remove" : { "index" : "test1", "alias" : "alias2" } }


]


}


 


在同一个 API 接口中可以先移除然后添加操作。该操作是原子操作,无需担心别名不指向任何一个索引的短暂瞬间:


POST /_aliases


{


"actions" : [


{ "remove" : { "index" : "test1", "alias" : "alias1" } },


{ "add" : { "index" : "test2", "alias" : "alias1" } }


]


}


 


过滤器别名


创建过滤器别名,必须要有相应的字段映射。过滤器可以使用 Query DSL 定义,该别名可以用来检索,计数,删除等操作。


 


PUT /test1


{


"mappings": {


"_doc": {


"properties": {


"user" : {


"type": "keyword"


}


}


}


}


}


 


Routing


可以将路由值与别名相关联。此功能可以与过滤器别名一起使用,以避免不必要的分片操作。


 


POST /_aliases


{


"actions" : [


{


"add" : {


"index" : "test",


"alias" : "alias1",


"routing" : "1"


}


}


]


}


 


创建索引的时候指定


curl -X PUT "localhost:9200/logs_20162801" -H 'Content-Type: application/json' -d'


{


"mappings" : {


"_doc" : {


"properties" : {


"year" : {"type" : "integer"}


}


}


},


"aliases" : {


"current_day" : {},


"2016" : {


"filter" : {


"term" : {"year" : 2016 }


}


}


}


}


'