16、ElasticSearch 7.3 实战:RestHighLevelClient Java api实现索引的创建、删除、是否存在、关闭、开启

1、写在前面

注意:导入的包区别,不同的包创建索引的方式不同。博主亲身实践,具体体现在createIndexRequest.mapping()里面。读者可自行试验。

import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;

由此可以猜想一下:

import org.elasticsearch.client.indices.*;
import org.elasticsearch.action.admin.indices.*;

可以看到上述两种方式导入的包的子类名是相同的,但是具体对索引的操作方式可能是不同的。具体的区别博主暂时还不清楚,后续若有进展再在此处更新。

2、同步创建索引

例如要实现如下索引的创建

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "description": {
        "type": "text"
      },
      "price": {
        "type": "long"
      },
      "pic": {
        "type": "text",
        "index": false
      }
    }
  },
  "aliases": {
    "default_index": {}
  }
}

代码如下(3种方式):

//创建索引
@Test
public void testCreateIndex() throws IOException {

    //创建索引对象
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
    //设置参数
    createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));
    //指定映射1
    createIndexRequest.mapping(" {\n" +
                " \t\"properties\": {\n" +
                "            \"name\":{\n" +
                "             \"type\":\"keyword\"\n" +
                "           },\n" +
                "           \"description\": {\n" +
                "              \"type\": \"text\"\n" +
                "           },\n" +
                "            \"price\":{\n" +
                "             \"type\":\"long\"\n" +
                "           },\n" +
                "           \"pic\":{\n" +
                "             \"type\":\"text\",\n" +
                "             \"index\":false\n" +
                "           }\n" +
                " \t}\n" +
                "}", XContentType.JSON);

        //指定映射2
//        Map<String, Object> message = new HashMap<>();
//        message.put("type", "text");
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("message", message);
//        Map<String, Object> mapping = new HashMap<>();
//        mapping.put("properties", properties);
//        createIndexRequest.mapping(mapping);

        //指定映射3
//        XContentBuilder builder = XContentFactory.jsonBuilder();
//        builder.startObject();
//        {
//            builder.startObject("properties");
//            {
//                builder.startObject("message");
//                {
//                    builder.field("type", "text");
//                }
//                builder.endObject();
//            }
//            builder.endObject();
//        }
//        builder.endObject();
//        createIndexRequest.mapping(builder);
        //设置别名
        createIndexRequest.alias(new Alias("itheima_index_new"));

        // 额外参数
        //设置超时时间
        createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
        //设置主节点超时时间
        createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes**1、**  );
        //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
        createIndexRequest.waitForActiveShards(ActiveShardCount.from(2));
        createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT);

        //操作索引的客户端
        IndicesClient indices = client.indices();
        //执行创建索引库
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);

        //得到响应(全部)
        boolean acknowledged = createIndexResponse.isAcknowledged();
        //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
        boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();

        System.out.println("acknowledged:" + acknowledged);
        System.out.println("shardsAcknowledged:" + shardsAcknowledged);

    }

3、异步创建索引

//创建索引异步方式
@Test
public void testCreateIndexAsync() throws IOException {

    //创建索引对象
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book");
    //设置参数
    createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));
    //指定映射1   与创建文档做类别
        createIndexRequest.mapping(" {\n" +
                " \t\"properties\": {\n" +
                "            \"name\":{\n" +
                "             \"type\":\"keyword\"\n" +
                "           },\n" +
                "           \"description\": {\n" +
                "              \"type\": \"text\"\n" +
                "           },\n" +
                "            \"price\":{\n" +
                "             \"type\":\"long\"\n" +
                "           },\n" +
                "           \"pic\":{\n" +
                "             \"type\":\"text\",\n" +
                "             \"index\":false\n" +
                "           }\n" +
                " \t}\n" +
                "}", XContentType.JSON);

        //指定映射2
//        Map<String, Object> message = new HashMap<>();
//        message.put("type", "text");
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("message", message);
//        Map<String, Object> mapping = new HashMap<>();
//        mapping.put("properties", properties);
//        createIndexRequest.mapping(mapping);

        //指定映射3
//        XContentBuilder builder = XContentFactory.jsonBuilder();
//        builder.startObject();
//        {
//            builder.startObject("properties");
//            {
//                builder.startObject("message");
//                {
//                    builder.field("type", "text");
//                }
//                builder.endObject();
//            }
//            builder.endObject();
//        }
//        builder.endObject();
//        createIndexRequest.mapping(builder);
        //设置别名
        createIndexRequest.alias(new Alias("itheima_index_new"));

        // 额外参数
        //设置超时时间
        createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
        //设置主节点超时时间
        createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes**1、**  );
        //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
        createIndexRequest.waitForActiveShards(ActiveShardCount.from(2));
        createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT);

        //操作索引的客户端
        IndicesClient indices = client.indices();
        //执行创建索引库
//        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                //得到响应(全部)
                boolean acknowledged = createIndexResponse.isAcknowledged();
                //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
                boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();

                System.out.println("acknowledged:" + acknowledged);
                System.out.println("shardsAcknowledged:" + shardsAcknowledged);
            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        };

        client.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT, listener);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

4、同步删除索引

//    删除索引
@Test
public void testDeleteIndex() throws IOException {
    //创建删除索引请求
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book");
    //        执行
    AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
    //得到相应
    boolean acknowledged = delete.isAcknowledged();
    System.out.println("acknowledged:" + acknowledged);

}

5、异步删除索引

//    删除索引异步操作
@Test
public void testDeleteIndexAsync() throws IOException {
    //创建删除索引请求
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book");
    //        执行
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
        @Override
        public void onResponse(AcknowledgedResponse acknowledgedResponse) {
            //得到相应
            boolean acknowledged = acknowledgedResponse.isAcknowledged();
            System.out.println("acknowledged:" + acknowledged);
        }

        @Override
        public void onFailure(Exception e) {
            e.printStackTrace();
        }
    };
//        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

}

6、索引是否存在

//index exist api
@Test
public void testExistIndex() throws IOException {
    GetIndexRequest request=new GetIndexRequest("itheima_book");
    //参数
    request.local(false);//从主节点返回本地索引信息状态
    request.humanReadable(true);//以适合人类的格式返回
    request.includeDefaults(false);//是否返回每个索引的所有默认配置

    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println("exists:"+exists);

}

7、关闭索引

//关闭索引
@Test
public void testCloseIndex() throws IOException {
    CloseIndexRequest request=new CloseIndexRequest("itheima_book");

    AcknowledgedResponse close = client.indices().close(request, RequestOptions.DEFAULT);

    boolean acknowledged = close.isAcknowledged();
    System.out.println("acknowledged:"+acknowledged);
}

8、开启索引

//开启索引
@Test
public void testOpenIndex() throws IOException {
    OpenIndexRequest request=new OpenIndexRequest("itheima_book");

    OpenIndexResponse open = client.indices().open(request, RequestOptions.DEFAULT);

    boolean acknowledged = open.isAcknowledged();
    System.out.println("acknowledged:"+acknowledged);

}