Search in sources :

Example 21 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project vertigo by KleeGroup.

the class ESFacetedQueryResultBuilder method createFacetList.

private static List<Facet> createFacetList(final SearchQuery searchQuery, final SearchResponse queryResponse) {
    final List<Facet> facets = new ArrayList<>();
    if (searchQuery.getFacetedQuery().isPresent() && queryResponse.getAggregations() != null) {
        final FacetedQueryDefinition queryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
        for (final FacetDefinition facetDefinition : queryDefinition.getFacetDefinitions()) {
            final Aggregation aggregation = obtainAggregation(queryResponse, facetDefinition.getName());
            if (aggregation != null) {
                final Facet facet = createFacet(facetDefinition, (MultiBucketsAggregation) aggregation);
                facets.add(facet);
            }
        }
    }
    return facets;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 22 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reduceStatisticsResults.

private static StatisticsResult reduceStatisticsResults(List<Aggregation> aggs) {
    List<StatisticsResult> results = new ArrayList<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof ExtendedStats) {
            ExtendedStats extendedStats = (ExtendedStats) agg;
            long count = extendedStats.getCount();
            double sum = extendedStats.getSum();
            double min = extendedStats.getMin();
            double max = extendedStats.getMax();
            double standardDeviation = extendedStats.getStdDeviation();
            results.add(new StatisticsResult(count, sum, min, max, standardDeviation));
        } else {
            throw new VertexiumException("Aggregation is not a statistics: " + agg.getClass().getName());
        }
    }
    return StatisticsResult.combine(results);
}
Also used : MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExtendedStats) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) VertexiumException(org.vertexium.VertexiumException)

Example 23 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation 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 24 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project zeppelin by apache.

the class ElasticsearchInterpreter method buildAggResponseMessage.

private InterpreterResult buildAggResponseMessage(Aggregations aggregations) {
    // Only the result of the first aggregation is returned
    // 
    final Aggregation agg = aggregations.asList().get(0);
    InterpreterResult.Type resType = InterpreterResult.Type.TEXT;
    String resMsg = "";
    if (agg instanceof InternalMetricsAggregation) {
        resMsg = XContentHelper.toString((InternalMetricsAggregation) agg).toString();
    } else if (agg instanceof InternalSingleBucketAggregation) {
        resMsg = XContentHelper.toString((InternalSingleBucketAggregation) agg).toString();
    } else if (agg instanceof InternalMultiBucketAggregation) {
        final Set<String> headerKeys = new HashSet<>();
        final List<Map<String, Object>> buckets = new LinkedList<>();
        final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
        for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
            try {
                final XContentBuilder builder = XContentFactory.jsonBuilder();
                bucket.toXContent(builder, null);
                final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(builder.string());
                headerKeys.addAll(bucketMap.keySet());
                buckets.add(bucketMap);
            } catch (final IOException e) {
                logger.error("Processing bucket: " + e.getMessage(), e);
            }
        }
        final StringBuffer buffer = new StringBuffer();
        final String[] keys = headerKeys.toArray(new String[0]);
        for (final String key : keys) {
            buffer.append("\t" + key);
        }
        buffer.deleteCharAt(0);
        for (final Map<String, Object> bucket : buckets) {
            buffer.append("\n");
            for (final String key : keys) {
                buffer.append(bucket.get(key)).append("\t");
            }
            buffer.deleteCharAt(buffer.length() - 1);
        }
        resType = InterpreterResult.Type.TABLE;
        resMsg = buffer.toString();
    }
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, resType, resMsg);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IOException(java.io.IOException) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) LinkedList(java.util.LinkedList) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) JsonObject(com.google.gson.JsonObject) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Example 25 with Aggregation

use of org.elasticsearch.search.aggregations.Aggregation in project zeppelin by apache.

the class TransportBasedClient method setAggregations.

private void setAggregations(Aggregations aggregations, ActionResponse actionResp) {
    // Only the result of the first aggregation is returned
    // 
    final Aggregation agg = aggregations.asList().get(0);
    if (agg instanceof InternalMetricsAggregation) {
        actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE, XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
    } else if (agg instanceof InternalSingleBucketAggregation) {
        actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE, XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
    } else if (agg instanceof InternalMultiBucketAggregation) {
        final Set<String> headerKeys = new HashSet<>();
        final List<Map<String, Object>> buckets = new LinkedList<>();
        final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
        for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
            try {
                final XContentBuilder builder = XContentFactory.jsonBuilder();
                bucket.toXContent(builder, null);
                actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
            } catch (final IOException e) {
            // Ignored
            }
        }
    }
}
Also used : AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) IOException(java.io.IOException) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) LinkedList(java.util.LinkedList) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)53 HashMap (java.util.HashMap)24 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)19 List (java.util.List)18 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)17 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 Map (java.util.Map)10 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregation (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation)5 VertexiumException (org.vertexium.VertexiumException)5 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 IOException (java.io.IOException)4 SearchHit (org.elasticsearch.search.SearchHit)4 SearchHits (org.elasticsearch.search.SearchHits)4 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)4 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)4