Search in sources :

Example 1 with NumberFieldType

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

the class ExpressionNumberSortScriptTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : NumberSortScript(org.opensearch.script.NumberSortScript) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SearchLookup(org.opensearch.search.lookup.SearchLookup) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 2 with NumberFieldType

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

the class NumberFieldTypeTests method testEqualsWithDifferentNumberTypes.

public void testEqualsWithDifferentNumberTypes() {
    NumberType type = randomFrom(NumberType.values());
    NumberFieldType fieldType = new NumberFieldType("foo", type);
    NumberType otherType = randomValueOtherThan(type, () -> randomFrom(NumberType.values()));
    NumberFieldType otherFieldType = new NumberFieldType("foo", otherType);
    assertNotEquals(fieldType, otherFieldType);
}
Also used : NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType)

Example 3 with NumberFieldType

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

the class NumberFieldTypeTests method doTestIndexSortRangeQueries.

public void doTestIndexSortRangeQueries(NumberType type, Supplier<Number> valueSupplier) throws IOException {
    // Create index settings with an index sort.
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put("index.sort.field", "field").build();
    IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
    IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
    // Create an index writer configured with the same index sort.
    NumberFieldType fieldType = new NumberFieldType("field", type);
    IndexNumericFieldData fielddata = (IndexNumericFieldData) fieldType.fielddataBuilder("index", () -> {
        throw new UnsupportedOperationException();
    }).build(null, null);
    SortField sortField = fielddata.sortField(null, MultiValueMode.MIN, null, randomBoolean());
    IndexWriterConfig writerConfig = new IndexWriterConfig();
    writerConfig.setIndexSort(new Sort(sortField));
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, writerConfig);
    final int numDocs = TestUtil.nextInt(random(), 100, 500);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(type.createFields("field", valueSupplier.get(), true, true, false));
    }
    // Ensure that the optimized index sort query gives the same results as a points query.
    DirectoryReader reader = DirectoryReader.open(w);
    IndexSearcher searcher = newSearcher(reader);
    QueryShardContext context = new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> 0L, null, null, () -> true, null);
    final int iters = 10;
    for (int iter = 0; iter < iters; ++iter) {
        Query query = type.rangeQuery("field", random().nextBoolean() ? null : valueSupplier.get(), random().nextBoolean() ? null : valueSupplier.get(), randomBoolean(), randomBoolean(), true, context);
        assertThat(query, instanceOf(IndexSortSortedNumericDocValuesRangeQuery.class));
        Query fallbackQuery = ((IndexSortSortedNumericDocValuesRangeQuery) query).getFallbackQuery();
        assertThat(fallbackQuery, instanceOf(IndexOrDocValuesQuery.class));
        IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) fallbackQuery;
        assertEquals(searcher.count(query), searcher.count(indexOrDvQuery.getIndexQuery()));
    }
    reader.close();
    w.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IndexSettings(org.opensearch.index.IndexSettings) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortField(org.apache.lucene.search.SortField) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) IndexWriter(org.apache.lucene.index.IndexWriter) Sort(org.apache.lucene.search.Sort) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) IndexSortSortedNumericDocValuesRangeQuery(org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Example 4 with NumberFieldType

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

the class ExpressionTermsSetQueryTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SearchLookup(org.opensearch.search.lookup.SearchLookup) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) TermsSetQueryScript(org.opensearch.script.TermsSetQueryScript) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 5 with NumberFieldType

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

the class FieldSortBuilder method extractNumericMinAndMax.

private static MinAndMax<?> extractNumericMinAndMax(IndexReader reader, SortField sortField, MappedFieldType fieldType, FieldSortBuilder sortBuilder) throws IOException {
    String fieldName = fieldType.name();
    if (PointValues.size(reader, fieldName) == 0) {
        return null;
    }
    if (fieldType instanceof NumberFieldType) {
        NumberFieldType numberFieldType = (NumberFieldType) fieldType;
        Number minPoint = numberFieldType.parsePoint(PointValues.getMinPackedValue(reader, fieldName));
        Number maxPoint = numberFieldType.parsePoint(PointValues.getMaxPackedValue(reader, fieldName));
        switch(IndexSortConfig.getSortFieldType(sortField)) {
            case LONG:
                return new MinAndMax<>(minPoint.longValue(), maxPoint.longValue());
            case INT:
                return new MinAndMax<>(minPoint.intValue(), maxPoint.intValue());
            case DOUBLE:
                return new MinAndMax<>(minPoint.doubleValue(), maxPoint.doubleValue());
            case FLOAT:
                return new MinAndMax<>(minPoint.floatValue(), maxPoint.floatValue());
            default:
                return null;
        }
    } else if (fieldType instanceof DateFieldType) {
        DateFieldType dateFieldType = (DateFieldType) fieldType;
        Function<byte[], Long> dateConverter = createDateConverter(sortBuilder, dateFieldType);
        Long min = dateConverter.apply(PointValues.getMinPackedValue(reader, fieldName));
        Long max = dateConverter.apply(PointValues.getMaxPackedValue(reader, fieldName));
        return new MinAndMax<>(min, max);
    }
    return null;
}
Also used : Function(java.util.function.Function) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType)

Aggregations

NumberFieldType (org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType)6 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)3 NumberType (org.opensearch.index.mapper.NumberFieldMapper.NumberType)3 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 Collections (java.util.Collections)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 Mockito.any (org.mockito.Mockito.any)2 Mockito.anyInt (org.mockito.Mockito.anyInt)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 LeafNumericFieldData (org.opensearch.index.fielddata.LeafNumericFieldData)2 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)2 MapperService (org.opensearch.index.mapper.MapperService)2 ScriptException (org.opensearch.script.ScriptException)2 SearchLookup (org.opensearch.search.lookup.SearchLookup)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2