Search in sources :

Example 1 with RangeType

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

the class IndicesModule method getMappers.

public static Map<String, Mapper.TypeParser> getMappers(List<MapperPlugin> mapperPlugins) {
    Map<String, Mapper.TypeParser> mappers = new LinkedHashMap<>();
    // builtin mappers
    for (NumberFieldMapper.NumberType type : NumberFieldMapper.NumberType.values()) {
        mappers.put(type.typeName(), type.parser());
    }
    for (RangeType type : RangeType.values()) {
        mappers.put(type.typeName(), type.parser());
    }
    mappers.put(BooleanFieldMapper.CONTENT_TYPE, BooleanFieldMapper.PARSER);
    mappers.put(BinaryFieldMapper.CONTENT_TYPE, BinaryFieldMapper.PARSER);
    DateFieldMapper.Resolution milliseconds = DateFieldMapper.Resolution.MILLISECONDS;
    mappers.put(milliseconds.type(), DateFieldMapper.MILLIS_PARSER);
    DateFieldMapper.Resolution nanoseconds = DateFieldMapper.Resolution.NANOSECONDS;
    mappers.put(nanoseconds.type(), DateFieldMapper.NANOS_PARSER);
    mappers.put(IpFieldMapper.CONTENT_TYPE, IpFieldMapper.PARSER);
    mappers.put(TextFieldMapper.CONTENT_TYPE, TextFieldMapper.PARSER);
    mappers.put(KeywordFieldMapper.CONTENT_TYPE, KeywordFieldMapper.PARSER);
    mappers.put(ObjectMapper.CONTENT_TYPE, new ObjectMapper.TypeParser());
    mappers.put(ObjectMapper.NESTED_CONTENT_TYPE, new ObjectMapper.TypeParser());
    mappers.put(CompletionFieldMapper.CONTENT_TYPE, CompletionFieldMapper.PARSER);
    mappers.put(FieldAliasMapper.CONTENT_TYPE, new FieldAliasMapper.TypeParser());
    mappers.put(GeoPointFieldMapper.CONTENT_TYPE, new GeoPointFieldMapper.TypeParser());
    for (MapperPlugin mapperPlugin : mapperPlugins) {
        for (Map.Entry<String, Mapper.TypeParser> entry : mapperPlugin.getMappers().entrySet()) {
            if (mappers.put(entry.getKey(), entry.getValue()) != null) {
                throw new IllegalArgumentException("Mapper [" + entry.getKey() + "] is already registered");
            }
        }
    }
    return Collections.unmodifiableMap(mappers);
}
Also used : DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) GeoPointFieldMapper(org.opensearch.index.mapper.GeoPointFieldMapper) MapperPlugin(org.opensearch.plugins.MapperPlugin) FieldAliasMapper(org.opensearch.index.mapper.FieldAliasMapper) LinkedHashMap(java.util.LinkedHashMap) RangeType(org.opensearch.index.mapper.RangeType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 2 with RangeType

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

the class RangeHistogramAggregatorTests method testMultipleRanges.

public void testMultipleRanges() 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, 5L, true, true));
        // bucket -5, 0
        multiRecord.add(new RangeFieldMapper.Range(rangeType, -3L, 4L, true, true));
        // bucket 0, 5, 10
        multiRecord.add(new RangeFieldMapper.Range(rangeType, 4L, 13L, true, true));
        // bucket 20, 25, 30
        multiRecord.add(new RangeFieldMapper.Range(rangeType, 22L, 29L, 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(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(1, histogram.getBuckets().get(1).getDocCount());
            assertEquals(5d, histogram.getBuckets().get(2).getKey());
            assertEquals(1, 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.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Example 3 with RangeType

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

the class RangeHistogramAggregatorTests method testLongsIrrationalInterval.

public void testLongsIrrationalInterval() throws Exception {
    RangeType rangeType = RangeType.LONG;
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket 0 5
        new RangeFieldMapper.Range(rangeType, 1L, 5L, true, true), // bucket -5, 0
        new RangeFieldMapper.Range(rangeType, -3L, 4L, true, true), // bucket 0, 5, 10
        new RangeFieldMapper.Range(rangeType, 4L, 13L, 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(Math.PI);
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
            assertEquals(6, histogram.getBuckets().size());
            assertEquals(-1 * Math.PI, histogram.getBuckets().get(0).getKey());
            assertEquals(1, histogram.getBuckets().get(0).getDocCount());
            assertEquals(0 * Math.PI, histogram.getBuckets().get(1).getKey());
            assertEquals(2, histogram.getBuckets().get(1).getDocCount());
            assertEquals(1 * Math.PI, histogram.getBuckets().get(2).getKey());
            assertEquals(3, histogram.getBuckets().get(2).getDocCount());
            assertEquals(2 * Math.PI, histogram.getBuckets().get(3).getKey());
            assertEquals(1, histogram.getBuckets().get(3).getDocCount());
            assertEquals(3 * Math.PI, histogram.getBuckets().get(4).getKey());
            assertEquals(1, histogram.getBuckets().get(4).getDocCount());
            assertEquals(4 * Math.PI, histogram.getBuckets().get(5).getKey());
            assertEquals(1, histogram.getBuckets().get(5).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.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 4 with RangeType

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

the class RangeHistogramAggregatorTests method testMinDocCount.

public void testMinDocCount() throws Exception {
    RangeType rangeType = RangeType.LONG;
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket -15
        new RangeFieldMapper.Range(rangeType, -14L, -11L, true, true), // bucket 0, 5
        new RangeFieldMapper.Range(rangeType, 0L, 9L, true, true), // bucket 5, 10
        new RangeFieldMapper.Range(rangeType, 6L, 12L, true, true), // bucket 10
        new RangeFieldMapper.Range(rangeType, 13L, 14L, 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).minDocCount(2);
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
            assertEquals(2, histogram.getBuckets().size());
            assertEquals(5d, histogram.getBuckets().get(0).getKey());
            assertEquals(2, histogram.getBuckets().get(0).getDocCount());
            assertEquals(10d, histogram.getBuckets().get(1).getKey());
            assertEquals(2, histogram.getBuckets().get(1).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.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 5 with RangeType

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

the class RangeHistogramAggregatorTests method testLongs.

public void testLongs() throws Exception {
    RangeType rangeType = RangeType.LONG;
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket 0 5
        new RangeFieldMapper.Range(rangeType, 1L, 5L, true, true), // bucket -5, 0
        new RangeFieldMapper.Range(rangeType, -3L, 4L, true, true), // bucket 0, 5, 10
        new RangeFieldMapper.Range(rangeType, 4L, 13L, true, true), // bucket 20, 25
        new RangeFieldMapper.Range(rangeType, 22L, 29L, 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.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

RangeType (org.opensearch.index.mapper.RangeType)22 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)19 BytesRef (org.apache.lucene.util.BytesRef)18 RangeFieldMapper (org.opensearch.index.mapper.RangeFieldMapper)18 Document (org.apache.lucene.document.Document)16 IndexReader (org.apache.lucene.index.IndexReader)15 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)15 IndexSearcher (org.apache.lucene.search.IndexSearcher)15 Directory (org.apache.lucene.store.Directory)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)11 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)7 HashSet (java.util.HashSet)4 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 Query (org.apache.lucene.search.Query)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 LinkedHashMap (java.util.LinkedHashMap)1