Search in sources :

Example 11 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.tests.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 12 with RangeType

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]"));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeType(org.opensearch.index.mapper.RangeType) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) IOException(java.io.IOException) Directory(org.apache.lucene.store.Directory)

Example 13 with RangeType

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());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 14 with RangeType

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());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 15 with RangeType

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());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

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