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

elasticsearch集成springboot详细使用

1.es下载&配置

配置JVM

在这里插入图片描述

配置跨域
在这里插入图片描述

配置https和密码

在这里插入图片描述

2.es启动

.\elasticsearch.bat

或 后台启动:
nohup ./bin/elasticsearch&

浏览器访问:https://localhost:9200
输入账户:elastic / 123456
在这里插入图片描述

3.重置es密码

.\elasticsearch-reset-password.bat -u elastic -i

在这里插入图片描述

4.安装图形化插件:elasticsearch-head插件,完成图形化界面的效果,完成索引数据的查看
  1. es核心概念

    面向文档

    可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤

    Elasticsearch对比传统[关系型数据库]如下:

    Relational DB ‐> Databases ‐> Tables ‐> Rows ‐> Columns
    Elasticsearch ‐> Index ‐> Types ‐> Documents ‐> Fields

集群cluster:一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能。一个集群由 一个唯一的名字标识,这个名字默认就是“elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集 群的名字,来加入这个集群。

节点node:一个节点是集群中的一个服务器,作为集群的一部分,它存储数据,参与集群的索引和搜索功能;一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫 做“elasticsearch”的集群中

分片和复制 shards&replicas:一个索引可以存储超出单个结点硬件限制的大量数据

ElasticSearch客户端操作:

  • 使用elasticsearch-head插件
  • 使用elasticsearch提供的Restful接口直接访问
  • 使用elasticsearch提供的API进行访问

Elasticsearch的接口语法:

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

查询文档document有三种方式:

  • 根据id查询;
  • 根据关键词查询
  • 根据输入的内容先分词,再查询
  1. elasticsearch集成springboot使用
      <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
1.创建实体import lombok.Data;
import org.apache.catalina.Store;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Document(indexName = "article")
@Data
public class Article {@Id@Field(index = false,type = FieldType.Integer)private Integer id;/*** index:是否设置分词  默认为true* analyzer:储存时使用的分词器* searchAnalyze:搜索时使用的分词器* store:是否存储  默认为false* type:数据类型  默认值是FieldType.Auto**/@Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)private String title;@Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)private String context;@Field(store = true,type =FieldType.Integer)private  Integer hits;
}2.创建CRUD操作类
import com.cloud.entities.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;import java.util.List;/***  自定义接口需要继承ElasticsearchRepository<实体类型,主键类型>  基本的crud 分页*/
@Component
public interface ArticalRepository extends ElasticsearchRepository<Article,Integer> {List<Article> findByTitle(String title);List<Article> findArticleByTitleOrContext(String title,String context);/*** 根据标题或内存查询(含分页)* @param title* @param context* @param pageable0* @return*/List<Article> findByTitleOrContext(String title, String context, Pageable pageable0);}3.创建启动入口类
@SpringBootApplication
@MapperScan("com.cloud.mapper")  //import tk.mybatis.spring.annotation.MapperScan
public class Main8001 {public static void main(String[] args) {SpringApplication.run(Main8001.class,args);}}
4.配置springboot配置esspring:elasticsearch:uris: http://localhost:9200connection-timeout: 5s#username: elastic#password: 1234565.创建单元测试类
import com.cloud.Main8001;
import com.cloud.entities.Article;
import com.cloud.repository.ArticalRepository;
import jakarta.annotation.Resource;
import org.elasticsearch.client.RestClient;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.test.context.junit4.SpringRunner;import java.util.Arrays;
import java.util.Map;@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main8001.class)
public class ArticleTest {@Resourceprivate RestClient restClient;@Resourceprivate ElasticsearchTemplate elasticsearchTemplate;@Resourceprivate ArticalRepository articalRepository;/**1.* 使用ElasticsearchTemplate*/@Testvoid insert(){Article article=new Article();article.setTitle("男乒乓");article.setId(1);article.setContext("中国赢了");article.setHits(100);elasticsearchTemplate.save(article);}/*** 2.* 使用Repository*/@Testvoid insert2(){Article article=new Article();article.setTitle("男乒乓2");article.setId(2);article.setContext("中国赢了2");article.setHits(120);articalRepository.save(article);}/*** 批量保存*/@Testvoid insert3(){Article article=new Article();article.setTitle("男乒乓3");article.setId(3);article.setContext("中国赢了2");article.setHits(130);articalRepository.saveAll(Arrays.asList(article));}@Testvoid update(){Article article= articalRepository.findById(2).get();article.setTitle("篮球");articalRepository.save(article);}@Testvoid update2(){Article article=new Article();article.setId(2);article.setTitle("网球");elasticsearchTemplate.update(article);}/***  查询全部数据*/@Testvoid findAll(){Iterable<Article> articles=articalRepository.findAll();articles.forEach(System.out::println);}/*** 根据id删除*/@Testvoid deleteById(){articalRepository.deleteById(3);}/*** 删除:传入实体类删*/@Testvoid delete(){Article article=new Article();article.setId(3);articalRepository.delete(article);}/*** 删除索引里面所有数据*/@Testvoid deleteAll(){articalRepository.deleteAll();}}
  1. 测试工具:postman
  2. es 参考资料:

