欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch索引管理

程序员文章站 2022-07-05 14:00:03
...

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.