当前位置: 首页 > 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

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

相关文章:

  • html+css网页制作 化妆品电商4个页面
  • 微调LLama 3.1——七月论文审稿GPT第5.5版:拿早期paper-review数据集微调LLama 3.1
  • rust 编译时报错:type annotations needed for Box
  • 应用方案 | 低功耗接地故障控制器D4145
  • 第一次彩色pcb打样记录
  • 通过 MediatR 实现了请求的分发和处理器的解耦
  • Naive UI+vue一些组件的注意事项
  • sgetrf M N is 103040 时报错,这是个bug么 lapack and Openblas the same,修复备忘
  • [后端代码审计] PHP 数组知识汇总
  • 单点Redis中面临哪些问题
  • 数学建模--蒙特卡洛算法之电子管更换刀片寿命问题
  • 如何解码Linux下事件响应工具evtest的时间戳
  • 基于STM32开发的智能门禁系统
  • EasyExcel-高性能的 Java Excel 处理库
  • 精益生产培训秘籍:六步策略,助力企业降本增效——张驰咨询
  • 【第19章】Spring Cloud之Gateway自定义Logback配置
  • Java流式编程
  • 高可用集群keepalived从部署到实战一篇解决
  • 22222222222
  • springboot宠物相亲平台-计算机毕业设计源码16285
  • 警惕:手机被监听时会出现这些情况
  • Windows 系统下 MongoDB和PostgreSQL数据库数据的备份和恢复
  • 必应Bing国内搜索广告开户收费标准公示
  • 大模型汇总:文心一言大模型、腾讯混元大模型、通义千问大模型、字节豆包大模型、智普清言大模型、KIMI 大模型、紫东太初大模型、讯飞星火大模型
  • C语言——结构体、共用体、枚举、位运算
  • [LitCTF 2024]exx
  • kafka运维常用命令
  • 笔记:在WPF中OverridesDefaultStyle属性如何使用
  • MATLAB/Simulink 与Gazebo联合仿真
  • 并查集-应用方向以及衍生汇总+代码实现(c++)-学习一个数据结构就会做三类大题!