Search in sources :

Example 21 with RangeType

use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.

the class RangeHistogramAggregator method getLeafCollector.

@Override
protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    final RangeType rangeType = valuesSource.rangeType();
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long owningBucketOrd) throws IOException {
            if (values.advanceExact(doc)) {
                // Is it possible for valuesCount to be > 1 here? Multiple ranges are encoded into the same BytesRef in the binary doc
                // values, so it isn't clear what we'd be iterating over.
                final int valuesCount = values.docValueCount();
                assert valuesCount == 1 : "Value count for ranges should always be 1";
                double previousKey = Double.NEGATIVE_INFINITY;
                for (int i = 0; i < valuesCount; i++) {
                    BytesRef encodedRanges = values.nextValue();
                    List<RangeFieldMapper.Range> ranges = rangeType.decodeRanges(encodedRanges);
                    double previousFrom = Double.NEGATIVE_INFINITY;
                    for (RangeFieldMapper.Range range : ranges) {
                        final Double from = rangeType.doubleValue(range.getFrom());
                        // The encoding should ensure that this assert is always true.
                        assert from >= previousFrom : "Start of range not >= previous start";
                        final Double to = rangeType.doubleValue(range.getTo());
                        final double effectiveFrom = (hardBounds != null && hardBounds.getMin() != null) ? Double.max(from, hardBounds.getMin()) : from;
                        final double effectiveTo = (hardBounds != null && hardBounds.getMax() != null) ? Double.min(to, hardBounds.getMax()) : to;
                        final double startKey = Math.floor((effectiveFrom - offset) / interval);
                        final double endKey = Math.floor((effectiveTo - offset) / interval);
                        for (double key = Math.max(startKey, previousKey); key <= endKey; key++) {
                            if (key == previousKey) {
                                continue;
                            }
                            // Bucket collection identical to NumericHistogramAggregator, could be refactored
                            long bucketOrd = bucketOrds.add(owningBucketOrd, Double.doubleToLongBits(key));
                            if (bucketOrd < 0) {
                                // already seen
                                bucketOrd = -1 - bucketOrd;
                                collectExistingBucket(sub, doc, bucketOrd);
                            } else {
                                collectBucket(sub, doc, bucketOrd);
                            }
                        }
                        if (endKey > previousKey) {
                            previousKey = endKey;
                        }
                    }
                }
            }
        }
    };
}
Also used : RangeType(org.opensearch.index.mapper.RangeType) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 22 with RangeType

use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.

the class DateRangeHistogramAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    RangeType rangeType = valuesSource.rangeType();
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long owningBucketOrd) throws IOException {
            if (values.advanceExact(doc)) {
                // Is it possible for valuesCount to be > 1 here? Multiple ranges are encoded into the same BytesRef in the binary doc
                // values, so it isn't clear what we'd be iterating over.
                int valuesCount = values.docValueCount();
                assert valuesCount == 1 : "Value count for ranges should always be 1";
                long previousKey = Long.MIN_VALUE;
                for (int i = 0; i < valuesCount; i++) {
                    BytesRef encodedRanges = values.nextValue();
                    List<RangeFieldMapper.Range> ranges = rangeType.decodeRanges(encodedRanges);
                    long previousFrom = Long.MIN_VALUE;
                    for (RangeFieldMapper.Range range : ranges) {
                        Long from = (Long) range.getFrom();
                        // The encoding should ensure that this assert is always true.
                        assert from >= previousFrom : "Start of range not >= previous start";
                        final Long to = (Long) range.getTo();
                        final long effectiveFrom = (hardBounds != null && hardBounds.getMin() != null) ? max(from, hardBounds.getMin()) : from;
                        final long effectiveTo = (hardBounds != null && hardBounds.getMax() != null) ? min(to, hardBounds.getMax()) : to;
                        final long startKey = preparedRounding.round(effectiveFrom);
                        final long endKey = preparedRounding.round(effectiveTo);
                        for (long key = max(startKey, previousKey); key <= endKey; key = preparedRounding.nextRoundingValue(key)) {
                            if (key == previousKey) {
                                continue;
                            }
                            // Bucket collection identical to NumericHistogramAggregator, could be refactored
                            long bucketOrd = bucketOrds.add(owningBucketOrd, key);
                            if (bucketOrd < 0) {
                                // already seen
                                bucketOrd = -1 - bucketOrd;
                                collectExistingBucket(sub, doc, bucketOrd);
                            } else {
                                collectBucket(sub, doc, bucketOrd);
                            }
                        }
                        if (endKey > previousKey) {
                            previousKey = endKey;
                        }
                    }
                }
            }
        }
    };
}
Also used : RangeType(org.opensearch.index.mapper.RangeType) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 23 with RangeType

