关于ES的简单介绍和基于Linux安装以及遇到的问题和解决方法,已经在 Linux安装Elasticsearch 这篇文章详细说过了,请参考该文章。这篇主要来说说如何在SpringBoot上整合elasticsearch,直接看下面的干货吧。
1、pom文件
引入elasticsearch依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、Config文件
SpringBoot方便的地方就是在于可以自动装配,几个注解就搞定了传统SSM那种繁琐的xml配置,@Configuration,@AutoConfigureBefore,@EnableCaching。
@AutoConfigureBefore(RedisConfig.class)
@EnableCaching
public class ElasticConfig {
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
@Bean(name = "elasticsearchTemplate")
public ElasticsearchTemplate elasticsearchTemplate(Client client,
ElasticsearchConverter converter) {
try {
return new ElasticsearchTemplate(client, converter);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@Bean
public ElasticsearchConverter elasticsearchConverter(
SimpleElasticsearchMappingContext mappingContext) {
return new MappingElasticsearchConverter(mappingContext);
}
@Bean
public SimpleElasticsearchMappingContext mappingContext() {
return new SimpleElasticsearchMappingContext();
}
}
注意,如果你的项目已经配置了redis,务必给AutoConfigureBefore这个注解传一个你的redis配置类,告诉spring,要在redis配置前先配置装载Elastic,否则会报错。
3、application.yml文件
在application.yml文件写以下几行
data:
elasticsearch:
cluster-name: xxx-elastic
cluster-nodes: 服务器ip地址:9300
注意,cluster-name的值要和config/elasticsearch.yml里面写的一致,如果config/elasticsearch.yml里面network.host是0.0.0.0,那么表示可以让任何计算机节点访问到。配置文件里端口是9200,这边是9300,这是因为9200是http访问的端口,9300是Java访问的端口,切记不要写错了哦。
4、Repository
创建Repository,继承ElasticsearchRepository,类似jpa。这样简单的CRUD就有了
public interface EsHotSentenceVideoRepository extends ElasticsearchRepository<HotSentenceVideoEntity,Long> {
}
5、接口Service
HotSentenceVideoEntity save(HotSentenceVideoEntity entity);
void delete(HotSentenceVideoEntity entity);
void deleteAll();
Iterable<HotSentenceVideoEntity> getAll();
List<HotSentenceVideoEntity> getByTitle(String title);
Page<HotSentenceVideoEntity> pageQuery(Integer pageNo, Integer pageSize);
Page<HotSentenceVideoEntity> pageQuery(Integer pageNo, Integer pageSize, String keyWord,String startTime,String endTime);
}
6、实现类ServiceImpl
@Service("esHotSentenceVideoService")
public class EsHotSentenceVideoServiceImpl implements EsHotSentenceVideoService {
@Autowired
private EsHotSentenceVideoRepository esHotSentenceVideoRepository;
@Override
public HotSentenceVideoEntity save(HotSentenceVideoEntity entity) {
return esHotSentenceVideoRepository.save(entity);
}
@Override
public void delete(HotSentenceVideoEntity entity) {
esHotSentenceVideoRepository.delete(entity);
}
@Override
public void deleteAll() {
esHotSentenceVideoRepository.deleteAll();
}
@Override
public Iterable<HotSentenceVideoEntity> getAll() {
return esHotSentenceVideoRepository.findAll();
}
@Override
public List<HotSentenceVideoEntity> getByTitle(String title) {
List<HotSentenceVideoEntity> list = new ArrayList<>();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("title", title);
Iterable<HotSentenceVideoEntity> iterable = esHotSentenceVideoRepository.search(matchQueryBuilder);
iterable.forEach(e->list.add(e));
return list;
}
@Override
public Page<HotSentenceVideoEntity> pageQuery(Integer pageNo, Integer pageSize) {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
// .withQuery(QueryBuilders.matchPhraseQuery("title", kw))
.withPageable(PageRequest.of(pageNo, pageSize))
.build();
return esHotSentenceVideoRepository.search(searchQuery);
}
/**
* @param pageNo
* @param pageSize
* @param keyWord
* @param startTime
* @param endTime
* @return
*/
@Override
public Page<HotSentenceVideoEntity> pageQuery(Integer pageNo, Integer pageSize, String keyWord,String startTime,String endTime) {
Pageable pageable = PageRequest.of(pageNo, pageSize);
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//分页
nativeSearchQueryBuilder.withPageable(pageable);
//过滤
if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
Long startTimeLong = DateUtils.strToLong(startTime);
Long endTimeLong = DateUtils.strToLong(endTime);
boolQueryBuilder.must(QueryBuilders.rangeQuery("publicTime").from(startTimeLong).to(endTimeLong));
nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
}
//搜索
if (StringUtils.isNotEmpty(keyWord)) {
nativeSearchQueryBuilder.withQuery(QueryBuilders.matchPhraseQuery("sentence",keyWord));
}
nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("diggCount").order(SortOrder.DESC));
NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
return esHotSentenceVideoRepository.search(searchQuery);
}
}
7、总结
查询速度,一个字,快,两个字,很快,三个字,超级快,优越性不仅体现在速度上,更值得一提的是ES还可以做分词检索,很方便。
山水有相逢,来日皆可期,谢谢阅读,我们再会
我手中的金箍棒,上能通天,下能探海