SpringBoot整合ElasticSearch
Posted 陈一一同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合ElasticSearch相关的知识,希望对你有一定的参考价值。
首先springBoot 项目的pom文件中引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
yml文件配置
# Spring配置
spring:
elasticsearch:
uris: 127.0.0.1:9200
我这里是使用继承ElasticsearchRepository来做简单的增删查的
建一个类ArticleRepository继承ElasticsearchRepository
@Repository
public interface ArticleRepository extends ElasticsearchRepository<EsArticleDto, Long>
List<EsArticleDto> findByTitle(String title,String lang);
List<EsArticleDto> findByTitleEnglish(String titleEnglish,String lang);
Page<EsArticleDto> findByTitleAndLangAndCreateDateIntBetween(String title, String lang,Integer from,Integer to, Pageable pageable);
List<EsArticleDto> findByDetailhtml(String content,String lang);
List<EsArticleDto> findByDetailHtmlEnglish(String contentEnglish,String lang);
List<EsArticleDto> findByTitleOrDetailHtmlAndLang(String title, String detailHtml, String lang);
List<EsArticleDto> findByLangAndCreateDateIntBetween(String lang,Integer from,Integer to,Pageable pageable);
建一个servi类
public interface ArticleService
EsArticleDto save(EsArticleDto articleDto);
void delete(EsArticleDto articleDto);
Iterable<EsArticleDto> getAll();
List<EsArticleDto> findByTitle(String title, String lang);
List<EsArticleDto> findByTitleEnglish(String titleEnglish,String lang);
Page<EsArticleDto> findByTitleAndLangAndCreateDateIntBetween(String title, String lang,Integer from,Integer to, Pageable pageable);
List<EsArticleDto> findByTitleOrDetailHtmlAndLang(String title, String detailHtml, String lang);
List<EsArticleDto> findByDetailHtml(String content,String lang);
List<EsArticleDto> findByDetailHtmlEnglish(String contentEnglish,String lang);
List<EsArticleDto> findByLangAndCreateDateIntBetween(String lang,Integer from,Integer to,Pageable pageable);
serviceimpl
@Service
public class ArticleServiceImpl implements ArticleService
@Autowired
private ArticleRepository articleRepository;
@Override
public EsArticleDto save(EsArticleDto articleDto)
return articleRepository.save(articleDto);
@Override
public void delete(EsArticleDto articleDto)
articleRepository.delete(articleDto);
@Override
public Iterable<EsArticleDto> getAll()
return articleRepository.findAll();
@Override
public List<EsArticleDto> findByTitle(String title, String lang)
return articleRepository.findByTitle(title,lang);
@Override
public List<EsArticleDto> findByTitleEnglish(String titleEnglish, String lang)
return articleRepository.findByTitleEnglish(titleEnglish,lang);
@Override
public Page<EsArticleDto> findByTitleAndLangAndCreateDateIntBetween(String title, String lang,Integer from,Integer to, Pageable pageable)
return articleRepository.findByTitleAndLangAndCreateDateIntBetween(title,lang,from,to,pageable);
@Override
public List<EsArticleDto> findByTitleOrDetailHtmlAndLang(String title, String detailHtml, String lang)
return articleRepository.findByTitleOrDetailHtmlAndLang(title,detailHtml,lang);
@Override
public List<EsArticleDto> findByDetailHtml(String content,String lang)
return articleRepository.findByDetailHtml(content,lang);
@Override
public List<EsArticleDto> findByDetailHtmlEnglish(String contentEnglish, String lang)
return articleRepository.findByDetailHtmlEnglish(contentEnglish,lang);
@Override
public List<EsArticleDto> findByLangAndCreateDateIntBetween(String lang, Integer from, Integer to, Pageable pageable)
return articleRepository.findByLangAndCreateDateIntBetween(lang,from,to,pageable);
在controller里面我这里是根据title搜索的,这里通过那个字段搜索直接加就行了
List<EsArticleDto> title = articleService.findByTitle("*" + article.getTitle() + "*", "*" + article.getTitle() + "*");
以上是关于SpringBoot整合ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot 整合 Elasticsearch 实现海量级数据搜索
Springboot 2.5.x整合ElasticSearch 7.1x
SpringBoot检索篇Ⅳ --- 整合ElasticSearch
SpringBoot整合ElasticSearch7.x及实战