https://cloud.tencent.com/developer/article/2249187

https://www.hadoopdoc.com/elasticsearch/elasticsearch-begin-tutorial

相关文章:

elasticsearch集成springboot详细使用

1.es下载&配置 配置JVM 配置跨域 配置https和密码 2.es启动 .\elasticsearch.bat 或 后台启动&#xff1a; nohup ./bin/elasticsearch& 浏览器访问&#xff1a;https://localhost:9200 输入账户&#xff1a;elastic / 123456 3.重置es密码 .\elasticsearch-r…...

html+css网页制作 化妆品电商4个页面

htmlcss网页制作 化妆品电商4个页面 网页作品代码简单&#xff0c;可使用任意HTML编辑软件&#xff08;如&#xff1a;Dreamweaver、HBuilder、Vscode 、Sublime 、Webstorm、Text 、Notepad 等任意html编辑软件进行运行及修改编辑等操作&#xff09;。 获取源码 1&#xff…...

微调LLama 3.1——七月论文审稿GPT第5.5版:拿早期paper-review数据集微调LLama 3.1

前言 对于llama3&#xff0c;我们之前已经做了针对llama3 早7数据微调后的测评 去pk llama2的早7数据微调后&#xff0c;推理测试集中的早期paper&#xff1a;出来7方面review去pk gpt4推理测试集中的早期paper&#xff1a;7方面reviewground truth是早期paper的7方面人工rev…...

rust 编译时报错:type annotations needed for Box

如下图所示&#xff1a; 解决方法&#xff1a; 升级time的版本&#xff1a; cargo update -p time...

应用方案 | 低功耗接地故障控制器D4145

一、概述 D4145 是一个接地故障断路器。它能够检测到不良的接地条件&#xff0c;譬如装置接触到水时&#xff0c;它会在有害或致命的电击发生之前将电路断开。 D4145能检测并保护从火线到地线,从零线到地线的故障.这种简单而传统的电路设计能够确保其应用自如和长时间的可靠性。…...

第一次彩色pcb打样记录

感受和总结 看到彩色电路板和绿油板放在一起&#xff0c;感触还是挺大的。而且彩色板还直接给沉金&#xff0c;感觉焊上器件不要外壳都很好看了。后面一定记录一下这个板子实现的功能。 板子功能暂时分配 五个触摸盘&#xff0c;为了通过触摸控制不同功能&#xff0c;例如&a…...

通过 MediatR 实现了请求的分发和处理器的解耦

