1.查詢
json
@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()); }
該方法主要處理邏輯tomcat
1.加入順序參數mvc
2.設置參數過濾參數app
3.設置高亮顯示參數elasticsearch
結果處理由組件 ResultsMapper 結果映射器處理ide
public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper { 學習
EntityMapper getEntityMapper();
}
ui
public interface SearchResultMapper { this
<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