一、创建索引
put http://localhost:9200/<index> ,<index>是索引名,传的JSON是这个索引的数据格式。
1、索引名不能包含任何大写字母;
2、如果索引已存在,则抛出索引已存在的异常;
3、elasticsearch 默认给一个索引设置5个分片1个分片,分派给定之后不再修改,副本可以随时修改;
put http://localhost:9200/blog
结果:
{
"acknowledged": true,
"shards_acknowledged": true
}
创建3个分片0个副本名为bolg的索引
put http://localhost:9200/blog
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
结果:
{
"acknowledged": true,
"shards_acknowledged": true
}
二、修改副本
put http://localhost:9200/blog/_settings
{
"number_of_replicas": 2
}
结果:
{
"acknowledged": true
}
三、读写权限
1、bloks.read_only:true 设置当前索引只允许读不允许写或者更新
2、blocks.read:true 设置对当前索引进行读操作
3、blocks.write:true 设置对当前索引进行写操作
put http://localhost:9200/blog/_settings
{
"blocks.write": true
}
结果:
{
"acknowledged": true
}
四、查看索引
1、查看索引的配置信息:get http://localhost:9200/blog/_settings
2、同时查看多个索引的setting信息:get http://localhost:9200/blog2,blog/_settings
3、查看集群中所有索引的setting信息:get http://localhost:9200/_all/_settings
五、删除索引
delete http://localhost:9200/<index>
六、索引的关闭与打开
1、关闭索引:POST http://localhost:9200/<index>/_close
2、打开索引:POST http://localhost:9200/<index>/_open
3、关闭或者打开索引也支持批量操作:POST http://localhost:9200/<index>,<index>,<index>/_open
4、关闭或者打开索引支持通配符方式,
比如,关闭已 test 开头的索引:POST http://localhost:9200/test*/_close
七、复制索引
_reindex API可以吧文档从一个索引(源索引)复制到另一个索引(目标索引),目标索引不会复制源索引的配置信息,_reindex 操作之前需要设置目标索引的分片数、副本数等配置信息。
post http://localhost:9200/_reindex
{
"source": {"index": "blog"},
"dest": {"index": "blog2"}
}
也可以在源索引中添加 type 和 query 来限制文档,
比如把 blog 索引 article 类型下 title 中含有 git 关键字的文档复制到 blog2 索引中
post http://localhost:9200/_reindex
{
"source": {
"index": "blog",
"type": "article",
"query": {
"term": {"title": "git"}
}
},
"dest": {"index": "blog2"}
}
八、收缩索引
一个索引的分片初始化以后是无法再做修改的,但可以使用 shrink index API 提供的缩小索引分片数机制,把一个索引变成一个更小分片的索引,但是收缩后的分片数必须是原始分片数的因子。收缩索引之前,索引中的每个分片都要在同一个节点上。
收缩索引步骤:
前提条件:
在缩小索引之前,索引必须被标记为只读,所有分片都会复制到一个相同的节点并且节点健康值为绿色的。
put http://localhost:9200/<index>/_settings
{
"index.routing.allocation.require._name": "shrink_node_name",
"index.blocks.write": true
}
首先,创建一个新的目标索引,设置与源索引相同,但是新索引的分片数量较少。
然后,把源索引硬链接到目标索引。(如果文件系统不支持硬链接,那么所有段都被复制到新索引中,这是一个耗时的过程)
最后,新的索引恢复使用。
post http://localhost:9200/<index>/_shrink/<index_1>
{
"settings": {
"index.number_of_replicas": 0,
"index.number_of_shards": 1,
"index.codec": "best_compression"
},
"aliases": {
"my_search_indices": {}
}
}
结果:
{
"acknowledged": true,
"shards_acknowledged": true
}
九、索引别名
设置了别名后,对别名的操作等价于对索引的操作。
注意:如果别名和索引是一对一,使用别名索引文档或者根据id查询文档是可以的,但是如果别名和索引是一对多的,使用别名会发生错误,因为elasticsearch 不知道把文档写入哪个索引或者从哪个索引中读取文档。
给一个索引添加别名:
post http://localhost:9200/_aliases
{
"actions": [
{ "add": { "index" : "<index>", "alias" : "<alias>"}}
]
}
移除一个索引的别名:
post http://localhost:9200/_aliases
{
"actions": [
{ "remove": { "index" : "<index>", "alias" : "<alias>"}}
]
}
一次给多个索引创建同一个别名:
post http://localhost:9200/_aliases
{
"actions": [
{ "add": { "index" : "<index1>", "alias" : "<alias>"}},
{ "add": { "index" : "<index2>", "alias" : "<alias>"}}
]
}
或
post http://localhost:9200/_aliases
{
"actions": [
{ "add": { "index" : "["<index1>", "<index2>"]", "alias" : "<alias>"}}
]
}
添加别名和移除别名也可以同时进行:
post http://localhost:9200/_aliases
{
"actions": [
{ "add": { "index" : "<index1>", "alias" : "<alias>"}},
{ "remove": { "index" : "<index2>", "alias" : "<alias>"}}
]
}
查看一个索引的别名:
get http://localhost:9200/<index>/_aliases
查看别名对应哪些索引:
get http://localhost:9200/<aliase>/_aliases