当前位置: 首页 > news >正文

elasticsearch(RestHighLevelClient API操作)(黑马)

操作全是换汤不换药,创建一个request,然后使用client发送就可以了

一、增加索引库数据

    @Testvoid testAddDocument() throws IOException {//从数据库查出数据Writer writer = writerService.getById(199);//将查出来的数据处理成json字符串String json = JSON.toJSONString(writer);// 1.准备RequestIndexRequest request = new IndexRequest("writer").id(String.valueOf(writer.getId()));// 2.准备请求参数DSL,其实就是文档的JSON字符串request.source(json, XContentType.JSON);// 3.发送请求client.index(request, RequestOptions.DEFAULT);}

二、查询索引库顺序

    @Testvoid testGetDocumentById() throws IOException {// 1.准备Request      // GET /hotel/_doc/{id}GetRequest request = new GetRequest("writer", "199");// 2.发送请求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 3.解析响应结果String json = response.getSourceAsString();Writer writer = JSON.parseObject(json, Writer.class);System.out.println("writerDoc = " + writer);}

三、删除索引库数据

@Testvoid testDeleteDocumentById() throws IOException {// 1.准备Request      // DELETE /hotel/_doc/{id}DeleteRequest request = new DeleteRequest("hotel", "61083");// 2.发送请求client.delete(request, RequestOptions.DEFAULT);}

四、修改索引库数据

@Testvoid testUpdateById() throws IOException {// 1.准备RequestUpdateRequest request = new UpdateRequest("hotel", "61083");// 2.准备参数request.doc("price", "870");// 3.发送请求client.update(request, RequestOptions.DEFAULT);}

五、批量导入数据

@Testvoid testBulkRequest() throws IOException {List<Writer> list = writerService.list();BulkRequest request = new BulkRequest();for (Writer writer : list) {HotelDoc hotelDoc = new HotelDoc(hotel);String json = JSON.toJSONString(writer);request.add(new IndexRequest("writer").id(String.valueOf(writer.getId())).source(json, XContentType.JSON));}client.bulk(request, RequestOptions.DEFAULT);}

http://www.lryc.cn/news/319952.html

相关文章:

  • 用尾插的思想实现移除链表中的元素
  • 【Kubernetes】k8s删除master节点后重新加入集群
  • HCIP—OSPF虚链路实验
  • RAxML-NG安装与使用-raxml-ng-v1.2.0(bioinfomatics tools-013)
  • Tomcat内存马
  • pytorch之诗词生成3--utils
  • OpenAI的ChatGPT企业版专注于安全性、可扩展性和定制化。
  • JS06-class对象
  • 深度学习1650ti在win10安装pytorch复盘
  • Node.js与webpack(三)
  • 测试覆盖率那些事
  • Etcd 介绍与使用(入门篇)
  • Docker 安装 LogStash
  • Selenium笔记
  • ChatGPT :确定性AI源自于确定性数据
  • linux驱动开发面试题
  • 【AI】Ubuntu系统深度学习框架的神经网络图绘制
  • AI推介-大语言模型LLMs论文速览(arXiv方向):2024.03.05-2024.03.10—(2)
  • AI解答——DNS、DHCP、SNMP、TFTP、IKE、RIP协议
  • 【TypeScript系列】声明合并
  • zookeeper基础学习之六: zookeeper java客户端curator
  • MySQL数据库操作学习(2)表查询
  • Java学习
  • C#八皇后算法:回溯法 vs 列优先法 vs 行优先法 vs 对角线优先法
  • springboot整合swagger,postman,接口规范
  • 029—pandas 遍历行非向量化修改数据
  • 相机安装位置固定后开始调试设备供电公司推荐使用方法
  • AI视频批量混剪系统|罐头鱼AI视频矩阵获客
  • 线程池学习-了解,自定义线程池
  • CentOS7.9 安装SIPp3.6