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;
}
}
}
}
}
};
}
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;
}
}
}
}
}
};
}
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));
}
}
}
}
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));
}
}
}
}
Aggregations