Elasticsearch学习3-RestClient

1、RestClient 介绍

JavaREST客户端有两种模式:

Java Low Level REST Client:ES官方的低级客户端。低级别的客户端通过http与Elasticearch集群通信。

Java High Level REST Client:ES官方的高级客户端。基于上面的低级客户端,也是通过HTTP与ES集群进行通信。它提供了更多的接口。

此外Spring也对RestClient进行了封装,本文主要介绍Java High Level REST Client的使用

2、使用RestClient操作索引

2.1创建链接

java 复制代码
//创建ES连接对象
        String esAuth = Base64.encodeBase64String(("test_user" + ":" + "test_user").getBytes());
        RestClientBuilder builder = RestClient.builder(new HttpHost("test01.es.58dns.org", 9299, "http"))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout(13000)
                        .setSocketTimeout(15000)
                        .setConnectionRequestTimeout(15000));
        builder.setDefaultHeaders(new BasicHeader[]{new BasicHeader("Authorization", "Basic " + esAuth)});
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        System.out.println(restHighLevelClient);

2.2创建索引库

下方代码仅创建了索引库,没有指定映射mapping

可通过request.source("映射字符串",XContentType.JSON)指定映射字符串

java 复制代码
// 创建索引
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("user2");
        CreateIndexResponse response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        //判断是否创建索引成功
        System.out.println(acknowledged);

2.3 查询索引信息、判断索引库是否存在

java 复制代码
        //查询索引
        GetIndexRequest getIndexRequest = new GetIndexRequest("user2");
        GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println("getIndexResponse.getAliases() = " + getIndexResponse.getAliases());
        System.out.println("getIndexResponse.getMappings() = " + getIndexResponse.getMappings());
        System.out.println("getIndexResponse.getSetting() = " + getIndexResponse.getSettings());
        //判断索引是否存在
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println("exists = " + exists);

2.4删除索引,判断是否删除成功

java 复制代码
//删除索引
 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user2");
 AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //判断是否删除成功
System.out.println("delete.isAcknowledged() = " + delete.isAcknowledged());

3、进行文档操作

java 复制代码
//新增数据
        User user = new User();
        user.setName("小李");
        user.setAge(12);
        user.setPassword("123321");
        //转化为json串
        ObjectMapper objectMapper = new ObjectMapper();
        String userString = objectMapper.writeValueAsString(user);
        //创建请求
        IndexRequest indexRequest = new IndexRequest();
        //指定索引库名称和id
        indexRequest.index("user").id("1");
        //指定索引类型
        indexRequest.type("_doc");
        //指定要添加的内容
        indexRequest.source(userString, XContentType.JSON);
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("index.getId() = " + index.getId());

        //查询文档
        GetRequest getIndexRequest1 = new GetRequest();
        getIndexRequest1.index("user").id("1");
        GetResponse getIndexResponse1 = restHighLevelClient.get(getIndexRequest1,RequestOptions.DEFAULT);
        String sourceAsString = getIndexResponse1.getSourceAsString();
        System.out.println("sourceAsString = " + sourceAsString);


        //修改文档
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("user").id("1");
        updateRequest.doc("name","129212","age",20);
        updateRequest.type("_doc");
        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println("update.getGetResult() = " + update.getGetResult());
        //再次查询文档
        GetRequest getIndexRequest2 = new GetRequest();
        getIndexRequest2.index("user").id("1");
        getIndexRequest2.type("_doc");
        GetResponse getIndexResponse2 = restHighLevelClient.get(getIndexRequest2,RequestOptions.DEFAULT);
        String sourceAsString2 = getIndexResponse2.getSourceAsString();
        System.out.println("sourceAsString2 = " + sourceAsString2);
//删除文档
DeleteRequest deleteRequest1 = new DeleteRequest();
        deleteRequest1.index("user").id("1");
        restHighLevelClient.delete(deleteRequest1,RequestOptions.DEFAULT);
相关推荐
懒惰的bit2 小时前
基础网络安全知识
学习·web安全·1024程序员节
Natural_yz5 小时前
大数据学习09之Hive基础
大数据·hive·学习
龙中舞王5 小时前
Unity学习笔记(2):场景绘制
笔记·学习·unity
Natural_yz5 小时前
大数据学习10之Hive高级
大数据·hive·学习
AI服务老曹5 小时前
建立更及时、更有效的安全生产优化提升策略的智慧油站开源了
大数据·人工智能·物联网·开源·音视频
Mephisto.java5 小时前
【大数据学习 | HBASE高级】storeFile文件的合并
大数据·sql·oracle·json·hbase·database
这样の我5 小时前
hbase集成phoenix
大数据·数据库·hbase
love_and_hope6 小时前
Pytorch学习--神经网络--完整的模型训练套路
人工智能·pytorch·python·深度学习·神经网络·学习
思通数据6 小时前
AI与OCR:数字档案馆图像扫描与文字识别技术实现与项目案例
大数据·人工智能·目标检测·计算机视觉·自然语言处理·数据挖掘·ocr
CoderJia程序员甲6 小时前
重学SpringBoot3-整合 Elasticsearch 8.x (三)使用Repository
java·大数据·spring boot·elasticsearch