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

SpringBoot Elasticsearch全文搜索

文章目录

  • 概念
  • 全文搜索相关技术
  • Elasticsearch
    • 概念
      • 近实时
      • 索引
      • 类型
      • 文档
      • 分片(Shard)和副本(Replica)
    • 下载
    • 启用
    • SpringBoot整合
      • 引入依赖
      • 创建文档类
      • 创建资源库
      • 测试文件初始化数据
      • 创建控制器
  • 问题
  • 参考

概念

全文搜索(检索),工作原理:计算机索引程序,扫描文章中的每一个词,对每一个词建立一个索引,指明出现次数和位置。查询时通过索引进行查找,类似于查字典。
因为是通过索引在查,速度较于通过sql查,会快很多。
具体过程如下:
1、建文本库
2、建立索引
3、执行搜索
4、过滤结果

全文搜索相关技术

Lucene:https://lucene.apache.org/core/
Solr:https://solr.apache.org/
Elasticsearch:https://www.elastic.co/cn/elasticsearch
Lucene是搜索引擎,Elasticsearch和Solr都是基于Lucene之上实现的全文检索系统
Elasticsearch和Solr对比,版本比较老,做参考即可

Elasticsearch

概念

一个高度可扩展的开源全文搜索和分析引擎,它允许用户快速地、近实时地对大数据进行存储、搜索和分析,它通常用来支撑有复杂的数据搜索需求的企业级应用 。

近实时

近实时,而不是实时
索引文档到可搜索的时间有一个轻微的延迟(通常为1秒)。之所以会有这个延时,主要考虑查询的性能优化。
想要实时,就得刷新,要么是牺牲索引的效率(每次索引之后刷新),要么就是牺牲查询的效率(每次查询之前都进行刷新 ),Elasticsearch取了折中,每隔n秒自动刷新
Elasticsearch 索引新文档后,不会直接写入磁盘,而是首先存入文件系统缓存,之后根据刷新设置,定期同步到磁盘。索引我们改完内容不会立即被搜索出来,但是会在1秒内可见

索引

相似文档的集合

类型

对一个索引中包含的文档进一步细分

文档

索引的基本单位,与索引中的一个类型相对应

分片(Shard)和副本(Replica)

数据量较大时,把索引分成多个分片来存储索引的部分数据,提高性能/吞吐量
为了安全,一个分片中的数据至少有一个副本

下载

https://www.elastic.co/cn/downloads/elasticsearch
注意版本,spring-boot2.x,不要用最新版本,用7.x.x

启用

命令行进入bin目录,执行elasticsearch启动服务,Ctrl/command + C停止服务
启用localhost:9200,测试Elasticsearch节点是否正在运行,可能会遇到安全认证问题,见问题部分

