码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch索引管理

时间:2016-08-24 09:58:06      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

1.判断索引是否存在

IndicesExistsResponse indexResponse = ia.client.admin().indices().prepareExists("blog")
.execute().actionGet();

System.out.println(indexResponse.isExists());

也可以同时判断多个索引是否存在:

IndicesExistsResponse indexResponse = ia.client.admin().indices().prepareExists("blog","blog1").execute().actionGet();

2. 判断类型是否存在

TypesExistsResponse typeResponse = ia.client.admin().indices().prepareTypesExists("news").setTypes("document").execute().actionGet();
System.out.println(typeResponse.isExists());

3.删除索引

DeleteIndexResponse deleteResponse = ia.client.admin().indices().prepareDelete("news")
                .execute().actionGet();
if (deleteResponse.isAcknowledged()) {
    System.out.println("删除索引成功");
} else {
    System.out.println("删除索引失败");
}

4.创建索引

CreateIndexResponse createIndexResponse = ia.client.admin().indices().prepareCreate("news")
                .execute().actionGet();
if (createIndexResponse.isAcknowledged()) {
    System.out.println("创建索引成功");
} else {
    System.out.println("创建索引失败");
}

5.关闭索引

关闭不使用的索引可以释放节点和集群的资源(比如CPU时钟周期和内存),如果某个索引库不想被搜索也可以直接关闭.
关闭索引的RESTful命令:

curl  -XPOST "http://127.0.0.1:9200/indexname/_close"

java api:

CloseIndexResponse cIndexResponse = ia.client.admin().indices().prepareClose("indexname ").execute().actionGet();
if (cIndexResponse.isAcknowledged()) {
    System.out.println("关闭索引成功");
} 

如果要关闭的索引不存在的话会报错.
也可以一次关闭多个索引:

CloseIndexResponse cIndexResponse = ia.client.admin().indices()
        .prepareClose("indexname1","indexname2")
.execute()
.actionGet();

6.打开索引

把已经关闭的索引打开。
RESTful命令:

curl -XPOST "http://127.0.0.1:9200/indexname/_open"

打开索引的Java api:

OpenIndexResponse oIndexResponse = ia.client.admin().indices().prepareOpen("indexname1","indexname2")execute().actionGet();

System.out.println(oIndexResponse.isAcknowledged());

同样,要打开的索引必须是存在的,否则会报IndexNotFoundException.

Elasticsearch索引管理

标签:

原文地址:http://blog.csdn.net/napoay/article/details/52296707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!