ES分布式搜索引擎-RestClient操作索引库

ES分布式搜索引擎-RestClient操作索引库

文章目录

一、利用JavaRestClient实现文档的CRUD

  1. 初始化JavaRestClient

    java 复制代码
    package cn.mannor.hotel;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.CreateIndexRequest;
    import org.elasticsearch.client.indices.GetIndexRequest;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    
    import static cn.mannor.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
    
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
    
    
        /**
         * 初始化测试环境,创建并配置RestHighLevelClient实例。
         */
        @BeforeEach
        void setUp() {
            this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                    HttpHost.create("http://192.168.12.131:9200") // 设置Elasticsearch的地址和端口
            ));
        }
    
        /**
         * 测试结束后清理资源,关闭RestHighLevelClient实例。
         *
         * @throws IOException 如果关闭客户端时发生IO错误
         */
        @AfterEach
        void tearDown() throws IOException {
            this.restHighLevelClient.close(); // 关闭高级REST客户端,释放资源
        }
    }

!!!:

值得注意的是,由于document中的数据类型与数据库中数据的类型有所不同,比如经纬度---> 数据,所以就需要准备一个专门接受处理过的一个模型,注意新增数据是的代码编写方式。

java 复制代码
package cn.mannor.hotel.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class HotelDoc {
 private Long id;
 private String name;
 private String address;
 private Integer price;
 private Integer score;
 private String brand;
 private String city;
 private String starName;
 private String business;
 private String location;
 private String pic;

 public HotelDoc(Hotel hotel) {
     this.id = hotel.getId();
     this.name = hotel.getName();
     this.address = hotel.getAddress();
     this.price = hotel.getPrice();
     this.score = hotel.getScore();
     this.brand = hotel.getBrand();
     this.city = hotel.getCity();
     this.starName = hotel.getStarName();
     this.business = hotel.getBusiness();
     this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
     this.pic = hotel.getPic();
 }
}
  1. 利用JavaRestClient新增酒店数据

    java 复制代码
    @SpringBootTest
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        @Autowired
        private IHotelService iHotelService;
    
        @Test
        void addDocumentTest() throws IOException {
            Hotel hotel = iHotelService.getById(45845L); //获取数据
            HotelDoc hotelDoc = new HotelDoc(hotel);//处理文档中不同的数据类型
            //1.准备request对象
            IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
            //2.准备JOSN文档(由JSON转换过来)
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            //3.发送请求
            restHighLevelClient.index(request, RequestOptions.DEFAULT);
        }
    }
  2. 利用JavaRestClient根据id查询酒店数据

    java 复制代码
    @SpringBootTest
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        @Autowired
        private IHotelService iHotelService;
    
        @Test
        void getDocumentByIdTest() throws IOException {
    
            //1.准备request对象
            GetRequest request = new GetRequest("hotel", "45845");
    
            //2.发送请求,得到响应
            GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
            //3.解析响应结果
            String json = response.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            System.out.println(hotelDoc);
        }
    
    }
  3. 利用JavaRestClient删除酒店数据

    java 复制代码
    @SpringBootTest
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        @Autowired
        private IHotelService iHotelService;
    
        @Test
        void updateDocumentByIdTest() throws IOException {
    
            //1.准备request对象
            UpdateRequest request = new UpdateRequest("hotel", "45845");
            //2.准备文档,键值对形式
            request.doc(
                    "age", 18,
                    "name", "曼诺尔雷迪亚兹"
            );
            //3.发送请求
            restHighLevelClient.update(request, RequestOptions.DEFAULT);
        } 
    }
  4. 利用JavaRestClient修改酒店数据

    java 复制代码
    @SpringBootTest
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        @Autowired
        private IHotelService iHotelService;
    
        @Test
        void deleteDocumentByIdTest() throws IOException {
    
            //1.准备request对象
           DeleteRequest request = new DeleteRequest("hotel", "45845");
    
            //2.发送请求
            restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        }
    }

二、使用JavaRestClient批量导入数据到ES

  1. 利用mybatis-plus查询酒店数据

  2. 将查询到的酒店数据(Hotel)转换为文档类型数据(HotelDoc)

  3. 利用JavaRestClient中的Bulk批处理,实现批量新增文档,示例代码如下

    java 复制代码
    @SpringBootTest
    public class HotelDocumentTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        @Autowired
        private IHotelService iHotelService;
    
        @Test
        void bulkDocumentTest() throws IOException {
    
            List<Hotel> hotels = iHotelService.list();
            //1.准备request对象
            BulkRequest request = new BulkRequest();
            //2.准备参数,添加多个Request
            for (Hotel hotel : hotels) {
                HotelDoc hotelDoc = new HotelDoc(hotel);
                request.add(new IndexRequest("hotel")
                        .id(hotelDoc.getId().toString())
                        .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
            }
            //3.发送请求
            restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        }
    }
相关推荐
极限实验室15 小时前
搜索百科(1):Lucene —— 打开现代搜索世界的第一扇门
搜索引擎·lucene
努力的小郑1 天前
从一次分表实践谈起:我们真的需要复杂的分布式ID吗?
分布式·后端·面试
Elasticsearch1 天前
平衡尺度:利用权重使倒数排序融合 (RRF) 更加智能
elasticsearch
AAA修煤气灶刘哥2 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
muyun28002 天前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
程序消消乐2 天前
Kafka 入门指南:从 0 到 1 构建你的 Kafka 知识基础入门体系
分布式·kafka
智能化咨询2 天前
Kafka架构:构建高吞吐量分布式消息系统的艺术——进阶优化与行业实践
分布式·架构·kafka
Chasing__Dreams2 天前
kafka--基础知识点--5.2--最多一次、至少一次、精确一次
分布式·kafka