2023年7月26日发(作者:)
SpringBoot笔记-SpringBoot与检索(⼗⼀)1.检索Elasticsearch是⼀个分布式搜索服务,提供Restful API,底层基于Lucene,采⽤多shard的⽅式保证数据安全,并且提供⾃动resharding的功能,github等⼤型的站点也是采⽤了Elasticsearch作为其搜索服务。2.概念以员⼯信息存储为例,⼀个⽂档代表⼀个员⼯数据。存储到ElasticSearch的⾏为叫索引,在索引⽂档之前,需要确定⽂档存储的位置。⼀个ElasticSearch集群可以包含多个索引,每个索引可以包含多个类型,不同类型存储多个⽂档,每个⽂档有多个属性。3.整合ElasticSearch测试1.安装镜像使⽤命令:docker pull elasticsearch:7.6.2下载镜像,不带tag会提⽰lastest找不到,所以就指定⼀个tag。使⽤命令:docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 elasticsearch:7.6.2启动镜像。这⾥需要说⼀下,ElasticSearch启动,默认吃2G的内存,内存不够的可以带上-e参数加上内存限制。如果内存⾜够,可以不带-e参数,这⾥有两个-p,9200端⼝是web通信端⼝,9300是分布式ElasticSearch结点间通信端⼝。这⾥,我启动失败了,使⽤命令docker logs 容器id,查看报错信息,⼀共有两个。ERROR: [2] bootstrap checks failed[1]: max virtual memory areas _map_count [65530] is too low, increase to at least [262144]ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/[2]: the default discovery settings are unsuitable for production use; at least one of [_hosts, _providers, l_master_nodes第⼀个报错的意思是ElasticSearch拥有的内存太⼩,⾄少需要262144。使⽤命令sysctl -a|grep _map_count查看得到_map_count = 65530,我们需要修改这个值。单次⽣效的⽅法:sysctl -w _map_count=262144。要想永久⽣效,需要在/etc/⽂件下添加⼀⾏:_map_count=262144并重启,即可解决。第⼆个问题应该是⾼版本和低版本的不同,修改启动命令为:docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e"=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.6.2,这⾥多了⼀个-e "=single-node"参数,应该是以单结点⽅式启动的意思,避免集群检查。2.功能测试Elasticsearch是⾯向⽂档的,意味着它存储整个对象或⽂档。Elasticsearch不仅存储⽂档,⽽且索引每个⽂档的内容,使之可以被检索。在Elasticsearch中,我们对⽂档进⾏索引、检索、排序和过滤—⽽不是对⾏列数据。这是⼀种完全不同的思考数据的⽅式,也是Elasticsearch能⽀持复杂全⽂检索的原因。ElasticSearch使⽤JSON作为⽂档的序列化格式。向ES中添加员⼯信息:从ES中获取员⼯信息:从ES中删除员⼯信息:查看ES中是否有员⼯信息:修改ES中员⼯信息:和新增⼀样,发送PUT请求即可。查询全部信息:按照条件查询:按照检索表达式查询:更复杂的检索表达式:{ "query" : { "bool": { "must": { "match" : { "last_name" : "smith"
} }, "filter": { "range" : { "age" : { "gt" : 30 }
} } } }}全⽂检索表达式:{ "query" : { "match" : { "about" : "rock climbing" } }}短语检索表达式:{ "query" : { "match_phrase" : { "about" : "rock climbing" } }}⾼亮检索表达式:{ "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } }} Boot整合ElasticSearch新建Spring Boot项⽬,添加Spring Web、Spring Data ElasticSearch(Access+Driver)依赖。Spring Boot默认⽀持两种技术和ES交互:Jest(现在已经@Deprecated了)和SpringData ElasticSearch。SpringData ElasticSearch的⾃动配置类中会导⼊Client、ElasticSearchTemplate,还提供了ElasticSearchRepository的⼦接⼝操作ES。加⼊yml配置⽂件。spring: data: elasticsearch: # cluster-name的取值,要和浏览器访问192.168.0.123:9200的返回结果⾥的cluster-name⼀致 cluster-name: docker-cluster cluster-nodes: 192.168.0.123:9300观察左侧导⼊的csearch:elasticsearch:6.8.7,这个6.8.7是spring-boot-starter-data-elasticsearch帮我们导⼊的,⽽我们镜像⾥的版本是7.6.2,ElasticSearch对版本要求⽐较严格,这⾥的版本不⼀致,后⾯会报错,所以需要将镜像换成6.8.7版本。Spring Data Elasticsearch导⼊的Elasticsearch versionsSpring Data icSearch6.8.16.2.25.5.02.4.02.2.01.5.2回到虚拟机中,替换Docker镜像版本。1. 使⽤命令docker stop 容器id,停⽌容器。2. 使⽤命令docker rm 容器id,删除容器。3. 使⽤命令docker images查看所有镜像,后使⽤docker rmi 镜像id,删除ElasticSearch的镜像。4. 使⽤命令docker pull elasticsearch:6.8.7下载6.8.7版本的ElasticSearch镜像。5. 使⽤命令docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e "=single-node" -p 9200:9200 -p9300:9300 elasticsearch:6.8.7创建并启动容器。6. 通过浏览器访问地址,确认版本号是不是6.8.7,以及容器是否正常启动。创建bean以及repository,通过测试类进⾏测试。package ;import nt;// indexName:索引名称 type:索引类型@Document(indexName = "atguigu", type = "book")public class Book { private int id; private String name; private String author; public Book() { } public Book(int id, String name, String author) { = id; = name; = author; } public int getId() { return id; } public void setId(int id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public String getAuthor() { return author; } public void setAuthor(String author) { = author; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + ''' + ", author='" + author + ''' + '}'; }}package tory;import ;import csearchRepository;import ;public interface BookRepository extends ElasticsearchRepository
@Test public void findByAuthorLike() { List
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690363531a338235.html
评论列表(0条)