1. 前端请求发起 假设前端通过 HTTP GET 请求访问 GetTemplateSettings 端点&#xff0c;URL 中包含了 SubjectUuid 和 SubjectType 作为查询参数。 2. 进入 Controller 方法 请求到达后&#xff0c;会进入 MinBcController 类中的 GetTemplateSettings 方法&#xff0c;该方…...

Naive UI+vue一些组件的注意事项

NSpace(间距 Space) 默认给出space内的组件加一个div间隔&#xff0c;只能批量修改space内的元素样式&#xff0c;不能单独修改自组件样式&#xff0c;一般用于横向布局&#xff0c;若垂直布局若需要flex布局&#xff0c;慎用space组件NDataTable(数据表格 Data Table) :flex-h…...

sgetrf M N is 103040 时报错,这是个bug么 lapack and Openblas the same,修复备忘

号外&#xff1a; $ clang-format -style"{BasedOnStyle: llvm, IndentWidth: 4}" -i hello.cpp $ clang-format -style"{BasedOnStyle: llvm, IndentWidth: 4}" -i hello.cpp IndentWidth:4不错&#xff0c;默认2太下了 1,现象 MN103040时&…...

[后端代码审计] PHP 数组知识汇总

文章目录 前言1. 数组基础1.1 数组概念1.2 索引数组1.3 关联数组1.4 多维数组 2. 数组函数2.1 count()2.2 array_merge()2.3 array_keys()2.4 array_values()2.5 in_array() 3. 数组遍历3.1 for循环遍历3.2 foreach遍历3.3 遍历索引数组3.4 遍历关联数组 4. 数组排序4.1 sort()…...

单点Redis中面临哪些问题

我的后端学习大纲 我的Redis学习大纲 1.面试&#xff1a;请说下在单点Redis中面临哪些问题&#xff1a; 1.1.单点Redis的问题&#xff1a; 1.数据丢失问题&#xff1a;Redis是内存存储&#xff0c;服务重启可能会丢失数据 2.并发能力问题&#xff1a;单节点Redis并发能力虽然…...

数学建模--蒙特卡洛算法之电子管更换刀片寿命问题

目录 1.电子管问题重述 2.电子管问题分析 3.电子管问题求解 4.刀片问题重述 5.刀片问题分析 6.刀片问题求解 1.电子管问题重述 某设备上安装有4只型号规格完全相同的电子管&#xff0c;已知电子管寿命服从100&#xff5e;200h之间的均匀分布&#xff0e; 只要有一个电子管…...

如何解码Linux下事件响应工具evtest的时间戳

evtest介绍 这里放一下原文链接evtest工具介绍及安装 在开发input子系统驱动时&#xff0c;常常会使用evtest工具进行测试。evtest是打印evdev内核事件的工具&#xff0c;它直接从内核设备读取并打印设备描述的带有值和符号名的事件&#xff0c;可以用来调试鼠标、键盘、触摸…...

基于STM32开发的智能门禁系统

目录 引言环境准备工作 硬件准备软件安装与配置系统设计 系统架构硬件连接代码实现 初始化代码控制代码应用场景 小区门禁管理企业办公门禁系统常见问题及解决方案 常见问题解决方案结论 1. 引言 智能门禁系统通过整合多种身份识别技术&#xff0c;如密码输入、RFID刷卡、指…...

EasyExcel-高性能的 Java Excel 处理库

EasyExcel 是阿里巴巴开发的一个高性能的 Java Excel 处理库&#xff0c;主要用于处理大规模的 Excel 文件。它特别注重性能&#xff0c;优化了内存消耗&#xff0c;适合处理大数据量的 Excel 文件&#xff0c;避免了传统 Excel 库在处理大文件时的性能瓶颈。 主要功能 高性能…...

精益生产培训秘籍:六步策略,助力企业降本增效——张驰咨询

