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

Elasticsearch中RestClient使用

🍓 简介:java系列技术分享(👉持续更新中…🔥)
🍓 初衷:一起学习、一起进步、坚持不懈
🍓 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正🙏
🍓 希望这篇文章对你有所帮助,欢迎点赞 👍 收藏 ⭐留言 📝

🍓 更多文章请点击
在这里插入图片描述在这里插入图片描述

简介及安装请查看这篇:Elasticsearch中倒排索引、分词器、DSL语法使用介绍

文章目录

  • 一、RestClient操作索引库
  • 二、初始化JavaRestClient
    • 2.1 引入依赖
    • 2.2 初始化RestHighLevelClient
  • 三、索引库操作
    • 3.1 创建
    • 3.2 删除
    • 3.3 判断索引库是否存在
  • 四、文档操作
    • 4.1 新增文档
    • 4.2 根据id查询数据
    • 4.3 根据id修改数据
    • 4.4 删除数据
    • 4.5 批量新增
  • 五、DSL语法
  • 六、拼音分词
    • 6.1 安装

一、RestClient操作索引库

这些客户端的本质就是组装DSL语句,通过Http请求发送给ES,官方地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
在这里插入图片描述

各种操作查看下方文档在这里插入图片描述

二、初始化JavaRestClient

具体使用还需查看对应文档,这里简单使用介绍,可能不全

2.1 引入依赖

        <dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version></dependency>

2.2 初始化RestHighLevelClient

第一种

   @Beanpublic RestHighLevelClient restHighLevelClient(){return new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));}

第二种

spring:elasticsearch:rest:uris: localhost:9200

三、索引库操作

建议对应上篇中的DSL语句进行操作

3.1 创建

	@Autowiredprivate RestHighLevelClient client;//创建索引库@Testpublic void testCreateHotelIndex() throws IOException {//1.创建Request对象CreateIndexRequest request=new CreateIndexRequest("hotel");//2.请求参数,MAPPING_TEMPLATE是静态常量字符串,内容是创建索引库的DSL语句request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);//3.发送请求client.indices().create(request,RequestOptions.DEFAULT);}

3.2 删除

    //删除索引库@Testpublic void testDeleteHotelIndex() throws IOException {//创建Request对象DeleteIndexRequest request= new DeleteIndexRequest("hotel");//发送请求client.indices().delete(request,RequestOptions.DEFAULT);}

3.3 判断索引库是否存在

    @Testpublic void  testExistsHotelIndex() throws IOException {//创建request对象GetIndexRequest request= new GetIndexRequest("hotel");//发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);//输出System.out.println(exists ? "索引库已经存在":"索引库不存在");}

四、文档操作

4.1 新增文档

@Autowired
private RestHighLevelClient client;@Test
public void testAddDocument() throws IOException {//1.根据id查询酒店数据Hotel hotel = service.getById(61073l);//2.转换为文档类型HotelDoc hotelDoc = new HotelDoc(hotel);//3.将HotelDoc转为jsonString json = JSON.toJSONString(hotelDoc);//4.准备request对象IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());//5.准备json文档request.source(json, XContentType.JSON);//6.发送请求client.index(request, RequestOptions.DEFAULT);
}

4.2 根据id查询数据

@Autowired
private RestHighLevelClient client;@Test
public void testGetDocumentById() throws IOException {//1.准备request对象GetRequest request = new GetRequest("hotel" ,"61083");//2.发送请求,得到响应GetResponse response = client.get(request, RequestOptions.DEFAULT);//3.解析响应结果String json = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}

4.3 根据id修改数据

@Autowired
private RestHighLevelClient client;@Test
public void testUpdateDocument() throws IOException {//1.准备requestUpdateRequest request=new UpdateRequest("hotel","61083");//2.准备请求参数request.doc("price","987","starName","四钻");//3.发送请求

4.4 删除数据

@Autowired
private RestHighLevelClient client;@Test
public void testDeleteDocument() throws IOException {//1.准备RequestDeleteRequest request=new DeleteRequest("hotel","61083");//2.发送请求client.delete(request,RequestOptions.DEFAULT);
}

4.5 批量新增


@Autowired
private RestHighLevelClient client;@Test
public void testBulkRequest() throws IOException {//批量查询酒店数据List<Hotel> hotels = service.list();//1.创建request对象BulkRequest request = new BulkRequest();//2.准备参数for (Hotel hotel : hotels) {//.转换文档类型HotelDoc doc = new HotelDoc(hotel);//.创建新增文档的request对象request.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(JSON.toJSONString(doc), XContentType.JSON));}//3.发送请求client.bulk(request,RequestOptions.DEFAULT);
}

五、DSL语法

个人介绍可能不太详细,查询及结果解析具体使用请查看下方文档
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

在这里插入图片描述

六、拼音分词

想要实现如下,根据拼音也能查到对应数据 那么需要安装拼音分词器

在这里插入图片描述

6.1 安装

可以在该文档下载拼音分词器或者在我的资源库进行下载

  1. 根据第一篇的Elasticsearch简介及安装安装我们知道,我是通过docker安装,挂载有数据卷,那么首先查看安装位置

    docker volume inspect es-plugins
    

    在这里插入图片描述找到对应位置进行安装
    在这里插入图片描述

  2. 重启容器

    	# 4、重启容器docker restart es
    
  3. 测试
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

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

相关文章:

  • 【LeetCode-中等题】208. 实现 Trie (前缀树)
  • python队列与多线程——生产者消费者模型
  • idea的安装
  • Unity下如何实现RTMP或RTSP播放端录像?
  • 【Python】Python基础语法
  • I2C总线驱动:裸机版、应用层的使用、二级外设驱动三种方法
  • Unix Network Programming Episode 77
  • 解决Ubuntu无法安装pycairo和PyGObject
  • Android Handler 机制解析
  • 酒店固定资产管理怎么分类
  • OpenCV(三十一):形态学操作
  • Python之面向对象(二)
  • ESP32用作经典蓝牙串口透传模块与手机进行串口通信
  • Python 操作 CSV
  • 自动化运维工具Ansible教程(二)【进阶篇】
  • 嵌入式Linux基础学习笔记目录
  • JVM | 垃圾回收器(GC)- Java内存管理的守护者
  • yolov5添加ECA注意力机制
  • 华为在高端智能手机市场再次撕开了一道深深的口子
  • 前端设计模式和设计原则之设计模式
  • 提高在速卖通产品上的曝光率——自养号测评优势全面解析!
  • 指针进阶(二)
  • 【HCIE】03.BGP高级特性
  • 单个处理数据祖籍列表层级关系
  • Maven部署打包多环境(开发、测试、生产)配置教程
  • 【计算思维题】少儿编程 蓝桥杯青少组计算思维 数学逻辑思维真题详细解析第9套
  • 【Hello Algorithm】贪心算法
  • TOP-K问题
  • 【保姆级从0到1】UE5 蓝图入门教程1:关卡、蓝图入门
  • 【码银送书第六期】《ChatGPT原理与实战:大型语言模型的算法、技术和私有化》