Search in sources :

Example 16 with RangeType

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

the class RangeHistogramAggregatorTests method testOffsetGtInterval.

public void testOffsetGtInterval() 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);
        }
        // I'd like to randomize the offset here, like I did in the test for the numeric side, but there's no way I can think of to
        // construct the intervals such that they wouldn't "slosh" between buckets.
        final double offset = 20;
        final double interval = 5;
        final double expectedOffset = offset % interval;
        HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(interval).offset(offset);
        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 + expectedOffset, histogram.getBuckets().get(0).getKey());
            assertEquals(1, histogram.getBuckets().get(0).getDocCount());
            assertEquals(0d + expectedOffset, histogram.getBuckets().get(1).getKey());
            assertEquals(3, histogram.getBuckets().get(1).getDocCount());
            assertEquals(5d + expectedOffset, histogram.getBuckets().get(2).getKey());
            assertEquals(2, histogram.getBuckets().get(2).getDocCount());
            assertEquals(10d + expectedOffset, histogram.getBuckets().get(3).getKey());
            assertEquals(1, histogram.getBuckets().get(3).getDocCount());
            assertEquals(15d + expectedOffset, histogram.getBuckets().get(4).getKey());
            assertEquals(0, histogram.getBuckets().get(4).getDocCount());
            assertEquals(20d + expectedOffset, histogram.getBuckets().get(5).getKey());
            assertEquals(1, histogram.getBuckets().get(5).getDocCount());
            assertEquals(25d + expectedOffset, 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 17 with RangeType

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

the class MissingAggregatorTests method testMatchSparseRangeField.

public void testMatchSparseRangeField() throws IOException {
    final RangeType rangeType = RangeType.DOUBLE;
    final MappedFieldType aggFieldType = new RangeFieldMapper.RangeFieldType("agg_field", rangeType);
    final MappedFieldType anotherFieldType = new RangeFieldMapper.RangeFieldType("another_field", rangeType);
    final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
    final BytesRef encodedRange = rangeType.encodeRanges(singleton(range));
    final BinaryDocValuesField encodedRangeField = new BinaryDocValuesField(aggFieldType.name(), encodedRange);
    final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name").field(aggFieldType.name());
    final int numDocs = randomIntBetween(100, 200);
    int docsMissingAggField = 0;
    final List<Set<IndexableField>> docs = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        if (randomBoolean()) {
            docs.add(singleton(encodedRangeField));
        } else {
            docs.add(singleton(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong())));
            docsMissingAggField++;
        }
    }
    final int finalDocsMissingAggField = docsMissingAggField;
    testCase(newMatchAllQuery(), builder, writer -> writer.addDocuments(docs), internalMissing -> {
        assertEquals(finalDocsMissingAggField, internalMissing.getDocCount());
        assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
    }, Arrays.asList(aggFieldType, anotherFieldType));
}
Also used : RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Set(java.util.Set) ArrayList(java.util.ArrayList) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) BytesRef(org.apache.lucene.util.BytesRef)

Example 18 with RangeType

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

the class SignificantTermsAggregatorTests method testRangeField.

/**
 * Uses the significant terms aggregation on a range field
 */
public void testRangeField() throws IOException {
    RangeType rangeType = RangeType.DOUBLE;
    final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
    final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true);
    final String fieldName = "rangeField";
    MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType);
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    indexWriterConfig.setMaxBufferedDocs(100);
    // flush on open to have a single segment
    indexWriterConfig.setRAMBufferSizeMB(100);
    try (Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
        for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { new RangeFieldMapper.Range(rangeType, 1L, 5L, true, true), new RangeFieldMapper.Range(rangeType, -3L, 4L, true, true), new RangeFieldMapper.Range(rangeType, 4L, 13L, true, true), new RangeFieldMapper.Range(rangeType, 42L, 49L, true, true) }) {
            Document doc = new Document();
            BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
            doc.add(new BinaryDocValuesField("field", encodedRange));
            w.addDocument(doc);
        }
        // Attempt aggregation on range field
        SignificantTermsAggregationBuilder sigAgg = new SignificantTermsAggregationBuilder("sig_text").field(fieldName);
        sigAgg.executionHint(randomExecutionHint());
        try (IndexReader reader = DirectoryReader.open(w)) {
            IndexSearcher indexSearcher = newIndexSearcher(reader);
            expectThrows(IllegalArgumentException.class, () -> createAggregator(sigAgg, indexSearcher, fieldType));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) BytesRef(org.apache.lucene.util.BytesRef) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Example 19 with RangeType

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

the class RareTermsAggregatorTests method testRangeField.

public void testRangeField() throws Exception {
    RangeType rangeType = RangeType.DOUBLE;
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            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 40, 45
            new RangeFieldMapper.Range(rangeType, 42.5, 49.3, true, true) }) {
                Document doc = new Document();
                BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
                doc.add(new BinaryDocValuesField("field", encodedRange));
                indexWriter.addDocument(doc);
            }
            MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType("field", rangeType);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                RareTermsAggregationBuilder aggregationBuilder = new RareTermsAggregationBuilder("_name").field("field");
                expectThrows(IllegalArgumentException.class, () -> createAggregator(aggregationBuilder, indexSearcher, fieldType));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) 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 20 with RangeType

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

the class ValueCountAggregatorTests method testRangeFieldValues.

public void testRangeFieldValues() throws IOException {
    RangeType rangeType = RangeType.DOUBLE;
    final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
    final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true);
    final String fieldName = "rangeField";
    MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType);
    final ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name").field(fieldName);
    Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2);
    multiRecord.add(range1);
    multiRecord.add(range2);
    testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
        iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
        iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
        iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2)))));
        iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(multiRecord))));
    }, count -> {
        assertEquals(4.0, count.getValue(), 0);
        assertTrue(AggregationInspectionHelper.hasValue(count));
    }, fieldType);
}
Also used : RangeType(org.opensearch.index.mapper.RangeType) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) 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