Search in sources :

Example 31 with Aggregation

use of org.graylog.shaded.elasticsearch7.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 32 with Aggregation

use of org.graylog.shaded.elasticsearch7.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)

Example 33 with Aggregation

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

the class AggregationPath method resolveValue.

/**
     * Resolves the value pointed by this path given an aggregations root
     *
     * @param root  The root that serves as a point of reference for this path
     * @return      The resolved value
     */
public double resolveValue(HasAggregations root) {
    HasAggregations parent = root;
    double value = Double.NaN;
    for (int i = 0; i < pathElements.size(); i++) {
        AggregationPath.PathElement token = pathElements.get(i);
        Aggregation agg = parent.getAggregations().get(token.name);
        if (agg == null) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Cannot find aggregation named [" + token.name + "]");
        }
        if (agg instanceof SingleBucketAggregation) {
            if (token.key != null && !token.key.equals("doc_count")) {
                throw new IllegalArgumentException("Invalid order path [" + this + "]. Unknown value key [" + token.key + "] for single-bucket aggregation [" + token.name + "]. Either use [doc_count] as key or drop the key all together");
            }
            parent = (SingleBucketAggregation) agg;
            value = ((SingleBucketAggregation) agg).getDocCount();
            continue;
        }
        // the agg can only be a metrics agg, and a metrics agg must be at the end of the path
        if (i != pathElements.size() - 1) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Metrics aggregations cannot have sub-aggregations (at [" + token + ">" + pathElements.get(i + 1) + "]");
        }
        if (agg instanceof InternalNumericMetricsAggregation.SingleValue) {
            if (token.key != null && !token.key.equals("value")) {
                throw new IllegalArgumentException("Invalid order path [" + this + "]. Unknown value key [" + token.key + "] for single-value metric aggregation [" + token.name + "]. Either use [value] as key or drop the key all together");
            }
            parent = null;
            value = ((InternalNumericMetricsAggregation.SingleValue) agg).value();
            continue;
        }
        // we're left with a multi-value metric agg
        if (token.key == null) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Missing value key in [" + token + "] which refers to a multi-value metric aggregation");
        }
        parent = null;
        value = ((InternalNumericMetricsAggregation.MultiValue) agg).value(token.key);
    }
    return value;
}
Also used : SingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation) InternalNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation) HasAggregations(org.elasticsearch.search.aggregations.HasAggregations) SingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation)

Example 34 with Aggregation

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

the class BucketMetricsPipelineAggregator method doReduce.

@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
    preCollection();
    List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
    for (Aggregation aggregation : aggregations) {
        if (aggregation.getName().equals(bucketsPath.get(0))) {
            bucketsPath = bucketsPath.subList(1, bucketsPath.size());
            InternalMultiBucketAggregation<?, ?> multiBucketsAgg = (InternalMultiBucketAggregation<?, ?>) aggregation;
            List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
            for (int i = 0; i < buckets.size(); i++) {
                Bucket bucket = buckets.get(i);
                Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
                if (bucketValue != null && !Double.isNaN(bucketValue)) {
                    collectBucketValue(bucket.getKeyAsString(), bucketValue);
                }
            }
        }
    }
    return buildAggregation(Collections.emptyList(), metaData());
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)

Example 35 with Aggregation

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

the class ScriptedMetricIT method testInitMapCombineReduceWithParamsFile.

public void testInitMapCombineReduceWithParamsFile() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    params.put("vars", varsMap);
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").params(params).initScript(new Script(ScriptType.FILE, CustomScriptPlugin.NAME, "init_script", Collections.emptyMap())).mapScript(new Script(ScriptType.FILE, CustomScriptPlugin.NAME, "map_script", Collections.emptyMap())).combineScript(new Script(ScriptType.FILE, CustomScriptPlugin.NAME, "combine_script", Collections.emptyMap())).reduceScript(new Script(ScriptType.FILE, CustomScriptPlugin.NAME, "reduce_script", Collections.emptyMap()))).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
    Aggregation aggregation = response.getAggregations().get("scripted");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(ScriptedMetric.class));
    ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
    assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
    assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
    assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
    List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
    assertThat(aggregationList.size(), equalTo(1));
    Object object = aggregationList.get(0);
    assertThat(object, notNullValue());
    assertThat(object, instanceOf(Number.class));
    assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayList(java.util.ArrayList) List(java.util.List)

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