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