zoukankan      html  css  js  c++  java
  • elasticsearch RestHighLevelClient 关于index的常用操作

    对于索引的操作是基于***IndexRequest来进行操作的。例如:CreateIndexRequest、DeleteIndexRequest、GetIndexRequest

    常见操作中还有校验索引是否存在:exists


    创建连接请求:

      

        private RestHighLevelClient client;
    
        //创建连接
        public ElasticsearchController() {
            RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost" , 9200 , "http")
                    )
            );
    
            this.client = restHighLevelClient;
        }

    创建索引:

        public String createIndex(){
    
            //create es index
            CreateIndexRequest requestUser = new CreateIndexRequest("usertestinfo");
            CreateIndexResponse createIndexResponse = null;
            try {
                createIndexResponse = this.client.indices().create(requestUser, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //get es index create result is true or false
            boolean acknowledged = false;
            try {
                acknowledged = createIndexResponse.isAcknowledged();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "Localhost Elasticsearch Create Index is1 : " + acknowledged;
        }

    查找索引:

        public String findIndex(){
    
            GetIndexRequest request = new GetIndexRequest("user");
            HashMap<String, Object> result = new HashMap<>();
            try {
                GetIndexResponse getIndexResponse = this.client.indices().get(request, RequestOptions.DEFAULT);
                Map<String, Settings> settings = getIndexResponse.getSettings();
    
                System.out.println("index settings is :" + settings.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "find es index complete";
        }

    删除索引:

        public String deleteIndex(){
            try {
                DeleteIndexRequest usertestinfo = new DeleteIndexRequest("user");
                AcknowledgedResponse delete = this.client.indices().delete(usertestinfo, RequestOptions.DEFAULT);
                System.out.println("delete index result is : " + delete.isAcknowledged());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "delete es index complete";
        }

    检验索引是否存在:

        //检验某条索引是否存在
        public String checkIndexIsExists(){
    
            GetIndexRequest request = new GetIndexRequest("user");
            boolean exists = false;
            try {
                exists = this.client.indices().exists(request , RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "exists es index complete , result is :" + exists;
        }
  • 相关阅读:
    高并发核心技术
    2年java,蚂蚁一面,卒
    Spring Boot 实战 入门
    spring-boot-plus后台快速开发脚手架之代码生成器使用
    spring-boot-plus后台快速开发框架1.0.0.RELEASE发布了
    spring-boot-plus后台快速开发框架1.0.0.RELEASE发布了
    Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
    从尾到头打印单向链表
    单向链表操作
    合并两个排序的数组
  • 原文地址:https://www.cnblogs.com/hxjz/p/14701684.html
Copyright © 2011-2022 走看看