大家好,我是小悟
ElasticSearch是一个分布式的开源搜索和分析引擎,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据。
Elasticsearch 以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名,是 Elastic Stack 的核心组件;Elastic Stack 是适用于数据采集、充实、存储、分析和可视化的一组开源工具。
人们通常将 Elastic Stack 称为 ELK Stack(代指 Elasticsearch、Logstash 和 Kibana),目前 Elastic Stack 包括一系列丰富的轻量型数据采集代理,这些代理统称为 Beats,可用来向 Elasticsearch 发送数据。
这篇内容主要来说说SpringBoot整合elasticsearch存储抖音短视频数据,使得查询海量视频数据变得又快又顺畅。通过接口获取到抖音短视频数据时,调用save方法存储到elasticsearch。然后前端再调用pageQuery方法查询数据回显。
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访问的端口,切记不要写错了哦。
做完elasticsearch的基本配置后,就可以尽情的撸接口了。
4、Repository
创建Repository,继承ElasticsearchRepository,类似jpa。这样简单的CRUD就有了
public interface EsHotSentenceVideoRepository extends ElasticsearchRepository<HotSentenceVideoEntity,Long> {
}
5、接口Service
public interface EsHotSentenceVideoService {
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()
.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还可以做分词检索,很方便。
您的一键三连,是我更新的最大动力,谢谢
山水有相逢,来日皆可期,谢谢阅读,我们再会
我手中的金箍棒,上能通天,下能探海
标签:return,SpringBoot,elasticsearch,实操,new,esHotSentenceVideoRepository,public,Strin From: https://blog.51cto.com/wukongmazi/6144703