use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.

the class BinaryDocValuesRangeQueryTests method testNoField.

public void testNoField() throws IOException {
    String fieldName = "long_field";
    RangeType rangeType = RangeType.LONG;
    // no field in index
    try (Directory dir = newDirectory()) {
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
            writer.addDocument(new Document());
            try (IndexReader reader = writer.getReader()) {
                IndexSearcher searcher = newSearcher(reader);
                Query query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -1L, 1L, true, true);
                assertEquals(0, searcher.count(query));
            }
        }
    }
    // no field in segment
    try (Directory dir = newDirectory()) {
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
            // intersects (within)
            Document document = new Document();
            BytesRef encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, 0L, 0L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            writer.commit();
            writer.addDocument(new Document());
            try (IndexReader reader = writer.getReader()) {
                IndexSearcher searcher = newSearcher(reader);
                Query query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -1L, 1L, true, true);
                assertEquals(1, searcher.count(query));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeType(org.opensearch.index.mapper.RangeType) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 24 with RangeType

use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.

the class BinaryDocValuesRangeQueryTests method testBasics.

public void testBasics() throws Exception {
    String fieldName = "long_field";
    RangeType rangeType = RangeType.LONG;
    try (Directory dir = newDirectory()) {
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
            // intersects (within)
            Document document = new Document();
            BytesRef encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, -10L, 9L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects (crosses)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, 10L, 20L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects (contains, crosses)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, -20L, 30L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects (within)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, -11L, 1L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects (crosses)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, 12L, 15L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // disjoint
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, -122L, -115L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects (crosses)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, Long.MIN_VALUE, -11L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // equal (within, contains, intersects)
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, -11L, 15L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // intersects, within
            document = new Document();
            encodedRange = rangeType.encodeRanges(singleton(new RangeFieldMapper.Range(rangeType, 5L, 10L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            // search
            try (IndexReader reader = writer.getReader()) {
                IndexSearcher searcher = newSearcher(reader);
                Query query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -11L, 15L, true, true);
                assertEquals(8, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, WITHIN, -11L, 15L, true, true);
                assertEquals(5, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, CONTAINS, -11L, 15L, true, true);
                assertEquals(2, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, CROSSES, -11L, 15L, true, true);
                assertEquals(3, searcher.count(query));
                // test includeFrom = false and includeTo = false
                query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -11L, 15L, false, false);
                assertEquals(7, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, WITHIN, -11L, 15L, false, false);
                assertEquals(2, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, CONTAINS, -11L, 15L, false, false);
                assertEquals(2, searcher.count(query));
                query = rangeType.dvRangeQuery(fieldName, CROSSES, -11L, 15L, false, false);
                assertEquals(5, searcher.count(query));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

RangeType (org.opensearch.index.mapper.RangeType)24 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)21 BytesRef (org.apache.lucene.util.BytesRef)20 RangeFieldMapper (org.opensearch.index.mapper.RangeFieldMapper)19 Document (org.apache.lucene.document.Document)18 IndexReader (org.apache.lucene.index.IndexReader)17 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 Directory (org.apache.lucene.store.Directory)17 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)11 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)7 HashSet (java.util.HashSet)4 Query (org.apache.lucene.search.Query)4 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)2 LeafBucketCollectorBase (org.opensearch.search.aggregations.LeafBucketCollectorBase)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1