1. 程式人生 > >spring中應用模板模式封裝ElasticsearchTemplate

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); 
}