springboot中如何集成elasticsearch
发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,这篇文章主要介绍"springboot中如何集成elasticsearch"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"springboot中如何集成elas
千家信息网最后更新 2025年11月16日springboot中如何集成elasticsearch
这篇文章主要介绍"springboot中如何集成elasticsearch"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"springboot中如何集成elasticsearch"文章能帮助大家解决问题。
1,引入依赖
org.springframework.boot spring-boot-starter-data-elasticsearch
2,编写实体映射类
@Data@Document(indexName = "index", createIndex = true)public class Index { @Id private String id; @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart") private String content;}3,编写访问接口(如果需要自动创建索引,该接口必须写,否则项目启动时不会自动检测并创建索引)
@Repositorypublic interface IndexRepository extends ElasticsearchRepository{ Page findByContent(String content, Pageable page);}
4,测试,用了template,和repository两种方式测试
@SpringBootTestpublic class EsTest { @Autowired ElasticsearchRestTemplate esTemplate; @Autowired IndexRepository indexRepository; @BeforeEach public void init() { System.out.println("init"); indexRepository.deleteAll(); indexRepository.saveAll(ListUtil.of( new Index("1","美国留给伊拉克的是个烂摊子吗"), new Index("2","公安部:各地校车将享最高路权"), new Index("3","中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"), new Index("4","中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"), new Index("5","中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索") )); } @Test void testRepositoryQuery() { Page pageList = indexRepository.findByContent("中国", PageRequest.of(0, 10)); pageList.getContent().forEach(e -> { System.out.println("repositoryQuery => "+e); }); } @Test void testTemplateQuery() { BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.simpleQueryStringQuery("中国").field("content")); NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .withPageable(PageRequest.of(0, 10)) .build(); SearchHits search = esTemplate.search(query, Index.class); if(search.hasSearchHits()) { search.getSearchHits().forEach(e -> { System.out.println("templateQuery => "+e.getContent()); }); } }} init datatemplateQuery => Index(id=3, content=中韩渔警冲突调查:韩警平均每天扣1艘中国渔船)templateQuery => Index(id=4, content=中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首)templateQuery => Index(id=5, content=中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索)init datarepositoryQuery => Index(id=3, content=中韩渔警冲突调查:韩警平均每天扣1艘中国渔船)repositoryQuery => Index(id=4, content=中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首)repositoryQuery => Index(id=5, content=中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索)
5,可启动一个定时任务,定时ping,防止Connection time out
@Scheduled(fixedRate = 15000) public void ping() { esTemplate.execute(client -> client.ping(RequestOptions.DEFAULT)); }关于"springboot中如何集成elasticsearch"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
中国
世界
亚裔
全球
天眼
嫌犯
渔船
男子
知识
脉冲
脉冲星
领事
领事馆
洛杉矶
冲突
大赛
开放
搜索
枪击
调查
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
杭州爱租号网络技术有限公司
洛阳银行软件开发待遇
服务器管理口的ip地址
服务器刚买来如何使用
昭通gpu云服务器厂家
信息化网络安全规划
合肥定制软件开发报价
防范网络安全的方法
怎样申请服务器端口
网络安全及数据安全方案
网络技术服务归属什么服务
13年淘宝数据库
分布式数据库统计分析
福建潮流软件开发设施
google软件开发过程
quest数据库软件市值
软件开发做前端工资高吗
数据库营销劣势
网络安全有啥作用
九游互联网络科技有限公司
杭州爱租号网络技术有限公司
延安网络技术操作
网络技术专业用的软件
宽带登录显示无法登陆服务器
网络安全法条例解读
营山县快讯网络技术有限责任公司
软件开发传媒有限公司招聘
上古世纪兽灵男捏脸数据库
按键精灵服务器连接失败
数据库应用管理系统结构