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));
}
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testIpRangesUnsupported.
public void testIpRangesUnsupported() throws Exception {
RangeType rangeType = RangeType.IP;
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(new RangeFieldMapper.Range(rangeType, InetAddresses.forString("10.0.0.1"), InetAddresses.forString("10.0.0.10"), true, true)));
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
Exception e = expectThrows(IllegalArgumentException.class, () -> searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)));
assertThat(e.getMessage(), equalTo("Expected numeric range type but found non-numeric range [ip_range]"));
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testDoubles.
public void testDoubles() throws Exception {
RangeType rangeType = RangeType.DOUBLE;
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket 0 5
new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true), // bucket -5, 0
new RangeFieldMapper.Range(rangeType, -3.1, 4.2, true, true), // bucket 0, 5, 10
new RangeFieldMapper.Range(rangeType, 4.2, 13.3, true, true), // bucket 20, 25
new RangeFieldMapper.Range(rangeType, 22.5, 29.3, true, true) }) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
}
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
assertEquals(7, histogram.getBuckets().size());
assertEquals(-5d, histogram.getBuckets().get(0).getKey());
assertEquals(1, histogram.getBuckets().get(0).getDocCount());
assertEquals(0d, histogram.getBuckets().get(1).getKey());
assertEquals(3, histogram.getBuckets().get(1).getDocCount());
assertEquals(5d, histogram.getBuckets().get(2).getKey());
assertEquals(2, histogram.getBuckets().get(2).getDocCount());
assertEquals(10d, histogram.getBuckets().get(3).getKey());
assertEquals(1, histogram.getBuckets().get(3).getDocCount());
assertEquals(15d, histogram.getBuckets().get(4).getKey());
assertEquals(0, histogram.getBuckets().get(4).getDocCount());
assertEquals(20d, histogram.getBuckets().get(5).getKey());
assertEquals(1, histogram.getBuckets().get(5).getDocCount());
assertEquals(25d, histogram.getBuckets().get(6).getKey());
assertEquals(1, histogram.getBuckets().get(6).getDocCount());
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testOffset.
public void testOffset() throws Exception {
RangeType rangeType = RangeType.DOUBLE;
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket -1, 4
new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true), // bucket -6 -1 4
new RangeFieldMapper.Range(rangeType, -3.1, 4.2, true, true), // bucket 4, 9
new RangeFieldMapper.Range(rangeType, 4.2, 13.3, true, true), // bucket 19, 24, 29
new RangeFieldMapper.Range(rangeType, 22.5, 29.3, true, true) }) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
}
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5).offset(4);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
assertEquals(8, histogram.getBuckets().size());
assertEquals(-6d, histogram.getBuckets().get(0).getKey());
assertEquals(1, histogram.getBuckets().get(0).getDocCount());
assertEquals(-1d, histogram.getBuckets().get(1).getKey());
assertEquals(2, histogram.getBuckets().get(1).getDocCount());
assertEquals(4d, histogram.getBuckets().get(2).getKey());
assertEquals(3, histogram.getBuckets().get(2).getDocCount());
assertEquals(9d, histogram.getBuckets().get(3).getKey());
assertEquals(1, histogram.getBuckets().get(3).getDocCount());
assertEquals(14d, histogram.getBuckets().get(4).getKey());
assertEquals(0, histogram.getBuckets().get(4).getDocCount());
assertEquals(19d, histogram.getBuckets().get(5).getKey());
assertEquals(1, histogram.getBuckets().get(5).getDocCount());
assertEquals(24d, histogram.getBuckets().get(6).getKey());
assertEquals(1, histogram.getBuckets().get(6).getDocCount());
assertEquals(29d, histogram.getBuckets().get(7).getKey());
assertEquals(1, histogram.getBuckets().get(7).getDocCount());
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testMultipleRangesLotsOfOverlap.
public void testMultipleRangesLotsOfOverlap() throws Exception {
RangeType rangeType = RangeType.LONG;
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
Document doc = new Document();
Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(4);
// bucket 0 5
multiRecord.add(new RangeFieldMapper.Range(rangeType, 1L, 2L, true, true));
// bucket -5, 0
multiRecord.add(new RangeFieldMapper.Range(rangeType, 1L, 4L, true, true));
// bucket 0, 5, 10
multiRecord.add(new RangeFieldMapper.Range(rangeType, 1L, 13L, true, true));
// bucket 40, 45
multiRecord.add(new RangeFieldMapper.Range(rangeType, 1L, 5L, true, true));
BytesRef encodedRange = rangeType.encodeRanges(multiRecord);
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(5);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
assertEquals(3, histogram.getBuckets().size());
assertEquals(0d, histogram.getBuckets().get(0).getKey());
assertEquals(1, histogram.getBuckets().get(0).getDocCount());
assertEquals(5d, histogram.getBuckets().get(1).getKey());
assertEquals(1, histogram.getBuckets().get(1).getDocCount());
assertEquals(10d, histogram.getBuckets().get(2).getKey());
assertEquals(1, histogram.getBuckets().get(2).getDocCount());
}
}
}
Aggregations