Search in sources :

Example 6 with Max

use of org.elasticsearch.search.aggregations.metrics.max.Max in project elasticsearch by elastic.

the class MaxIT method testSingleValuedFieldWithFormatter.

public void testSingleValuedFieldWithFormatter() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(max("max").format("0000.0").field("value")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Max max = searchResponse.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    assertThat(max.getValue(), equalTo(10.0));
    assertThat(max.getValueAsString(), equalTo("0010.0"));
}
Also used : Max(org.elasticsearch.search.aggregations.metrics.max.Max) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with Max

use of org.elasticsearch.search.aggregations.metrics.max.Max in project elasticsearch by elastic.

the class MaxIT method testMultiValuedField.

@Override
public void testMultiValuedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(max("max").field("values")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Max max = searchResponse.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    assertThat(max.getValue(), equalTo(12.0));
}
Also used : Max(org.elasticsearch.search.aggregations.metrics.max.Max) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with Max

use of org.elasticsearch.search.aggregations.metrics.max.Max in project elasticsearch by elastic.

the class MaxIT method testScriptMultiValuedWithParams.

@Override
public void testScriptMultiValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "[ doc['value'].value, doc['value'].value + inc ]", params);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(max("max").script(script)).get();
    assertHitCount(searchResponse, 10);
    Max max = searchResponse.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    assertThat(max.getValue(), equalTo(11.0));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Max(org.elasticsearch.search.aggregations.metrics.max.Max) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with Max

use of org.elasticsearch.search.aggregations.metrics.max.Max in project sonarqube by SonarSource.

the class EsClient method getMaxFieldValue.

public long getMaxFieldValue(IndexType indexType, String fieldName) {
    SearchRequestBuilder request = prepareSearch(indexType).setQuery(QueryBuilders.matchAllQuery()).setSize(0).addAggregation(AggregationBuilders.max("latest").field(fieldName));
    Max max = request.get().getAggregations().get("latest");
    return (long) max.getValue();
}
Also used : ProxySearchRequestBuilder(org.sonar.server.es.request.ProxySearchRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Max(org.elasticsearch.search.aggregations.metrics.max.Max)

Example 10 with Max

use of org.elasticsearch.search.aggregations.metrics.max.Max in project graylog2-server by Graylog2.

the class Indices method indexRangeStatsOfIndex.

/**
     * Calculate min and max message timestamps in the given index.
     *
     * @param index Name of the index to query.
     * @return the timestamp stats in the given index, or {@code null} if they couldn't be calculated.
     * @see org.elasticsearch.search.aggregations.metrics.stats.Stats
     */
public IndexRangeStats indexRangeStatsOfIndex(String index) {
    final FilterAggregationBuilder builder = AggregationBuilders.filter("agg").filter(QueryBuilders.existsQuery("timestamp")).subAggregation(AggregationBuilders.min("ts_min").field("timestamp")).subAggregation(AggregationBuilders.max("ts_max").field("timestamp")).subAggregation(AggregationBuilders.terms("streams").field("streams"));
    final SearchRequestBuilder srb = c.prepareSearch().setIndices(index).setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0).addAggregation(builder);
    final SearchResponse response;
    try {
        final SearchRequest request = srb.request();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Index range query: _search/{}: {}", index, XContentHelper.convertToJson(request.source(), false));
        }
        response = c.search(request).actionGet();
    } catch (IndexClosedException e) {
        throw e;
    } catch (org.elasticsearch.index.IndexNotFoundException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw e;
    } catch (ElasticsearchException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw new org.elasticsearch.index.IndexNotFoundException("Index " + index + " not found", e);
    } catch (IOException e) {
        // the index range aggregation query on DEBUG (via XContentHelper)
        throw new RuntimeException(e);
    }
    final Filter f = response.getAggregations().get("agg");
    if (f.getDocCount() == 0L) {
        LOG.debug("No documents with attribute \"timestamp\" found in index <{}>", index);
        return IndexRangeStats.EMPTY;
    }
    final Min minAgg = f.getAggregations().get("ts_min");
    final DateTime min = new DateTime((long) minAgg.getValue(), DateTimeZone.UTC);
    final Max maxAgg = f.getAggregations().get("ts_max");
    final DateTime max = new DateTime((long) maxAgg.getValue(), DateTimeZone.UTC);
    // make sure we return an empty list, so we can differentiate between old indices that don't have this information
    // and newer ones that simply have no streams.
    ImmutableList.Builder<String> streamIds = ImmutableList.builder();
    final Terms streams = f.getAggregations().get("streams");
    if (!streams.getBuckets().isEmpty()) {
        streamIds.addAll(streams.getBuckets().stream().map(Terms.Bucket::getKeyAsString).collect(toSet()));
    }
    return IndexRangeStats.create(min, max, streamIds.build());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Max(org.elasticsearch.search.aggregations.metrics.max.Max) ImmutableList(com.google.common.collect.ImmutableList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) Min(org.elasticsearch.search.aggregations.metrics.min.Min) IndexClosedException(org.elasticsearch.indices.IndexClosedException) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter)

Aggregations

Max (org.elasticsearch.search.aggregations.metrics.max.Max)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)23 Script (org.elasticsearch.script.Script)8 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)7 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)5 HashMap (java.util.HashMap)4 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)2 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 IndexClosedException (org.elasticsearch.indices.IndexClosedException)1 SearchHits (org.elasticsearch.search.SearchHits)1 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)1