Search in sources :

Example 1 with TopHitsBuilder

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder 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)

Example 2 with TopHitsBuilder

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder in project sonarqube by SonarSource.

the class ComponentIndex method createSubAggregation.

private static TopHitsBuilder createSubAggregation(ComponentIndexQuery query) {
    TopHitsBuilder sub = AggregationBuilders.topHits(DOCS_AGGREGATION_NAME);
    query.getLimit().ifPresent(sub::setSize);
    return sub.setFetchSource(false);
}
Also used : TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder)

Example 3 with TopHitsBuilder

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method appendFacetDefinition.

private static void appendFacetDefinition(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final SearchIndexDefinition myIndexDefinition, final DtListState myListState) {
    Assertion.checkNotNull(searchRequestBuilder);
    // On ajoute le cluster, si présent
    if (searchQuery.isClusteringFacet()) {
        // si il y a un cluster on le place en premier
        final FacetDefinition clusteringFacetDefinition = searchQuery.getClusteringFacetDefinition();
        final AggregationBuilder<?> aggregationBuilder = facetToAggregationBuilder(clusteringFacetDefinition);
        final TopHitsBuilder topHitsBuilder = AggregationBuilders.topHits(TOPHITS_SUBAGGREGATION_NAME).setSize(myListState.getMaxRows().orElse(TOPHITS_SUBAGGREGATION_SIZE)).setFrom(myListState.getSkipRows()).setHighlighterNumOfFragments(3).addHighlightedField("*");
        if (myListState.getSortFieldName().isPresent()) {
            topHitsBuilder.addSort(getFieldSortBuilder(myIndexDefinition, myListState));
        }
        aggregationBuilder.subAggregation(topHitsBuilder);
        // We fetch source, because it's our only source to create result list
        searchRequestBuilder.addAggregation(aggregationBuilder);
    }
    // Puis les facettes liées à la query, si présent
    if (searchQuery.getFacetedQuery().isPresent()) {
        final FacetedQueryDefinition facetedQueryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
        final Collection<FacetDefinition> facetDefinitions = new ArrayList<>(facetedQueryDefinition.getFacetDefinitions());
        if (searchQuery.isClusteringFacet() && facetDefinitions.contains(searchQuery.getClusteringFacetDefinition())) {
            facetDefinitions.remove(searchQuery.getClusteringFacetDefinition());
        }
        for (final FacetDefinition facetDefinition : facetDefinitions) {
            final AggregationBuilder<?> aggregationBuilder = facetToAggregationBuilder(facetDefinition);
            searchRequestBuilder.addAggregation(aggregationBuilder);
        }
    }
}
Also used : TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)

Aggregations

TopHitsBuilder (org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder)3 IAggregationQueryManager (alien4cloud.dao.IAggregationQueryManager)1 FacetedSearchResult (alien4cloud.dao.model.FacetedSearchResult)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)1 FacetedQueryDefinition (io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)1 ArrayList (java.util.ArrayList)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 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)1