Search in sources :

Example 16 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project metron by apache.

the class ElasticsearchDao method getFacetCounts.

public Map<String, Map<String, Long>> getFacetCounts(List<String> fields, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) {
    Map<String, Map<String, Long>> fieldCounts = new HashMap<>();
    for (String field : fields) {
        Map<String, Long> valueCounts = new HashMap<>();
        Aggregation aggregation = aggregations.get(getFacetAggregationName(field));
        if (aggregation instanceof Terms) {
            Terms terms = (Terms) aggregation;
            terms.getBuckets().stream().forEach(bucket -> valueCounts.put(formatKey(bucket.getKey(), commonColumnMetadata.get(field)), bucket.getDocCount()));
        }
        fieldCounts.put(field, valueCounts);
    }
    return fieldCounts;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Map(java.util.Map) HashMap(java.util.HashMap)

Example 17 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually.

@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
    final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
    final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
    final Map<String, Aggregation> results = response.getAggregations().asMap();
    final StringTerms topTags = (StringTerms) results.get("top_tags");
    final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
    assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 18 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project metasfresh-webui-api by metasfresh.

the class KPIDataLoader method loadData.

private void loadData(final KPIDataResult.Builder data, final TimeRange timeRange) {
    logger.trace("Loading data for {}", timeRange);
    // 
    // Create query evaluation context
    final Evaluatee evalCtx = Evaluatees.mapBuilder().put("MainFromMillis", data.getRange().getFromMillis()).put("MainToMillis", data.getRange().getToMillis()).put("FromMillis", timeRange.getFromMillis()).put("ToMillis", timeRange.getToMillis()).build().andComposeWith(Evaluatees.ofCtx(Env.getCtx()));
    // 
    // Resolve esQuery's variables
    final IStringExpression esQuery = kpi.getESQuery();
    final String esQueryParsed = esQuery.evaluate(evalCtx, OnVariableNotFound.Preserve);
    // 
    // Execute the query
    final SearchResponse response;
    try {
        logger.trace("Executing: \n{}", esQueryParsed);
        response = elasticsearchClient.prepareSearch(kpi.getESSearchIndex()).setTypes(kpi.getESSearchTypes()).setSource(esQueryParsed).get();
        logger.trace("Got response: \n{}", response);
    } catch (final NoNodeAvailableException e) {
        // elastic search transport error => nothing to do about it
        throw e;
    } catch (final Exception e) {
        throw new AdempiereException("Failed executing query for " + this + ": " + e.getLocalizedMessage() + "\nQuery: " + esQueryParsed, e);
    }
    // Fetch data
    try {
        final List<Aggregation> aggregations = response.getAggregations().asList();
        for (final Aggregation agg : aggregations) {
            if (agg instanceof MultiBucketsAggregation) {
                final String aggName = agg.getName();
                final MultiBucketsAggregation multiBucketsAggregation = (MultiBucketsAggregation) agg;
                for (final Bucket bucket : multiBucketsAggregation.getBuckets()) {
                    final Object key = dataSetValueKeyExtractor.apply(bucket, timeRange);
                    for (final KPIField field : kpi.getFields()) {
                        final Object value = field.getBucketValueExtractor().extractValue(aggName, bucket);
                        final Object jsonValue = formatValue(field, value);
                        if (jsonValue == null) {
                            continue;
                        }
                        final String fieldName = fieldNameExtractor.apply(field, timeRange);
                        data.putValue(aggName, key, fieldName, jsonValue);
                    }
                    // 
                    // Make sure the groupByField's value is present in our dataSet value.
                    // If not exist, we can use the key as it's value.
                    final KPIField groupByField = kpi.getGroupByFieldOrNull();
                    if (groupByField != null) {
                        data.putValueIfAbsent(aggName, key, groupByField.getFieldName(), key);
                    }
                }
            } else if (agg instanceof NumericMetricsAggregation.SingleValue) {
                final NumericMetricsAggregation.SingleValue singleValueAggregation = (NumericMetricsAggregation.SingleValue) agg;
                // N/A
                final String key = "NO_KEY";
                for (final KPIField field : kpi.getFields()) {
                    final Object value;
                    if ("value".equals(field.getESPathAsString())) {
                        value = singleValueAggregation.value();
                    } else {
                        throw new IllegalStateException("Only ES path ending with 'value' allowed for field: " + field);
                    }
                    final Object jsonValue = field.convertValueToJson(value);
                    data.putValue(agg.getName(), key, field.getFieldName(), jsonValue);
                }
            } else {
                new AdempiereException("Aggregation type not supported: " + agg.getClass()).throwIfDeveloperModeOrLogWarningElse(logger);
            }
        }
    } catch (final Exception e) {
        throw new AdempiereException(e.getLocalizedMessage() + "\n KPI: " + this + "\n Query: " + esQueryParsed + "\n Response: " + response, e);
    }
}
Also used : NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) AdempiereException(org.adempiere.exceptions.AdempiereException) Evaluatee(org.compiere.util.Evaluatee) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) AdempiereException(org.adempiere.exceptions.AdempiereException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) IStringExpression(org.adempiere.ad.expression.api.IStringExpression)

Example 19 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method searchResponse.

private SearchResponse<T> searchResponse(org.elasticsearch.action.search.SearchResponse response) {
    SearchHit[] hits = response.getHits().getHits();
    List<T> items = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        items.add(reader.fromJSON(BytesReference.toBytes(hit.getSourceRef())));
    }
    Aggregations aggregationResponse = response.getAggregations();
    Map<String, Aggregation> aggregations = aggregationResponse == null ? Maps.newHashMap() : aggregationResponse.asMap();
    return new SearchResponse<>(items, response.getHits().getTotalHits(), aggregations);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SearchHit(org.elasticsearch.search.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) SearchResponse(core.framework.search.SearchResponse)

Example 20 with Aggregation

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

the class ESFacetedQueryResultBuilder method createCluster.

private Map<FacetValue, DtList<I>> createCluster(final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
    final Map<FacetValue, DtList<I>> resultCluster = new LinkedHashMap<>();
    final FacetDefinition facetDefinition = searchQuery.getClusteringFacetDefinition();
    final Aggregation facetAggregation = queryResponse.getAggregations().get(facetDefinition.getName());
    if (facetDefinition.isRangeFacet()) {
        // Cas des facettes par 'range'
        final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
        for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
            final Bucket value = getBucketByKey(multiBuckets, facetRange.getListFilter().getFilterValue());
            populateCluster(value, facetRange, resultCluster, dtcIndex, resultHighlights);
        }
    } else {
        // Cas des facettes par 'term'
        final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
        FacetValue facetValue;
        for (final Bucket bucket : multiBuckets.getBuckets()) {
            final String term = bucket.getKeyAsString();
            final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
            final MessageText label = MessageText.of(term);
            facetValue = new FacetValue(term, ListFilter.of(query), label);
            populateCluster(bucket, facetValue, resultCluster, dtcIndex, resultHighlights);
        }
    }
    return resultCluster;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MessageText(io.vertigo.core.locale.MessageText) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)47 SearchResponse (org.elasticsearch.action.search.SearchResponse)23 HashMap (java.util.HashMap)21 List (java.util.List)20 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)16 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)10 Map (java.util.Map)9 Test (org.junit.Test)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)6 DateTime (org.joda.time.DateTime)6 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