这里记录一下elasticsearch2.1.0索引的相关的操作,代码都是自己经过测试可以直接使用的代码。
/**
* es索引相关操作测试类
* @author yujie.wang
*/
public class IndexOperator {
private static String INDEX = "my_store";
private static String TYPE = "products";
/**
* 判断一个索引是否存在
* @param client
* @param index
*/
public void isExists(IndicesAdminClient client,String index){
IndicesExistsResponse response = client.prepareExists(index).get();
System.out.println(response.isExists() ? "exists" : "no");
// another mthod
/* IndicesExistsRequest request = new IndicesExistsRequest(index);
IndicesExistsResponse response2 = indexClient.exists(request).actionGet();
System.out.println(response2.isExists() ? "exists": " no");*/
}
/**
* 判断一个索引的类型是否存在
* @param client
* @param index
* @param type
*/
public void isTypeExists(IndicesAdminClient client , String index, String type){
TypesExistsResponse response = client.prepareTypesExists(index).setTypes(type).get();
System.out.println(response.isExists() ? " type exists": " no");
}
/**
* 创建一个索引
* @param client
* @param index
*/
public void createIndex(IndicesAdminClient client , String index){
CreateIndexResponse response = client.prepareCreate(index).get();
System.out.println(response.isAcknowledged() ? " create successfully": " create failed");
}
/* {
"state": "open",
"settings": {
"index": {
"creation_date": "1513589566589",
"uuid": "d8C9DP04QdW9BUD2M6JBLg