Search in sources :

Example 56 with SortedNumericDocValuesField

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

the class MinAggregatorTests method testMinAggregator_noValue.

public void testMinAggregator_noValue() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 7));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 3));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number1", 1));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("_name").field("number2");
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number2");
    try (MinAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        aggregator.preCollection();
        indexSearcher.search(new MatchAllDocsQuery(), aggregator);
        aggregator.postCollection();
        InternalMin result = (InternalMin) aggregator.buildAggregation(0L);
        assertEquals(Double.POSITIVE_INFINITY, result.getValue(), 0);
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) 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 57 with SortedNumericDocValuesField

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

the class DocValuesSliceQueryTests method testSearch.

public void testSearch() throws Exception {
    final int numDocs = randomIntBetween(100, 200);
    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    int max = randomIntBetween(2, 10);
    int[] sliceCounters1 = new int[max];
    int[] sliceCounters2 = new int[max];
    Set<String> keys = new HashSet<>();
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        String uuid = UUIDs.base64UUID();
        int intValue = randomInt();
        long doubleValue = NumericUtils.doubleToSortableLong(randomDouble());
        doc.add(new StringField("uuid", uuid, Field.Store.YES));
        doc.add(new SortedNumericDocValuesField("intField", intValue));
        doc.add(new SortedNumericDocValuesField("doubleField", doubleValue));
        w.addDocument(doc);
        sliceCounters1[Math.floorMod(BitMixer.mix((long) intValue), max)]++;
        sliceCounters2[Math.floorMod(BitMixer.mix(doubleValue), max)]++;
        keys.add(uuid);
    }
    final IndexReader reader = w.getReader();
    final IndexSearcher searcher = newSearcher(reader);
    for (int id = 0; id < max; id++) {
        DocValuesSliceQuery query1 = new DocValuesSliceQuery("intField", id, max);
        assertThat(searcher.count(query1), equalTo(sliceCounters1[id]));
        DocValuesSliceQuery query2 = new DocValuesSliceQuery("doubleField", id, max);
        assertThat(searcher.count(query2), equalTo(sliceCounters2[id]));
        searcher.search(query1, new Collector() {

            @Override
            public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
                return new LeafCollector() {

                    @Override
                    public void setScorer(Scorer scorer) throws IOException {
                    }

                    @Override
                    public void collect(int doc) throws IOException {
                        Document d = context.reader().document(doc, Collections.singleton("uuid"));
                        String uuid = d.get("uuid");
                        assertThat(keys.contains(uuid), equalTo(true));
                        keys.remove(uuid);
                    }
                };
            }

            @Override
            public boolean needsScores() {
                return false;
            }
        });
    }
    assertThat(keys.size(), equalTo(0));
    w.close();
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) LeafCollector(org.apache.lucene.search.LeafCollector) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) LeafCollector(org.apache.lucene.search.LeafCollector) Collector(org.apache.lucene.search.Collector) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Example 58 with SortedNumericDocValuesField

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

the class ExtendedStatsAggregatorTests method testRandomDoubles.

public void testRandomDoubles() throws IOException {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
    ft.setName("field");
    final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator();
    testCase(ft, iw -> {
        int numDocs = randomIntBetween(10, 50);
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            int numValues = randomIntBetween(1, 5);
            for (int j = 0; j < numValues; j++) {
                double value = randomDoubleBetween(-100d, 100d, true);
                long valueAsLong = NumericUtils.doubleToSortableLong(value);
                doc.add(new SortedNumericDocValuesField("field", valueAsLong));
                expected.add(value);
            }
            iw.addDocument(doc);
        }
    }, stats -> {
        assertEquals(expected.count, stats.getCount(), 0);
        assertEquals(expected.sum, stats.getSum(), TOLERANCE);
        assertEquals(expected.min, stats.getMin(), 0);
        assertEquals(expected.max, stats.getMax(), 0);
        assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE);
        assertEquals(expected.sumOfSqrs, stats.getSumOfSquares(), TOLERANCE);
        assertEquals(expected.stdDev(), stats.getStdDeviation(), TOLERANCE);
        assertEquals(expected.variance(), stats.getVariance(), TOLERANCE);
        assertEquals(expected.stdDevBound(ExtendedStats.Bounds.LOWER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER), TOLERANCE);
        assertEquals(expected.stdDevBound(ExtendedStats.Bounds.UPPER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER), TOLERANCE);
    });
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Document(org.apache.lucene.document.Document)

Example 59 with SortedNumericDocValuesField

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

the class ExtendedStatsAggregatorTests method testRandomLongs.

public void testRandomLongs() throws IOException {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    ft.setName("field");
    final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator();
    testCase(ft, iw -> {
        int numDocs = randomIntBetween(10, 50);
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            int numValues = randomIntBetween(1, 5);
            for (int j = 0; j < numValues; j++) {
                long value = randomIntBetween(-100, 100);
                doc.add(new SortedNumericDocValuesField("field", value));
                expected.add(value);
            }
            iw.addDocument(doc);
        }
    }, stats -> {
        assertEquals(expected.count, stats.getCount(), 0);
        assertEquals(expected.sum, stats.getSum(), TOLERANCE);
        assertEquals(expected.min, stats.getMin(), 0);
        assertEquals(expected.max, stats.getMax(), 0);
        assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE);
        assertEquals(expected.sumOfSqrs, stats.getSumOfSquares(), TOLERANCE);
        assertEquals(expected.stdDev(), stats.getStdDeviation(), TOLERANCE);
        assertEquals(expected.variance(), stats.getVariance(), TOLERANCE);
        assertEquals(expected.stdDevBound(ExtendedStats.Bounds.LOWER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER), TOLERANCE);
        assertEquals(expected.stdDevBound(ExtendedStats.Bounds.UPPER, stats.getSigma()), stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER), TOLERANCE);
    });
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Document(org.apache.lucene.document.Document)

Example 60 with SortedNumericDocValuesField

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

the class MaxAggregatorTests method testNoMatchingField.

public void testNoMatchingField() throws IOException {
    testCase(new MatchAllDocsQuery(), iw -> {
        iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
        iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
    }, max -> {
        assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0);
    });
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

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