spring中如何使用模式封装ElasticsearchTemplate
spring中如何使用模式封装ElasticsearchTemplate?这篇文章运用了实例代码展示,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联建站专注于库车企业网站建设,成都响应式网站建设,商城开发。库车网站建设公司,为库车等地区提供建站服务。全流程按需定制设计,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
1.查询
@Override
public FacetedPage queryForPage(SearchQuery query, Class 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 {
FacetedPage mapResults(SearchResponse response, Class 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 FacetedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) {
long totalHits = response.getHits().totalHits();
List results = new ArrayList();
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 facets = new ArrayList();
if (response.getFacets() != null) {
for (Facet facet : response.getFacets()) {
FacetResult facetResult = DefaultFacetMapper.parse(facet);
if (facetResult != null) {
facets.add(facetResult);
}
}
}
return new FacetedPageImpl(results, pageable, totalHits, facets);
}
主要是讲json格式转换为指定Class类型bean对象
##2.自主扩展
搜索逻辑绝大部分可以固化封装,只有结果处理需要留出用户扩展空间,可以用回调的方式,定义接口,参数中传入匿名实现
@Override
public
SearchResponse response = doSearch(prepareSearch(query), query);
return resultsExtractor.extract(response);
}
public interface ResultsExtractor
T extract(SearchResponse response);
}
以上就是spring中使用模式封装ElasticsearchTemplate的方法,详细使用情况还需要大家自己亲自动手使用过才能领会。如果想了解更多相关内容,欢迎关注创新互联行业资讯频道!
分享题目:spring中如何使用模式封装ElasticsearchTemplate
当前URL:http://azwzsj.com/article/ggojcp.html