• 【Java】RestClient的使用


    RestClient的使用

    先导入Maven坐标,要和elasticsearch和kibana的版本保持一致

            <dependency>
                <groupId>org.elasticsearch.clientgroupId>
                <artifactId>elasticsearch-rest-high-level-clientartifactId>
                <version>7.12.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加一个Index索引

        void createHotelIndex() throws IOException {
            //创建Request对象
            CreateIndexRequest request = new CreateIndexRequest("hotel");
            //准备请求参数,DSL语句
            request.source(MAPPING_TEMPLATE, XContentType.JSON);
            //发送请求
            client.indices().create(request, RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    删除一个index索引

        void DeleteHotelIndex() throws IOException {
            //创建对象
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("hotel");
            //发送请求
            client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    判断一个index索引是否存在

        void ExitHotel() throws IOException {
            GetIndexRequest request=new GetIndexRequest("hotel");
            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
            System.err.println(exists?"索引已经存在":"索引不存在");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    初始化

    几乎所有的操作都依赖于RestHighLevelClient这个对象

    private RestHighLevelClient client;
    
        void setRestHighLevelClient(){
            this.client=new RestHighLevelClient(RestClient.builder(
                    HttpHost.create("http://ip:9200")
            ));
        }
        
        //销毁client
        @AfterEach
        void tearDown() throws IOException {
            this.client.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    添加一个doc文档

        void testAddDocument() throws IOException {
            Hotel hotel = hotelService.getById(61083L);
            //转换为文档类型
            HotelDoc hotelDoc=new HotelDoc(hotel);
    
            //准备request对象
            IndexRequest request=new IndexRequest("hotel").id(hotelDoc.getId().toString());
            //准备JSON文档
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    获取一个doc文档

        void testGetDocumentById() throws IOException {
            //准备请求
            GetRequest request=new GetRequest("hotel","61083");
            //发送请求,得到响应
            GetResponse response = client.get(request, RequestOptions.DEFAULT);
            //拿到数据
            String json = response.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            System.out.println(hotelDoc);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    局部更新文档

        void testUpdateDocument() throws IOException {
            UpdateRequest request=new UpdateRequest("hotel","61083");
            //准备发送请求
            request.doc(
                    "price","123456"
            );
            //发送请求
            client.update(request,RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    删除文档

        void DeleteDocument() throws IOException {
            DeleteRequest request=new DeleteRequest("hotel","61083");
            client.delete(request,RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4

    批量添加doc文档

        void BulkRequest() throws IOException {
            List<Hotel> hotels = hotelService.list();
            BulkRequest request=new BulkRequest();
            for (Hotel hotel:hotels){
                HotelDoc hotelDoc=new HotelDoc(hotel);
                request.add(new IndexRequest("hotel")
                        .id(hotelDoc.getId().toString())
                        .source(JSON.toJSONString(hotelDoc),XContentType.JSON)
                );
            }
            //发送请求
            client.bulk(request,RequestOptions.DEFAULT);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

  • 相关阅读:
    Java学习总结(答案版)
    FreqBlender: Enhancing DeepFake Detection by Blending Frequency Knowledge
    vscode使用remote-ssh免密连接服务器
    数据结构篇_编程思想板块_第一章顺序表和链表
    代码随想录笔记_动态规划_416分割等和子集
    JS中==操作符的强制类型转换规定
    SpringCloud的新闻资讯项目02--- app端文章查看,静态化freemarker,分布式文件系统minIO
    PyCharm:2023新版PyCharm无UI工具栏,如何回旧版
    云原生之K8s的亲和、反亲和、污点与容忍
    redis的原理和源码-数据过期expire的介绍
  • 原文地址:https://blog.csdn.net/2301_79516932/article/details/136262823