Search in sources :

Example 66 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class HistogramAggregatorTests method testMissing.

public void testMissing() throws Exception {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", value));
            w.addDocument(doc);
            w.addDocument(new Document());
        }
        HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5).missing(2d);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            Histogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            assertEquals(4, histogram.getBuckets().size());
            assertEquals(-10d, histogram.getBuckets().get(0).getKey());
            assertEquals(2, histogram.getBuckets().get(0).getDocCount());
            assertEquals(0d, histogram.getBuckets().get(1).getKey());
            assertEquals(7, histogram.getBuckets().get(1).getDocCount());
            assertEquals(5d, histogram.getBuckets().get(2).getKey());
            assertEquals(2, histogram.getBuckets().get(2).getDocCount());
            assertEquals(50d, histogram.getBuckets().get(3).getKey());
            assertEquals(1, histogram.getBuckets().get(3).getDocCount());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 67 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class HistogramAggregatorTests method testExtendedBounds.

public void testExtendedBounds() throws Exception {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3.2, -5, -4.5, 4.3 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5).extendedBounds(-12, 13);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            Histogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            assertEquals(6, histogram.getBuckets().size());
            assertEquals(-15d, histogram.getBuckets().get(0).getKey());
            assertEquals(0, histogram.getBuckets().get(0).getDocCount());
            assertEquals(-10d, histogram.getBuckets().get(1).getKey());
            assertEquals(0, histogram.getBuckets().get(1).getDocCount());
            assertEquals(-5d, histogram.getBuckets().get(2).getKey());
            assertEquals(2, histogram.getBuckets().get(2).getDocCount());
            assertEquals(0d, histogram.getBuckets().get(3).getKey());
            assertEquals(2, histogram.getBuckets().get(3).getDocCount());
            assertEquals(5d, histogram.getBuckets().get(4).getKey());
            assertEquals(0, histogram.getBuckets().get(4).getDocCount());
            assertEquals(10d, histogram.getBuckets().get(5).getKey());
            assertEquals(0, histogram.getBuckets().get(5).getDocCount());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 68 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class ScriptedMetricAggregatorTests method testScriptedMetricWithCombine.

/**
     * test that combine script sums the list produced by the "mapScript"
     */
public void testScriptedMetricWithCombine() throws IOException {
    try (Directory directory = newDirectory()) {
        Integer 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).combineScript(COMBINE_SCRIPT);
            ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
            assertEquals(AGG_NAME, scriptedMetric.getName());
            assertNotNull(scriptedMetric.aggregation());
            assertEquals(numDocs, scriptedMetric.aggregation());
        }
    }
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 69 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseLong.

public void testCollapseLong() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Long>() {

        @Override
        public Long randomGroup(int maxGroup) {
            return randomNonNegativeLong() % maxGroup;
        }

        @Override
        public void add(Document doc, Long value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", value));
            } else {
                doc.add(new NumericDocValuesField("field", value));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.LONG);
            } else {
                return new SortField("field", SortField.Type.LONG);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Example 70 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseFloat.

public void testCollapseFloat() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Float>() {

        @Override
        public Float randomGroup(int maxGroup) {
            return new Float(randomIntBetween(0, maxGroup - 1));
        }

        @Override
        public void add(Document doc, Float value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", NumericUtils.floatToSortableInt(value)));
            } else {
                doc.add(new NumericDocValuesField("field", Float.floatToIntBits(value)));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.FLOAT);
            } else {
                return new SortField("field", SortField.Type.FLOAT);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Aggregations

SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)104 Document (org.apache.lucene.document.Document)86 Directory (org.apache.lucene.store.Directory)72 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)44 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)40 IndexReader (org.apache.lucene.index.IndexReader)35 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)27 BytesRef (org.apache.lucene.util.BytesRef)21 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)19 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)18 StringField (org.apache.lucene.document.StringField)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)18 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)16 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)16 Sort (org.apache.lucene.search.Sort)15 SortField (org.apache.lucene.search.SortField)14 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12 StoredField (org.apache.lucene.document.StoredField)12