Search in sources :

Example 6 with RangeType

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

the class TermsAggregatorTests method testRangeField.

public void testRangeField() throws Exception {
    try (Directory directory = newDirectory()) {
        double start = randomDouble();
        double end = randomDoubleBetween(Math.nextUp(start), Double.MAX_VALUE, false);
        RangeType rangeType = RangeType.DOUBLE;
        final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, start, end, true, true);
        final String fieldName = "field";
        final BinaryDocValuesField field = new BinaryDocValuesField(fieldName, rangeType.encodeRanges(Collections.singleton(range)));
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(field);
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType);
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field(fieldName);
                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) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 7 with RangeType

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

the class CardinalityAggregatorTests 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 CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("_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))));
    }, card -> {
        assertEquals(3.0, card.getValue(), 0);
        assertTrue(AggregationInspectionHelper.hasValue(card));
    }, 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)

Example 8 with RangeType

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

the class AggregatorTestCase method writeTestDoc.

/**
 * Helper method to write a single document with a single value specific to the requested fieldType.
 *
 * Throws an exception if it encounters an unknown field type, to prevent new ones from sneaking in without
 * being tested.
 */
private void writeTestDoc(MappedFieldType fieldType, String fieldName, RandomIndexWriter iw) throws IOException {
    String typeName = fieldType.typeName();
    ValuesSourceType vst = fieldToVST(fieldType);
    Document doc = new Document();
    String json;
    if (vst.equals(CoreValuesSourceType.NUMERIC)) {
        long v;
        if (typeName.equals(NumberFieldMapper.NumberType.DOUBLE.typeName())) {
            double d = Math.abs(randomDouble());
            v = NumericUtils.doubleToSortableLong(d);
            json = "{ \"" + fieldName + "\" : \"" + d + "\" }";
        } else if (typeName.equals(NumberFieldMapper.NumberType.FLOAT.typeName())) {
            float f = Math.abs(randomFloat());
            v = NumericUtils.floatToSortableInt(f);
            json = "{ \"" + fieldName + "\" : \"" + f + "\" }";
        } else if (typeName.equals(NumberFieldMapper.NumberType.HALF_FLOAT.typeName())) {
            // Generate a random float that respects the limits of half float
            float f = Math.abs((randomFloat() * 2 - 1) * 65504);
            v = HalfFloatPoint.halfFloatToSortableShort(f);
            json = "{ \"" + fieldName + "\" : \"" + f + "\" }";
        } else {
            // smallest numeric is a byte so we select the smallest
            v = Math.abs(randomByte());
            json = "{ \"" + fieldName + "\" : \"" + v + "\" }";
        }
        doc.add(new SortedNumericDocValuesField(fieldName, v));
    } else if (vst.equals(CoreValuesSourceType.BYTES)) {
        if (typeName.equals(BinaryFieldMapper.CONTENT_TYPE)) {
            doc.add(new BinaryFieldMapper.CustomBinaryDocValuesField(fieldName, new BytesRef("a").bytes));
            json = "{ \"" + fieldName + "\" : \"a\" }";
        } else {
            doc.add(new SortedSetDocValuesField(fieldName, new BytesRef("a")));
            json = "{ \"" + fieldName + "\" : \"a\" }";
        }
    } else if (vst.equals(CoreValuesSourceType.DATE)) {
        // positive integer because date_nanos gets unhappy with large longs
        long v;
        v = Math.abs(randomInt());
        doc.add(new SortedNumericDocValuesField(fieldName, v));
        json = "{ \"" + fieldName + "\" : \"" + v + "\" }";
    } else if (vst.equals(CoreValuesSourceType.BOOLEAN)) {
        long v;
        v = randomBoolean() ? 0 : 1;
        doc.add(new SortedNumericDocValuesField(fieldName, v));
        json = "{ \"" + fieldName + "\" : \"" + (v == 0 ? "false" : "true") + "\" }";
    } else if (vst.equals(CoreValuesSourceType.IP)) {
        InetAddress ip = randomIp(randomBoolean());
        json = "{ \"" + fieldName + "\" : \"" + NetworkAddress.format(ip) + "\" }";
        doc.add(new SortedSetDocValuesField(fieldName, new BytesRef(InetAddressPoint.encode(ip))));
    } else if (vst.equals(CoreValuesSourceType.RANGE)) {
        Object start;
        Object end;
        RangeType rangeType;
        if (typeName.equals(RangeType.DOUBLE.typeName())) {
            start = randomDouble();
            end = RangeType.DOUBLE.nextUp(start);
            rangeType = RangeType.DOUBLE;
        } else if (typeName.equals(RangeType.FLOAT.typeName())) {
            start = randomFloat();
            end = RangeType.FLOAT.nextUp(start);
            rangeType = RangeType.DOUBLE;
        } else if (typeName.equals(RangeType.IP.typeName())) {
            boolean v4 = randomBoolean();
            start = randomIp(v4);
            end = RangeType.IP.nextUp(start);
            rangeType = RangeType.IP;
        } else if (typeName.equals(RangeType.LONG.typeName())) {
            start = randomLong();
            end = RangeType.LONG.nextUp(start);
            rangeType = RangeType.LONG;
        } else if (typeName.equals(RangeType.INTEGER.typeName())) {
            start = randomInt();
            end = RangeType.INTEGER.nextUp(start);
            rangeType = RangeType.INTEGER;
        } else if (typeName.equals(RangeType.DATE.typeName())) {
            start = randomNonNegativeLong();
            end = RangeType.DATE.nextUp(start);
            rangeType = RangeType.DATE;
        } else {
            throw new IllegalStateException("Unknown type of range [" + typeName + "]");
        }
        final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, start, end, true, true);
        doc.add(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(Collections.singleton(range))));
        json = "{ \"" + fieldName + "\" : { \n" + "        \"gte\" : \"" + start + "\",\n" + "        \"lte\" : \"" + end + "\"\n" + "      }}";
    } else if (vst.equals(CoreValuesSourceType.GEOPOINT)) {
        double lat = randomDouble();
        double lon = randomDouble();
        doc.add(new LatLonDocValuesField(fieldName, lat, lon));
        json = "{ \"" + fieldName + "\" : \"[" + lon + "," + lat + "]\" }";
    } else {
        throw new IllegalStateException("Unknown field type [" + typeName + "]");
    }
    doc.add(new StoredField("_source", new BytesRef(json)));
    iw.addDocument(doc);
}
Also used : RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) Mockito.anyString(org.mockito.Mockito.anyString) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) RangeType(org.opensearch.index.mapper.RangeType) StoredField(org.apache.lucene.document.StoredField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) InetAddress(java.net.InetAddress) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 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 10 with RangeType

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

the class BinaryDocValuesRangeQueryTests method testNoField.

public void testNoField() throws IOException {
    String fieldName = "long_field";
    RangeType rangeType = RangeType.LONG;
    // no field in index
    try (Directory dir = newDirectory()) {
        try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
            writer.addDocument(new Document());
            try (IndexReader reader = writer.getReader()) {
                IndexSearcher searcher = newSearcher(reader);
                Query query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -1L, 1L, true, true);
                assertEquals(0, searcher.count(query));
            }
        }
    }
    // no field in segment
    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, 0L, 0L, true, true)));
            document.add(new BinaryDocValuesField(fieldName, encodedRange));
            writer.addDocument(document);
            writer.commit();
            writer.addDocument(new Document());
            try (IndexReader reader = writer.getReader()) {
                IndexSearcher searcher = newSearcher(reader);
                Query query = rangeType.dvRangeQuery(fieldName, INTERSECTS, -1L, 1L, true, true);
                assertEquals(1, searcher.count(query));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RangeType(org.opensearch.index.mapper.RangeType) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) 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