Elasticsearch - SpringBoot 索引与文档相关demo

2024-09-03 16:04

本文主要是介绍Elasticsearch - SpringBoot 索引与文档相关demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
    • Elasticsearch - SpringBoot 索引与文档相关demo
      • 1. _cat/* 相关API
      • 2. 查看所有索引
      • 3. 创建索引
      • 4. 检索索引
      • 5. 删除索引
      • 6. 新增文档
      • 7. 更新文档
      • 8. 根据索引中的文档 ID 查询文档
      • 9. 删除文档

前言

  如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
  而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!


Elasticsearch - SpringBoot 索引与文档相关demo

1. _cat/* 相关API

示例:

GET /_cat/indices/yzy-*-20240830?v&h=index,pri.store.size&s=ss:desc&bytes=gb

在这里插入图片描述
含义:

GET /_cat/indices/yzy-*-20240830:请求 _cat/indices API,获取匹配模式为 yzy-*-20240830 的索引信息。
v:表示显示表头(列名)。
h=index,pri.store.size:指定要显示的列,index 是索引名称,pri.store.size 是主分片的存储大小。
s=ss:desc:按存储大小(ss)降序排序。
bytes=gb:将存储大小以 GB 为单位显示。

代码示例:

    @GetMapping("/api/index_cat")public String getIndexCat()   {try {// 构建 Cat Indices 请求Request request = new Request("GET", "/_cat/indices/yzy-*-20240830?v&h=index,pri.store.size&s=ss:desc&bytes=gb");// 执行请求Response response = client.getLowLevelClient().performRequest(request);// 处理响应String responseBody = EntityUtils.toString(response.getEntity());log.info(responseBody);return responseBody;} catch (ResponseException e) {log.error("Error executing request: " + e.getMessage());} catch (IOException i){log.error("错误 ",i);}return null;}

2. 查看所有索引

代码示例:

    @GetMapping("/api/index_all")public String getAllIndex()   {try {// 创建索引请求Request request = new Request("GET", "/_cat/indices?v");// 执行请求Response response = client.getLowLevelClient().performRequest(request);String msg = EntityUtils.toString(response.getEntity());log.info(msg);return msg;} catch (ResponseException e) {log.error("Error executing request: " + e.getMessage());} catch (IOException i){log.error("错误 ",i);}return null;}

测试:
在这里插入图片描述

3. 创建索引

示例:

     PUT /yzy_test{"settings": {"number_of_shards": 1,"number_of_replicas": 1},"mappings": {"properties": {"field1": { "type": "text" },"field2": { "type": "keyword" }}}}

代码:

    @GetMapping("/api/index_create")public String getIndexCreate()   {try {// 创建索引请求Request request = new Request("PUT", "/yzy_test");String jsonString = "{ \"settings\": { \"number_of_shards\": 1, \"number_of_replicas\": 1 }, " +"\"mappings\": { \"properties\": { \"field1\": { \"type\": \"text\" }, \"field2\": { \"type\": \"keyword\" } } } }";request.setJsonEntity(jsonString);// 执行请求Response response = client.getLowLevelClient().performRequest(request);String msg = EntityUtils.toString(response.getEntity());log.info(msg);return msg;} catch (ResponseException e) {log.error("Error executing request: " + e.getMessage());} catch (IOException i){log.error("错误 ",i);}return null;}

测试在这里插入图片描述
在这里插入图片描述

4. 检索索引

代码:

 @GetMapping("/api/index_get")public String getGetIndex()   {try {// 创建索引请求Request request = new Request("GET", "/yzy_test");// 执行请求Response response = client.getLowLevelClient().performRequest(request);String msg = EntityUtils.toString(response.getEntity());log.info(msg);return msg;} catch (ResponseException e) {log.error("Error executing request: " + e.getMessage());} catch (IOException i){log.error("错误 ",i);}return null;}

测试:
在这里插入图片描述

5. 删除索引

代码:

 @GetMapping("/api/index_delete")public String getDelIndex()   {try {// 创建索引请求Request request = new Request("DELETE", "/yzy_test");// 执行请求Response response = client.getLowLevelClient().performRequest(request);String msg = EntityUtils.toString(response.getEntity());log.info(msg);return msg;} catch (ResponseException e) {log.error("Error executing request: " + e.getMessage());} catch (IOException i){log.error("错误 ",i);}return null;}

测试:
在这里插入图片描述
在这里插入图片描述

6. 新增文档

代码:

    @GetMapping("/api/doc_create")public void createDocument() {try {// 创建 IndexRequest 对象,指定索引名称和文档IDIndexRequest request = new IndexRequest("yzy-app-a-20240830").id("1");// 设置文档内容String jsonString = "{" +"\"user\":\"kimchy\"," +"\"postDate\":\"2023-08-30\"," +"\"message\":\"trying out Elasticsearch\"" +"}";request.source(jsonString, XContentType.JSON);// 执行请求IndexResponse response = client.index(request, RequestOptions.DEFAULT);log.info( "Document add: " + response.getResult());} catch (IOException e) {log.error("Error creating document", e);}}

测试:
在这里插入图片描述
在这里插入图片描述

7. 更新文档

代码:

    @GetMapping("/api/doc_update")public void updateDocument() {try {// 创建 UpdateRequest 对象,指定索引名称和文档IDUpdateRequest request = new UpdateRequest("yzy-app-a-20240830", "1");// 设置更新内容String jsonString = "{" +"\"message\":\"updating document content\"" +"}";request.doc(jsonString, XContentType.JSON);// 执行请求UpdateResponse response = client.update(request, RequestOptions.DEFAULT);log.info( "Document update: " + response.getResult());} catch (IOException e) {log.error("Error updating document", e);}}

测试:
在这里插入图片描述
在这里插入图片描述

8. 根据索引中的文档 ID 查询文档

代码:

    @GetMapping("/api/doc/{index}/{id}")public String getDocumentById(@PathVariable String index, @PathVariable String id) {try {// 创建 GetRequest 对象,指定索引名称和文档IDGetRequest getRequest = new GetRequest(index, id);// 执行查询GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);// 判断文档是否存在if (getResponse.isExists()) {// 返回文档内容return getResponse.getSourceAsString();} else {return "Document not found";}} catch (Exception e) {// 错误处理e.printStackTrace();return "Error retrieving document: " + e.getMessage();}}

测试:
在这里插入图片描述

9. 删除文档

    @GetMapping("/api/doc_delete")public void deleteDocument() {try {// 创建 DeleteRequest 对象,指定索引名称和文档IDDeleteRequest request = new DeleteRequest("yzy-app-a-20240830", "1");// 执行请求DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);log.info( "Document deleted: " + response.getResult());} catch (IOException e) {log.error("Error deleting document", e);}}

测试:
在这里插入图片描述
在这里插入图片描述

这篇关于Elasticsearch - SpringBoot 索引与文档相关demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133399

相关文章

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

springboot security之前后端分离配置方式

《springbootsecurity之前后端分离配置方式》:本文主要介绍springbootsecurity之前后端分离配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录前言自定义配置认证失败自定义处理登录相关接口匿名访问前置文章总结前言spring boot secu

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3