Java API 之 索引管理

本文详细介绍了Elasticsearch中用于索引管理的API,包括索引存在、类型存在、创建、删除、关闭、打开、设置映射、别名管理、统计等操作,提供了丰富的代码示例。
摘要由CSDN通过智能技术生成

ElasticSearch为了便于处理索引管理(Indices administration)请求,提供了

org.elasticsearch.client.IndicesAdminClient接口。通过如下代码从 Client 对象中获得这个接口的实现:

 
 
 
  1. IndicesAdminClient indicesAdminClient = client.admin().indices();
IndicesAdminClient定义了好几种 prepareXXX()方法作为创建请求的入口点。
1. 索引存在API

索引存在API用于检查集群中是否存在由prepareExists调用指定的索引。

 
 
 
  1.    /**
  2.     * 判断索引是否存在
  3.     * @param client
  4.     * @param index
  5.     * @return
  6.     */
  7.    public static boolean isIndexExists(Client client, String index) {
  8.        if(Objects.equal(client, null)){
  9.            logger.info("--------- IndexAPI isIndexExists 请求客户端为null");
  10.            return false;
  11.        }
  12.        if(StringUtils.isBlank(index)){
  13.            logger.info("--------- IndexAPI isIndexExists 索引名称为空");
  14.            return false;
  15.        }
  16.        IndicesAdminClient indicesAdminClient = client.admin().indices();
  17.        IndicesExistsResponse response = indicesAdminClient.prepareExists(index).get();
  18.        return response.isExists();
  19.        /* 另一种方式
  20.        IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(index);
  21.        IndicesExistsResponse response = client.admin().indices().exists(indicesExistsRequest).actionGet();*/
  22.    }


prepareExists()可以同时指定多个索引:

 
 
 
  1. IndicesExistsResponse response = indicesAdminClient.prepareExists(index1, index2 ....).get();


2. 类型存在API

类型存在API和索引存在API类似,只是不是用来检查索引是否存在,而是检查指定索引下的指定类型是否存在。为了确保成功返回结果,请确保索引已经存在,否则不会查找到指定的类型。下面代码演示查找索引下的指定类型:

 
 
 
  1.    /**
  2.     * 判断类型是否存在
  3.     * @param client
  4.     * @param index
  5.     * @param type
  6.     * @return
  7.     */
  8.    public static boolean isTypeExists(Client client, String index, String type) {
  9.        if(!isIndexExists(client, index)){
  10.            logger.info("--------- isTypeExists 索引 [{}] 不存在",index);
  11.            return false;
  12.        }
  13.        IndicesAdminClient indicesAdminClient = client.admin().indices();
  14.        TypesExistsResponse response = indicesAdminClient.prepareTypesExists(index).setTypes(type).get();
  15.        return response.isExists();
  16.    }


3. 创建索引API

创建索引API可以用来建立一个新索引。我们可以创建空索引或者给它设置它的映射(mapping)和设置信息(settings)。

3.1 创建空索引

下面代码创建了一个空索引:

 
 
 
  1.    /**
  2.     * 创建空索引  默认setting 无mapping
  3.     * @param client
  4.     * @param index
  5.     * @return
  6.     */
  7.    public static boolean createSimpleIndex(Client client, String index){
  8.        IndicesAdminClient indicesAdminClient = client.admin().indices();
  9.        CreateIndexResponse response = indicesAdminClient.prepareCreate(index).get();
  10.        return response.isAcknowledged();
  11.    }

查看索引状态信息:

 
 
 
  1. {
  2.    "state": "open",
  3.    "settings": {
  4.        "index": {
  5.            "creation_date": "1476078197394",
  6.            "number_of_shards": "5",
  7.            "number_of_replicas": "1",
  8.            "uuid": "rBATEkx_SBq_oUEIlW8ryQ",
  9.            "version": {
  10.                "created": "2030399"
  11.            }
  12.        }
  13.    },
  14.    "mappings": {
  15.        
  16.    },
  17.    "aliases": [
  18.        
  19.    ]
  20. }


3.2. 创建复杂索引

下面代码创建复杂索引,给它设置它的映射(mapping)和设置信息(settings),指定分片个数为3,副本个数为2,同时设置school字段不分词。

 
 
 
  1.    /**
  2.     * 创建索引 指定setting
  3.     * @param client
  4.     * @param index
  5.     * @return
  6.     */
  7.    public static boolean createIndex(Client client, String index){
  8.        // settings
  9.        Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2).build();
  10.        // mapping
  11.        XContentBuilder mappingBuilder;
  12.        try {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值