您的位置:首页 > 其它

Elasticsearch索引管理

2016-08-24 09:18 253 查看

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: