Spring Boot 集成Elasticsearch模块实现简单查询功能
作者:剑圣无痕 发布时间:2022-09-05 06:31:31
背景
项目中我们经常会用搜索功能,普通的搜索我们可以用一个SQL的like也能实现匹配,但是搜索的核心需求是全文匹配,对于全文匹配,数据库的索引是根本派不上用场的,那只能全表扫描。全表扫描的速度已经非常慢了,还需要在每条记录上做全文匹配,一个字一个字的比对,导致查询的数据更慢。所以,使用数据来做搜索,性能上完全没法满足要求。所以我们需要采用Elasticsearch来实现检索,本文将介绍SpringBoot如何集成Elasticsearch?
系统集成
引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.yml文件中添加ES配置
elasticsearch:
rest:
uris: http://localhost:9200
注意:不同的ES版本,引入jar包和配送属性文件的方式不同,本文采用的是Spring Boot 2.2+Elasticsearch7.0的版本。
创建文档实体
@Document(indexName = "product", createIndex = true)
public class Product implements Serializable
{
private static final long serialVersionUID = -2408117939493050954L;
@Id
@Field(type = FieldType.Text)
private String id;
@Field(type = FieldType.Text)
private String skuNo;
@Field(type = FieldType.Text)
private String tilte;
@Field(type = FieldType.Double)
private BigDecimal price;
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
private Date createDate;
}
说明:
indexName:索引的名称
createIndex:ture表示如果不存在,则创建
@Id:索引id
@Field:type字段的类型,format:查询出时间格式化类型。
接口实现
public interface EsProductRepository extends ElasticsearchRepository<Product,String>
{
List<Product> findByskuNoAndTilte(String sku,String title);
}
说明:集成ElasticsearchRepository接口,采用的是JPA的方式实现,JPA默认提供了相关的接口实现。
具体实现
Elasticsearch的实现分为基础查询和DSL查询。
基础查询
基础查询主要包含的CRUD查询,以及一些模糊、范围查询等。
新增文档
请求参数
{
"id":"5",
"skuNo":"sku0005",
"tilte":"红楼梦",
"price":"93.37",
"createDate":"1514736000000"
}
说明:date类型传入的参数为long类型。
Controller实现
@PostMapping("/addProduct")
public Result addProduct(@RequestBody Product product)
{
esProductRepository.save(product);
Result result = new Result();
result.setCode(200);
result.setData(product);
return result;
}
返回结果
{
"data": {
"id": "5",
"skuNo": "sku0005",
"tilte": "红楼梦",
"price": 93.37,
"createDate": "2017-12-31T16:00:00.000+00:00"
},
"code": 200,
"msg": null
}
修改文档
修改与新增基本相同,唯一区别为:请求参数传入的Id,如果存在则为修改,否则为新增。
通过id查询文档信息
Controller实现
@GetMapping("/getProductById")
public Result getProductById(@RequestParam String id) {
Optional<Product> product = esProductRepository.findById(id);
return Result.success(product);
}
删除文档
Controller实现
@PostMapping("/deleteById")
public Result deleteById(@RequestParam String id)
{
return Result.success(null);
}
分页查询
Controller实现
@GetMapping("/getPageList")
public Result getPageList(@RequestParam int pageNum,@RequestParam int pageSize)
{
Pageable pageable = PageRequest.of(pageNum, pageSize);
Page<Product> pageList= esProductRepository.findAll(pageable);
return Result.success(pageList);
}
返回结果
{
"data": {
"content": [
{
"id": "1",
"skuNo": "p0001",
"tilte": null,
"price": 99.9,
"createDate": null
},
{
"id": "3",
"skuNo": "p0002",
"tilte": null,
"price": 99.8,
"createDate": null
},
{
"id": "4",
"skuNo": "p0004",
"tilte": null,
"price": 110,
"createDate": null
},
{
"id": "L1zuVYEBuycvlc7eiQ7_",
"skuNo": "sku0001",
"tilte": "水浒传",
"price": 93.37,
"createDate": "1970-01-01T05:37:00.611+00:00"
},
{
"id": "5",
"skuNo": "sku0005",
"tilte": "红楼梦",
"price": 93.37,
"createDate": "2017-12-31T16:00:00.000+00:00"
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageSize": 5,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"aggregations": null,
"scrollId": null,
"maxScore": 1.0,
"totalPages": 1,
"totalElements": 5,
"number": 0,
"size": 5,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"numberOfElements": 5,
"first": true,
"last": true,
"empty": false
},
"code": 200,
"msg": null
}
说明:
totalPages:总页数
totalElements:总记录数
模糊查询
Controller实现
@GetMapping("/findByTilteLike")
public Result findByTilteLike(@RequestParam String key) {
List<Product> products = esProductRepository.findByTilteLike(key);
return Result.success(products);
}
说明:模糊查询通过findByxxlike
范围查询
范围查询通常是指>、< >= <=等
Controller实现
@GetMapping("/findByPriceGreaterThanEqual")
public Result findByPriceGreaterThanEqual(@RequestParam Double price) {
List<Product> products = esProductRepository.findByPriceGreaterThanEqual(price);
return Result.success(products);
}
说明:范围查询通过findByxxGreaterThanEqual
大于:GreaterThan
大于等于:GreaterThanEqual
小于:LessThan
小于等于:LessThanEqual
来源:https://juejin.cn/post/7108331484494184455


猜你喜欢
- 1.概念1.AOP技术简介AOP 为Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运
- 本篇随笔将讲解一下Android当中比较常用的两个布局容器--ScrollView和HorizontalScrollView,从字面意义上来
- 在C#中,如果在方法参数前面加上ref关键字,说明参数传递的是引用,而不是值。如何理解呢?参数是简单类型的例子static void Mai
- 上一篇:瑞吉外卖项目:新增员工一. 员工信息分页查询1. 需求分析当系统中的用户越来越多页面展示不完整,我们需要通过实现分页的方式去展示员工
- 题目从命令行读入两个数组的长度和数组的值,其中第一行两个数na和nb代表aa和bb数组的长度代码import java.util.Scann
- 前言为什么用动静态库我们在实际开发中,经常要使用别人已经实现好的功能,这是为了开发效率和鲁棒性(健壮性);因为那些功能都是顶尖的工程师已经写
- JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范. 为Java开发人员提供了一种对象/
- 1、理论一般如果想将类注册到spring容器,让spring来完成实例化,常用方式如下:xml中通过bean节点来配置;使用@Service
- 在Java 字符终端上获取输入有三种方式:1、java.lang.System.in (目前JDK版本均支持)2、java.util.Sca
- using System;using System.Collections.Generic;using System.ComponentMo
- 本文实例为大家分享了java实现图片分割指定大小的具体代码,供大家参考,具体内容如下1.使用工具:ThumbnailsThumbnails
- 在 Lock 接口中,获取锁的方法有 4 个:lock()、tryLock()、tryLock(long,TimeUnit)、lockInt
- 安装方式:1):通过ppa(源) 方式安装.2):通过官网安装包安装.JDK官网下载地址一:使用ppa(源)方式安装:1):添加ppa源su
- TTL简介TTL 是什么呢?TTL 是 RabbitMQ 中一个消息或者队列的属性,表明一条消息或者该队列中的所有消息的最大存活时间,单位是
- 本文实例分析了C#中out保留字的用法,分享给大家供大家参考。具体用法分析如下:C#中的out保留字表示这个变量要回传值,最简单的应用是除法
- 本文实例总结了Android开发之Button事件实现与监听方法。分享给大家供大家参考,具体如下:先来介绍Button事件实现的两种方法ma
- 一、效果:我们看到很多软件的通讯录在右侧都有一个字母索引功能,像微信,小米通讯录,QQ,还有美团选择地区等等。这里我截了一张美团选择城市的图
- ProgressDialog(精度条对话框):1.直接调用ProgressDialog提供的静态方法show()显示2.创建Progress
- 一、前期工作1.开启邮箱服务开启邮箱的POP3/SMTP服务(这里以qq邮箱为例,网易等都是一样的)2.导入依赖在springboot项目中
- 传输层安全性协议(英语:Transport Layer Security,缩写作 TLS),及其前身安全套接层(Secure Sockets