Search in sources :

Example 6 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testMultiValuedFieldWithValueScriptReverse.

public void testMultiValuedFieldWithValueScriptReverse() throws Exception {
    // test what happens when values arrive in reverse order since the min
    // aggregator is optimized to work on sorted values
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value * -1", emptyMap()))).get();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(-12d));
}
Also used : Script(org.elasticsearch.script.Script) Min(org.elasticsearch.search.aggregations.metrics.min.Min) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testMultiValuedFieldWithValueScript.

@Override
public void testMultiValuedFieldWithValueScript() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(min("min").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - 1", emptyMap()))).get();
    assertHitCount(searchResponse, 10);
    Min min = searchResponse.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(1.0));
}
Also used : Script(org.elasticsearch.script.Script) Min(org.elasticsearch.search.aggregations.metrics.min.Min) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project sonarqube by SonarSource.

the class IssueIndex method getMinCreatedAt.

private Optional<Long> getMinCreatedAt(Map<String, QueryBuilder> filters, QueryBuilder esQuery) {
    String facetNameAndField = IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT;
    SearchRequestBuilder esRequest = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSize(0);
    BoolQueryBuilder esFilter = boolQuery();
    filters.values().stream().filter(Objects::nonNull).forEach(esFilter::must);
    if (esFilter.hasClauses()) {
        esRequest.setQuery(QueryBuilders.filteredQuery(esQuery, esFilter));
    } else {
        esRequest.setQuery(esQuery);
    }
    esRequest.addAggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
    Min minValue = esRequest.get().getAggregations().get(facetNameAndField);
    Double actualValue = minValue.getValue();
    if (actualValue.isInfinite()) {
        return Optional.empty();
    }
    return Optional.of(actualValue.longValue());
}
Also used : Min(org.elasticsearch.search.aggregations.metrics.min.Min) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 9 with Min

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

Example 10 with Min

use of org.elasticsearch.search.aggregations.metrics.min.Min in project elasticsearch by elastic.

the class MinIT method testSingleValuedFieldPartiallyUnmapped.

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

Aggregations

Min (org.elasticsearch.search.aggregations.metrics.min.Min)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)17 Script (org.elasticsearch.script.Script)8 HashMap (java.util.HashMap)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)2 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1