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

使用springboot对Elasticsearch 进行索引的增、删、改、查

一 SpringBoot + Elasticsearch 项目环境搭建

1.1 修改pom文件添加依赖

目前使用spring-boot-starter-parent版本为2.2.8.RELEASE

对应spring-data-elasticsearch版本为2.2.8.RELEASE,版本对应可以自行百度,如果不行直接用elasticsearch-rest-high-level-client工具类吧

      <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>2.2.8.RELEASE</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.5.0</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.5.0</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.5.0</version></dependency>
1.2 新建配置文件
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** ES配置类** @author lc* @version 1.0* @date 2022/3/25 10:53*/
@Configuration
public class ElasticSearchClientConfig {@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.100", 9200, "http")));return client;}
}

二 RestHighLevelClient的使用

RestHighLevelClient是Elasticsearch 的操作方法,我们先进行引用吧。

@Autowired
private RestHighLevelClient client;

1、创建索引

 @Testvoid testCreateIndex() throws IOException {//1 创建索引请求CreateIndexRequest request = new CreateIndexRequest("zlc_index");//2 客户端执行请求CreateIndexResponse createIndexResponse =client.indices().create(request, RequestOptions.DEFAULT);System.out.println(createIndexResponse);}

2、索引是否存在

@Testvoid testExistIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("zlc_index");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);}

3、删除索引

@Testvoid testDeleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println(delete.isAcknowledged());}

4、添加文档

@Testvoid testAddDocument() throws IOException {//创建对象UserES user = new UserES();user.setUserName("suwerw");user.setUserPhone("178245774");//创建请求IndexRequest request = new IndexRequest("zlc_index");//规则 put /zlc_index/_doc/1request.id("1");request.timeout(TimeValue.timeValueSeconds(1));request.timeout("1s");//将数据放入请求request.source(JSON.toJSONString(user), XContentType.JSON);//客户端发送请求,获取响应结果IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);System.out.println(indexResponse.toString());System.out.println(indexResponse.status());}

5、判断文档是否存在

@Testvoid testIsExists() throws IOException {GetRequest getRequest = new GetRequest("zlc_index", "1");//不获取返回的 _source 的上下文,提高效率getRequest.fetchSourceContext(new FetchSourceContext(false));getRequest.storedFields("_none_");boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);System.out.println(exists);}

6、获取文档

 @Testvoid testGetDocument() throws IOException {GetRequest getRequest = new GetRequest("zlc_index", "1");GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);System.out.println(getResponse);System.out.println(getResponse.getSourceAsString());}

7、更新文档信息

 @Testvoid testUpdateDocument() throws IOException {UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");updateRequest.timeout("1s");UserES user = new UserES();user.setUserName("Zhou_LC");user.setUserPhone("233669");updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);System.out.println(updateResponse);System.out.println(updateResponse.status());}

8、删除文档

  @Testvoid testDeleteDocument() throws IOException {DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");deleteRequest.timeout("1s");DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println(deleteResponse);System.out.println(deleteResponse.status());}
http://www.lryc.cn/news/213690.html

相关文章:

  • Project#2: Extendible Hash Index
  • Emscripten + CMakeLists.txt 将 C++ 项目编译成 WebAssembly(.wasm)/js,并编译 Html 测试
  • MATLAB中preparets函数用法
  • ARM 版 OpenEuler 22.03 部署 KubeSphere v3.4.0 不完全指南续篇
  • react官网
  • 前端css介绍
  • MySql创建索引
  • 前后端分离vue+springboot家庭理财账单财务管理系统
  • LeetCode:2003. 每棵子树内缺失的最小基因值(C++)
  • React Hooks之useContext使用
  • 多模态对比语言图像预训练CLIP:打破语言与视觉的界限
  • 使用s3cmd访问S3存储 -【真实案例】
  • 51单片机复位电容计算与分析(附带Proteus电路图)
  • 前端性能瓶颈崩溃项目?Webpack助力解决!
  • 纷享销客BI,助力企业激活数据价值,科学企业决策
  • SpringBoot整合阿里云OSS对象存储
  • 【ES专题】ElasticSearch快速入门
  • 案例分析真题-质量属性
  • 微信小程序面试题之理论篇
  • C++前缀和算法的应用:统计上升四元组
  • 华泰证券:新奥能源:零售气待恢复,泛能与智家仍是亮点
  • FPGA与ASIC有什么差异?二者该如何选用?
  • Kotlin run 用法
  • iZotope RX 10(音频修复和增强工具)
  • MES 价值点之数据追随
  • yolo8制作自己的数据集训练和预测分割
  • 分享一下怎么做一个同城配送小程序
  • Qt 中model/View 架构 详解,以及案例实现相薄功能
  • 提高微星笔记本Linux下散热性能,MSI-EC 驱动新补丁发布
  • Apache Doris (五十): Doris表结构变更-动态分区(2)