Search in sources :

Example 1 with IAggregationQueryManager

use of alien4cloud.dao.IAggregationQueryManager in project alien4cloud by alien4cloud.

the class AbstractToscaIndexSearchService method search.

public FacetedSearchResult search(Class<? extends T> clazz, String query, Integer size, Map<String, String[]> filters) {
    TopHitsBuilder topHitAggregation = AggregationBuilders.topHits("highest_version").setSize(1).addSort(new FieldSortBuilder("nestedVersion.majorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.minorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.incrementalVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.qualifier").order(SortOrder.DESC).missing("_first"));
    AggregationBuilder aggregation = AggregationBuilders.terms("query_aggregation").field(getAggregationField()).size(size).subAggregation(topHitAggregation);
    FacetedSearchResult<? extends T> searchResult = alienDAO.buildSearchQuery(clazz, query).setFilters(FilterUtil.singleKeyFilter(filters, "workspace", AlienConstants.GLOBAL_WORKSPACE_ID)).prepareSearch().setFetchContext(FetchContext.SUMMARY, topHitAggregation).facetedSearch(new IAggregationQueryManager() {

        @Override
        public AggregationBuilder getQueryAggregation() {
            return aggregation;
        }

        @Override
        @SneakyThrows({ IOException.class })
        public void setData(ObjectMapper objectMapper, Function getClassFromType, FacetedSearchResult result, Aggregation aggregation) {
            List<Object> resultData = Lists.newArrayList();
            List<String> resultTypes = Lists.newArrayList();
            if (aggregation == null) {
                result.setData(getArray(0));
                result.setTypes(new String[0]);
            }
            for (Terms.Bucket bucket : safe(((Terms) aggregation).getBuckets())) {
                TopHits topHits = bucket.getAggregations().get("highest_version");
                for (SearchHit hit : topHits.getHits()) {
                    resultTypes.add(hit.getType());
                    resultData.add(objectMapper.readValue(hit.getSourceAsString(), ((Function<String, Class>) getClassFromType).apply(hit.getType())));
                }
            }
            result.setData(resultData.toArray(getArray(resultData.size())));
            result.setTypes(resultTypes.toArray(new String[resultTypes.size()]));
            result.setFrom(0);
            result.setTo(resultData.size());
            if (size == Integer.MAX_VALUE || resultData.size() < size) {
                result.setTotalResults(resultData.size());
            } else {
                // just to show that there is more results to fetch but iteration is not possible through aggregations.
                result.setTotalResults(resultData.size() + ((Terms) aggregation).getSumOfOtherDocCounts());
            }
        }
    });
    return searchResult;
}
Also used : TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SneakyThrows(lombok.SneakyThrows) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Function(java.util.function.Function) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) IAggregationQueryManager(alien4cloud.dao.IAggregationQueryManager) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult)

Aggregations

IAggregationQueryManager (alien4cloud.dao.IAggregationQueryManager)1 FacetedSearchResult (alien4cloud.dao.model.FacetedSearchResult)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 Function (java.util.function.Function)1 SneakyThrows (lombok.SneakyThrows)1 SearchHit (org.elasticsearch.search.SearchHit)1 Aggregation (org.elasticsearch.search.aggregations.Aggregation)1 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)1 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)1 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)1 TopHitsBuilder (org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder)1 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)1