首页 > 其他 > 详细

elasticsearch RestHighLevelClient 关于index的常用操作

时间:2021-04-25 23:46:00      阅读:25      评论:0      收藏:0      [点我收藏+]

 

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

创建连接请求:

  

    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";
    }

 

elasticsearch RestHighLevelClient 关于index的常用操作

原文:https://www.cnblogs.com/hxjz/p/14701684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!