ElasticSearch为了便于处理索引管理(Indices administration)请求,提供了
org.elasticsearch.client.IndicesAdminClient接口。通过如下代码从 Client 对象中获得这个接口的实现:
IndicesAdminClient定义了好几种 prepareXXX()方法作为创建请求的入口点。
IndicesAdminClient indicesAdminClient = client.admin().indices();
1. 索引存在API
索引存在API用于检查集群中是否存在由prepareExists调用指定的索引。
/**
* 判断索引是否存在
* @param client
* @param index
* @return
*/
public static boolean isIndexExists(Client client, String index) {
if(Objects.equal(client, null)){
logger.info("--------- IndexAPI isIndexExists 请求客户端为null");
return false;
}
if(StringUtils.isBlank(index)){
logger.info("--------- IndexAPI isIndexExists 索引名称为空");
return false;
}
IndicesAdminClient indicesAdminClient = client.admin().indices();
IndicesExistsResponse response = indicesAdminClient.prepareExists(index).get();
return response.isExists();
/* 另一种方式
IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(index);
IndicesExistsResponse response = client.admin().indices().exists(indicesExistsRequest).actionGet();*/
}
prepareExists()可以同时指定多个索引:
IndicesExistsResponse response = indicesAdminClient.prepareExists(index1, index2 ....).get();
2. 类型存在API
类型存在API和索引存在API类似,只是不是用来检查索引是否存在,而是检查指定索引下的指定类型是否存在。为了确保成功返回结果,请确保索引已经存在,否则不会查找到指定的类型。下面代码演示查找索引下的指定类型:
/**
* 判断类型是否存在
* @param client
* @param index
* @param type
* @return
*/
public static boolean isTypeExists(Client client, String index, String type) {
if(!isIndexExists(client, index)){
logger.info("--------- isTypeExists 索引 [{}] 不存在",index);
return false;
}
IndicesAdminClient indicesAdminClient = client.admin().indices();
TypesExistsResponse response = indicesAdminClient.prepareTypesExists(index).setTypes(type).get();
return response.isExists();
}
3. 创建索引API
创建索引API可以用来建立一个新索引。我们可以创建空索引或者给它设置它的映射(mapping)和设置信息(settings)。
3.1 创建空索引
下面代码创建了一个空索引:
/**
* 创建空索引 默认setting 无mapping
* @param client
* @param index
* @return
*/
public static boolean createSimpleIndex(Client client, String index){
IndicesAdminClient indicesAdminClient = client.admin().indices();
CreateIndexResponse response = indicesAdminClient.prepareCreate(index).get();
return response.isAcknowledged();
}
查看索引状态信息:
{
"state": "open",
"settings": {
"index": {
"creation_date": "1476078197394",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "rBATEkx_SBq_oUEIlW8ryQ",
"version": {
"created": "2030399"
}
}
},
"mappings": {
},
"aliases": [
]
}
3.2. 创建复杂索引
下面代码创建复杂索引,给它设置它的映射(mapping)和设置信息(settings),指定分片个数为3,副本个数为2,同时设置school字段不分词。
/**
* 创建索引 指定setting
* @param client
* @param index
* @return
*/
public static boolean createIndex(Client client, String index){
// settings
Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2).build();
// mapping
XContentBuilder mappingBuilder;
try {