• ElasticSearch批处理


      

          在刚才的新增当中,我们是一次新增一条数据。那么如果你将来的数据库里有数千上万的数据,你一次新增一个,那得多麻烦。所以我们还要学习一下批量导入功能。

            也就是说批量的把数据库的数据写入索引库。那这里的需求是,首先利用mybatisplus去批量查询酒店数据,然后将查询到的酒店数据,也就是hotel把它转换成我们的hotelDoc文档类型。最后再利用我们的这个Bulk批处理方式实现批量新增:

    1. @Test
    2. void testCreateHotelIndex() throws IOException{
    3. //创建索引库 CreateIndexReqeust
    4. //1、创建Request对象:
    5. CreateIndexRequest request = new CreateIndexRequest("hotel");
    6. //2、请求参数:
    7. request.source(MAPPING_TEMPLATE, XContentType.JSON);
    8. //3、发送请求
    9. client.indices().create(request,RequestOptions.DEFAULT);
    10. }
    11. //1、创建DeleteIndexRequset
    12. DeleteIndexReqeust reqeust = new DeleteIndexReqeust("hotel");
    13. client.indices().delete(reqeust. ReqeustOptions.DEFAULT);
    14. //判读索引库是否存在:
    15. GetIndexReqeust request = new GetIndexRequest("hotel");
    16. boolean exists = client.indices().exists(reqeust, RequestOptions.DEFAULT);
    17. @Test
    18. void testBulkRequest() throws IOException{
    19. List hotels = hoteService.list();
    20. BulkRequest request = new BulkRequest();
    21. for(Hotel hotel : hotels){
    22. HotelDoc hotelDoc = new HotelDoc(hotel);
    23. request.add(new IndexRequest("hotel"))
    24. .id(hotelDoc.getId().toString())
    25. .source(JSON.toJSONString(hotelDoc), XContentType.JSON);
    26. }
    27. // 发送请求
    28. client.bulk(request,RequestOptions.DEFAULT);
    29. }

    用Stream+Map转换更优雅:

    1. @Test
    2. void testBulkRequest() throws IOException {
    3. List hotels = hoteService.list();
    4. BulkRequest request = new BulkRequest();
    5. hotels.stream()
    6. .map(hotel -> new HotelDoc(hotel))
    7. .forEach(hotelDoc -> {
    8. try {
    9. request.add(new IndexRequest("hotel")
    10. .id(hotelDoc.getId().toString())
    11. .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
    12. } catch (IOException e) {
    13. // Handle exception
    14. e.printStackTrace();
    15. }
    16. });
    17. client.bulk(request, RequestOptions.DEFAULT);
    18. }

    其实是把多个 IndexRequest的请求合并到BulkRequest 对象里,然后一次性完成提交,这种就叫批处理,确实没有什么新东西, 就是把以前的那种新增的代码给它合到一起去提交了。

    接下来我们就来学习第一种全文检索查询。全文检索查询它会对用户输入的内容做分词以后进行匹配。比较常见的用于这个搜索框的这种搜索

    match和multi_match的区别是什么?一个是根据单字段查询,一个是根据多字段。而multi_match参与查询的字段越多,性能越差,建议利用copy to把多个要查的字段拷贝到一个字段当中

  • 相关阅读:
    SpringCloud Alibaba(七) - JWT(JSON Web Token)
    TPD4E05U06DQAR功能和参数及如何正确安装使用
    C#泛型
    Oracle/PLSQL: BFilename Function
    倒序数(模拟)
    【Qt常用关键字知识拓展】
    2-FreeRTOS编码标准、风格指南
    生成式AI:未来的发展方向是什么?
    jemter使用和优化
    长事务管理不再难:Saga模式全面解析
  • 原文地址:https://blog.csdn.net/weixin_53676834/article/details/138173053