Search in sources :

Example 16 with PercentileRanks

use of org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks in project elasticsearch by elastic.

the class TDigestPercentileRanksAggregatorTests method testEmpty.

public void testEmpty() throws IOException {
    PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.TDIGEST).values(0.5);
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
    fieldType.setName("field");
    try (IndexReader reader = new MultiReader()) {
        IndexSearcher searcher = new IndexSearcher(reader);
        PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
        Percentile rank = ranks.iterator().next();
        assertEquals(Double.NaN, rank.getPercent(), 0d);
        assertEquals(0.5, rank.getValue(), 0d);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) MultiReader(org.apache.lucene.index.MultiReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 17 with PercentileRanks

use of org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks in project elasticsearch by elastic.

the class TDigestPercentileRanksAggregatorTests method testSimple.

public void testSimple() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3, 0.2, 10 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.TDIGEST).values(0.1, 0.5, 12);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            Iterator<Percentile> rankIterator = ranks.iterator();
            Percentile rank = rankIterator.next();
            assertEquals(0.1, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~15
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(0d));
            rank = rankIterator.next();
            assertEquals(0.5, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.greaterThan(0d));
            assertThat(rank.getPercent(), Matchers.lessThan(100d));
            rank = rankIterator.next();
            assertEquals(12, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~59
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(100d));
            assertFalse(rankIterator.hasNext());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 18 with PercentileRanks

use of org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks in project elasticsearch by elastic.

the class TDigestPercentileRanksIT method testScriptMultiValuedWithParams.

@Override
public void testScriptMultiValuedWithParams() throws Exception {
    Script script = AggregationTestScriptsPlugin.DECREMENT_ALL_VALUES;
    final double[] pcts = randomPercents(minValues - 1, maxValues - 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(randomCompression(percentileRanks("percentile_ranks")).script(script).values(pcts)).execute().actionGet();
    assertHitCount(searchResponse, 10);
    final PercentileRanks values = searchResponse.getAggregations().get("percentile_ranks");
    assertConsistent(pcts, values, minValues - 1, maxValues - 1);
}
Also used : Script(org.elasticsearch.script.Script) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with PercentileRanks

use of org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks in project elasticsearch by elastic.

the class TDigestPercentileRanksIT method testSingleValuedFieldWithValueScriptWithParams.

@Override
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    final double[] pcts = randomPercents(minValue - 1, maxValue - 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(randomCompression(percentileRanks("percentile_ranks")).field("value").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params)).values(pcts)).execute().actionGet();
    assertHitCount(searchResponse, 10);
    final PercentileRanks values = searchResponse.getAggregations().get("percentile_ranks");
    assertConsistent(pcts, values, minValue - 1, maxValue - 1);
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with PercentileRanks

use of org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks in project elasticsearch by elastic.

the class TDigestPercentileRanksIT method testMultiValuedField.

@Override
public void testMultiValuedField() throws Exception {
    final double[] pcts = randomPercents(minValues, maxValues);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(randomCompression(percentileRanks("percentile_ranks")).field("values").values(pcts)).execute().actionGet();
    assertHitCount(searchResponse, 10);
    final PercentileRanks values = searchResponse.getAggregations().get("percentile_ranks");
    assertConsistent(pcts, values, minValues, maxValues);
}
Also used : PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)40 SearchResponse (org.elasticsearch.action.search.SearchResponse)36 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)36 Script (org.elasticsearch.script.Script)18 HashMap (java.util.HashMap)6 IndexReader (org.apache.lucene.index.IndexReader)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)4 Percentile (org.elasticsearch.search.aggregations.metrics.percentiles.Percentile)4 PercentileRanksAggregationBuilder (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder)4 Document (org.apache.lucene.document.Document)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 MultiReader (org.apache.lucene.index.MultiReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 Directory (org.apache.lucene.store.Directory)2 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)2 Global (org.elasticsearch.search.aggregations.bucket.global.Global)2 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2