高德地图怎么申报地址,公司seo是什么级别,重庆市建设工程信息网信用信息发布平台,南昌seo网站排名文章目录 概念全文搜索相关技术Elasticsearch概念近实时索引类型文档分片(Shard)和副本(Replica) 下载启用SpringBoot整合引入依赖创建文档类创建资源库测试文件初始化数据创建控制器 问题参考 概念
全文搜索#xff08;检索#xff09;#xff0c;工作原理#xff1a;计算… 文章目录 概念全文搜索相关技术Elasticsearch概念近实时索引类型文档分片(Shard)和副本(Replica) 下载启用SpringBoot整合引入依赖创建文档类创建资源库测试文件初始化数据创建控制器 问题参考 概念
全文搜索检索工作原理计算机索引程序扫描文章中的每一个词对每一个词建立一个索引指明出现次数和位置。查询时通过索引进行查找类似于查字典。 因为是通过索引在查速度较于通过sql查会快很多。 具体过程如下 1、建文本库 2、建立索引 3、执行搜索 4、过滤结果
全文搜索相关技术
Lucenehttps://lucene.apache.org/core/ Solrhttps://solr.apache.org/ Elasticsearchhttps://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整合
引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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 ElasticsearchRepositoryEsBlog, String {PageEsBlog 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 月;PageEsBlog 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 ListEsBlog 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);PageEsBlog 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{requestLinePOST /blog/_doc?timeout1m HTTP/1.1, hosthttp://localhost:9200, responseHTTP/1.1 201 Created} es服务器的响应程序解析不了有可能是spring-boot版本低了 spring-boot 2.7.3es: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