Search in sources :

Example 41 with IndexReader

use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.

the class HDRPercentileRanksAggregatorTests method testEmpty.

public void testEmpty() throws IOException {
    PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.HDR).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 42 with IndexReader

use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.

the class HDRPercentileRanksAggregatorTests 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.HDR).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);
            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);
            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 43 with IndexReader

use of org.apache.lucene.index.IndexReader 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 44 with IndexReader

use of org.apache.lucene.index.IndexReader 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 45 with IndexReader

use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.

the class ScriptedMetricAggregatorTests method testScriptedMetricWithoutCombine.

/**
     * without combine script, the "_aggs" map should contain a list of the size of the number of documents matched
     */
@SuppressWarnings("unchecked")
public void testScriptedMetricWithoutCombine() throws IOException {
    try (Directory directory = newDirectory()) {
        int numDocs = randomInt(100);
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < numDocs; i++) {
                indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i)));
            }
        }
        try (IndexReader indexReader = DirectoryReader.open(directory)) {
            ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME);
            aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT);
            ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
            assertEquals(AGG_NAME, scriptedMetric.getName());
            assertNotNull(scriptedMetric.aggregation());
            Map<String, Object> agg = (Map<String, Object>) scriptedMetric.aggregation();
            assertEquals(numDocs, ((List<Integer>) agg.get("collector")).size());
        }
    }
}
Also used : MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) HashMap(java.util.HashMap) Map(java.util.Map) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)962 Document (org.apache.lucene.document.Document)610 Directory (org.apache.lucene.store.Directory)603 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)549 IndexSearcher (org.apache.lucene.search.IndexSearcher)410 Term (org.apache.lucene.index.Term)332 TopDocs (org.apache.lucene.search.TopDocs)204 TermQuery (org.apache.lucene.search.TermQuery)160 Query (org.apache.lucene.search.Query)158 IndexWriter (org.apache.lucene.index.IndexWriter)150 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)142 Field (org.apache.lucene.document.Field)135 BytesRef (org.apache.lucene.util.BytesRef)134 IOException (java.io.IOException)133 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 ArrayList (java.util.ArrayList)108 TextField (org.apache.lucene.document.TextField)81 Test (org.junit.Test)81