spring中应用模板模式封装ElasticsearchTemplate
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring中应用模板模式封装ElasticsearchTemplate相关的知识,希望对你有一定的参考价值。
1.查询
?
@Override
public <T> FacetedPage<T> queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper) {
SearchResponse response = doSearch(prepareSearch(query, clazz), query);
return mapper.mapResults(response, clazz, query.getPageable());
}
?
?
?
主要逻辑在doSearch方法中
?
private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) {
if (searchQuery.getFilter() != null) {
searchRequest.setPostFilter(searchQuery.getFilter());
}
if (CollectionUtils.isNotEmpty(searchQuery.getElasticsearchSorts())) {
for (SortBuilder sort : searchQuery.getElasticsearchSorts()) {
searchRequest.addSort(sort);
}
}
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
for (FacetRequest facetRequest : searchQuery.getFacets()) {
FacetBuilder facet = facetRequest.getFacet();
if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) {
facet.facetFilter(searchQuery.getFilter());
}
searchRequest.addFacet(facet);
}
}
if (searchQuery.getHighlightFields() != null) {
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
searchRequest.addHighlightedField(highlightField);
}
}
if (CollectionUtils.isNotEmpty(searchQuery.getAggregations())) {
for (AbstractAggregationBuilder aggregationBuilder : searchQuery.getAggregations()) {
searchRequest.addAggregation(aggregationBuilder);
}
}
return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()).execute());
}
?
该方法主要处理逻辑
1.加入顺序参数
2.设置参数过滤参数
3.设置高亮显示参数
?
?
结果处理由组件?ResultsMapper 结果映射器处理
public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper {
EntityMapper getEntityMapper();
}
?
?
public interface SearchResultMapper {
<T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable);
}
?
初始化映射器
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter, ResultsMapper resultsMapper) {
this.client = client;
this.elasticsearchConverter = (elasticsearchConverter == null) ? new MappingElasticsearchConverter(
new SimpleElasticsearchMappingContext()) : elasticsearchConverter;
this.resultsMapper = (resultsMapper == null) ? new DefaultResultMapper(this.elasticsearchConverter.getMappingContext()) : resultsMapper;
}
?
默认实现的映射器DefaultResultMapper
?
@Override
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
long totalHits = response.getHits().totalHits();
List<T> results = new ArrayList<T>();
for (SearchHit hit : response.getHits()) {
if (hit != null) {
T result = null;
if (!Strings.isNullOrEmpty(hit.sourceAsString())) {
result = mapEntity(hit.sourceAsString(), clazz);
} else {
result = mapEntity(hit.getFields().values(), clazz);
}
setPersistentEntityId(result, hit.getId(), clazz);
results.add(result);
}
}
List<FacetResult> facets = new ArrayList<FacetResult>();
if (response.getFacets() != null) {
for (Facet facet : response.getFacets()) {
FacetResult facetResult = DefaultFacetMapper.parse(facet);
if (facetResult != null) {
facets.add(facetResult);
}
}
}
return new FacetedPageImpl<T>(results, pageable, totalHits, facets);
}
?
?
?
主要是讲json格式转换为指定Class类型bean对象
?
?
##2.自主扩展
?
搜索逻辑绝大部分可以固化封装,只有结果处理需要留出用户扩展空间,可以用回调的方式,定义接口,参数中传入匿名实现
?
@Override
public <T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor) {
SearchResponse response = doSearch(prepareSearch(query), query);
return resultsExtractor.extract(response);
}
?
?
public interface ResultsExtractor<T> {
T extract(SearchResponse response);
}
?
学习视频
?
复制链接,在浏览器打开
tomcat源码解析
https://study.163.com/course/introduction/1209535854.htm
Springmvc源码解析
https://study.163.com/course/introduction/1209536851.htm
dubbo源码解析
https://study.163.com/course/introduction/1209648816.htm
以上是关于spring中应用模板模式封装ElasticsearchTemplate的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot-spring-data-elasticsearch7.12.0
Spring中支持JDBC的JdbcTemplate模板类的使用