{"name": "zhangxingxingdeMacBook-Pro.local","cluster_name": "elasticsearch","cluster_uuid": "DwgXhzhwQ9WS0drElcEZmg","version": {"number": "7.11.1", // 当前elasticsearch版本"build_flavor": "default","build_type": "tar","build_hash": "ff17057114c2199c9c1bbecc727003a907c0db7a","build_date": "2021-02-15T13:44:09.394032Z","build_snapshot": false,"lucene_version": "8.7.0", //lucene版本"minimum_wire_compatibility_version": "6.8.0","minimum_index_compatibility_version": "6.0.0-beta1"},"tagline": "You Know, for Search"
}

SpringBoot整合

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

创建文档类

@Document(indexName = "blog")
@Table(name = "article")
public class EsBlog implements Serializable {private static final long serialVersionUID = 1L;@Id // 主键private String id;private String title;private  String author;private String content;protected EsBlog(){}public EsBlog(String title, String author, String content){this.title = title;this.author = author;this.content = content;}......@Overridepublic String toString(){return String.format("Article[id=%s, title='%s', author='%s', content='%s']",id, title, author, content);}
}

创建资源库

@Repository
public interface EsBlogRepository extends ElasticsearchRepository<EsBlog, String> {Page<EsBlog> findByTitleContainingOrAuthorContainingOrContentContaining(String title, String author, String content, Pageable pageable);
}

注意在创建启动类中进行包扫描,否则注入的时候找不到bean

@EnableJpaRepositories(basePackages = "com.xxx.xxx")

测试文件初始化数据

@RunWith(SpringRunner.class)
@SpringBootTest(classes= SpringApplicationSock.class) // 启动sping-boot,引入IOC
public class EsBlogRepositoryTest {@Autowiredprivate EsBlogRepository esBlogRepository;@Beforepublic void initRepositoryData(){// 清除所有数据esBlogRepository.deleteAll();// 初始化数据,存入es存储库esBlogRepository.save(new EsBlog("静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。"));esBlogRepository.save(new EsBlog("咏柳", "贺知章", "碧玉妆成一树高,万条垂下绿丝绦。不知细叶谁裁出,二月春风似剪刀。"));esBlogRepository.save(new EsBlog("悯农", "李绅", "锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。"));}@Testpublic void testFindDistincEsBlogTitleContainingOrSummaryContainingOrContentContaining(){// 初始化一个分页请求Pageable pageable = PageRequest.of(0, 20);String title = "咏";String author = "王";String content = "月";Page<EsBlog> page = esBlogRepository.findByTitleContainingOrAuthorContainingOrContentContaining(title, author, content, pageable);System.out.println("=================start");for(EsBlog blog : page){System.out.println(blog.toString());}System.out.println("=================end");}
}

查看存储库
http://localhost:9200/_cat/indices?v=
在这里插入图片描述

上述内容通过查询条件,只能查出两条数据
在这里插入图片描述
查看blog相关信息
http://localhost:9200/blog

{"blog": {"aliases": {},"mappings": {"properties": {"_class": {"type": "keyword","index": false,"doc_values": false},"author": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"content": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"title": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}},"settings": {"index": {"routing": {"allocation": {"include": {"_tier_preference": "data_content"}}},"refresh_interval": "1s","number_of_shards": "1","provided_name": "blog","creation_date": "1703233943853","store": {"type": "fs"},"number_of_replicas": "1","uuid": "0ELJkqnmTg-tDwritULELA","version": {"created": "7110199"}}}}
}

创建控制器

@RestController
@RequestMapping("/blogs")
public class EsBlogController {@Autowiredprivate EsBlogRepository esBlogRepository;@GetMappingpublic List<EsBlog> list(@RequestParam(value = "title", required = false, defaultValue = "") String title,@RequestParam(value = "author", required = false, defaultValue = "") String author,@RequestParam(value = "content", required = false, defaultValue = "") String content,@RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize){Pageable pageable = PageRequest.of(pageIndex, pageSize);Page<EsBlog> page = esBlogRepository.findByTitleContainingOrAuthorContainingOrContentContaining(title, author, content, pageable);return page.getContent();}
}

在这里插入图片描述

问题

1、ElasticSearch服务正常启动,但是在浏览器上无法访问http://localhost:9200,最新版本可能会有这个问题
received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{localAddress=/[0:0:0:0:0:0:0:1]:9200, remoteAddress=/[0:0:0:0:0:0:0:1]:63470}
解决方法:
ElasticSearch默认开启了安全认证,需要将安全认证关掉
config/elasticsearch.yml,将下面两处的true改为false
在这里插入图片描述
2、启动test,提示Unsatisfied dependency expressed through field ‘esBlogRepository’;
未启动spring boot,没有IOC
https://blog.csdn.net/weixin_43801567/article/details/96643032
3、Unable to parse response body for Response{requestLine=POST /blog/_doc?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}
es服务器的响应程序解析不了,有可能是spring-boot版本低了
spring-boot 2.7.3,es:8.11.3 会有问题,将es改为7.11.1正常

参考

https://blog.csdn.net/weixin_38201936/article/details/121746906
https://blog.csdn.net/qq_50652600/article/details/125521823

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

相关文章:

  • Python 常用模块re
  • 【华为OD题库-106】全排列-java
  • Three.js 详细解析(持续更新)
  • Unity中Shader平移矩阵
  • python dash 的学习笔记1
  • SQLITE如何同时查询出第一条和最后一条两条记录
  • 四、ensp配置ftp服务器实验
  • VS2020使用MFC开发一个贪吃蛇游戏
  • 【经典LeetCode算法题目专栏分类】【第9期】深度优先搜索DFS与并查集:括号生成、岛屿问题、扫雷游戏
  • 字符设备驱动开发-注册-设备文件创建
  • TrustZone之可信操作系统
  • java定义三套场景接口方案
  • idea连接数据库,idea连接MySQL,数据库驱动下载与安装
  • Redis-实践知识
  • 多维时序 | MATLAB实现SSA-CNN-SVM麻雀算法优化卷积神经网络-支持向量机多变量时间序列预测
  • leetcode160相交链表思路解析
  • 在线分析工具-日志优化
  • 硬核实战!mysql 错误操作整个表全部数据后如何恢复?附解决过程、思路(百万行SQL,通过binlog日志恢复)
  • 【什么是反射机制?为什么反射慢?】
  • PostGreSQL:货币类型
  • ESP8266网络相框采用TFT_eSPI库TJpg_Decoder库mixly库UDP库实现图片传送
  • Go 泛型发展史与基本介绍
  • python 解决手机拍的书籍图片发灰的问题
  • 【prompt一】Domain Adaptation via Prompt Learning
  • 视频编辑与制作,添加视频封面的软件
  • Deepin更换仿Mac主题
  • 【Flink-Kafka-To-ClickHouse】使用 Flink 实现 Kafka 数据写入 ClickHouse
  • 浅谈Redis分布式锁(下)
  • Django Rest Framework框架的安装
  • 深度学习(七):bert理解之输入形式