在当今竞争激烈的市场环境中&#xff0c;企业为了提高生产效率、降低成本、增强市场竞争力&#xff0c;纷纷引入精益生产理念。精益生产作为一种以客户需求为导向&#xff0c;通过持续消除浪费、优化流程、提升质量的生产方式&#xff0c;已成为众多企业转型升级的利器。张驰咨…...

【第19章】Spring Cloud之Gateway自定义Logback配置

文章目录 前言一、内置配置1. 关联依赖2. 内置配置 二、自定义配置1. 日志级别2. 彩色日志3. 自定义配置4. 增加打印语句5. 效果展示 总结 前言 网关层作为我们程序的主入口&#xff0c;有着至关重要的作用&#xff0c;下面我们通过自定义Logback配置增强网关层的日志输出&…...

Java流式编程

一、流的基础概念 流&#xff08;Stream&#xff09;&#xff1a; 定义&#xff1a;流是一种可以在数据集合上进行操作的抽象化序列&#xff0c;它没有存储数据的能力&#xff0c;而是通过一系列的操作来处理数据。特性&#xff1a; 无存储&#xff1a;流不存储数据&#xff0c…...

高可用集群keepalived从部署到实战一篇解决

目录 一.高可用集群 1.1 集群类型 1.2 系统可用性 1.3 系统故障 1.4 实现高可用 1.5.VRRP&#xff1a; 1.5.1 VRRP 相关术语 1.5.2 VRRP 相关技术 二.Keepalived 部署 2.1 keepalived 简介 2.2keepalived架构 2.3 Keepalived 环境准备 2.4 Keepalived 相关文件 2.…...

22222222222

222222222222222222...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

如何将联系人从 iPhone 转移到 Android

从 iPhone 换到 Android 手机时&#xff0c;你可能需要保留重要的数据&#xff0c;例如通讯录。好在&#xff0c;将通讯录从 iPhone 转移到 Android 手机非常简单&#xff0c;你可以从本文中学习 6 种可靠的方法&#xff0c;确保随时保持连接&#xff0c;不错过任何信息。 第 1…...

现代密码学 | 椭圆曲线密码学—附py代码

Elliptic Curve Cryptography 椭圆曲线密码学&#xff08;ECC&#xff09;是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础&#xff0c;例如椭圆曲线数字签…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

保姆级教程:在无网络无显卡的Windows电脑的vscode本地部署deepseek

文章目录 1 前言2 部署流程2.1 准备工作2.2 Ollama2.2.1 使用有网络的电脑下载Ollama2.2.2 安装Ollama&#xff08;有网络的电脑&#xff09;2.2.3 安装Ollama&#xff08;无网络的电脑&#xff09;2.2.4 安装验证2.2.5 修改大模型安装位置2.2.6 下载Deepseek模型 2.3 将deepse…...

Java求职者面试指南:计算机基础与源码原理深度解析

Java求职者面试指南&#xff1a;计算机基础与源码原理深度解析 第一轮提问&#xff1a;基础概念问题 1. 请解释什么是进程和线程的区别&#xff1f; 面试官&#xff1a;进程是程序的一次执行过程&#xff0c;是系统进行资源分配和调度的基本单位&#xff1b;而线程是进程中的…...

uniapp 字符包含的相关方法

在uniapp中&#xff0c;如果你想检查一个字符串是否包含另一个子字符串&#xff0c;你可以使用JavaScript中的includes()方法或者indexOf()方法。这两种方法都可以达到目的&#xff0c;但它们在处理方式和返回值上有所不同。 使用includes()方法 includes()方法用于判断一个字…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

Kafka主题运维全指南:从基础配置到故障处理

#作者&#xff1a;张桐瑞 文章目录 主题日常管理1. 修改主题分区。2. 修改主题级别参数。3. 变更副本数。4. 修改主题限速。5.主题分区迁移。6. 常见主题错误处理常见错误1&#xff1a;主题删除失败。常见错误2&#xff1a;__consumer_offsets占用太多的磁盘。 主题日常管理 …...