use of org.opensearch.index.fielddata.IndexNumericFieldData 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);
}
use of org.opensearch.index.fielddata.IndexNumericFieldData in project OpenSearch by opensearch-project.
the class ExpressionFieldScriptTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.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);
}
use of org.opensearch.index.fielddata.IndexNumericFieldData in project OpenSearch by opensearch-project.
the class ScaledFloatFieldTypeTests method testFieldData.
public void testFieldData() throws IOException {
double scalingFactor = 0.1 + randomDouble() * 100;
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("scaled_float1", 10));
doc.add(new SortedNumericDocValuesField("scaled_float2", 5));
doc.add(new SortedNumericDocValuesField("scaled_float2", 12));
w.addDocument(doc);
try (DirectoryReader reader = DirectoryReader.open(w)) {
// single-valued
ScaledFloatFieldMapper.ScaledFloatFieldType f1 = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float1", scalingFactor);
IndexNumericFieldData fielddata = (IndexNumericFieldData) f1.fielddataBuilder("index", () -> {
throw new UnsupportedOperationException();
}).build(null, null);
assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE);
LeafNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0));
SortedNumericDoubleValues values = leafFieldData.getDoubleValues();
assertTrue(values.advanceExact(0));
assertEquals(1, values.docValueCount());
assertEquals(10 / f1.getScalingFactor(), values.nextValue(), 10e-5);
// multi-valued
ScaledFloatFieldMapper.ScaledFloatFieldType f2 = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float2", scalingFactor);
fielddata = (IndexNumericFieldData) f2.fielddataBuilder("index", () -> {
throw new UnsupportedOperationException();
}).build(null, null);
leafFieldData = fielddata.load(reader.leaves().get(0));
values = leafFieldData.getDoubleValues();
assertTrue(values.advanceExact(0));
assertEquals(2, values.docValueCount());
assertEquals(5 / f2.getScalingFactor(), values.nextValue(), 10e-5);
assertEquals(12 / f2.getScalingFactor(), values.nextValue(), 10e-5);
}
IOUtils.close(w, dir);
}
use of org.opensearch.index.fielddata.IndexNumericFieldData 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();
}
use of org.opensearch.index.fielddata.IndexNumericFieldData in project OpenSearch by opensearch-project.
the class TermsSetQueryBuilder method createValuesSource.
private LongValuesSource createValuesSource(QueryShardContext context) {
LongValuesSource longValuesSource;
if (minimumShouldMatchField != null) {
MappedFieldType msmFieldType = context.fieldMapper(minimumShouldMatchField);
if (msmFieldType == null) {
throw new QueryShardException(context, "failed to find minimum_should_match field [" + minimumShouldMatchField + "]");
}
IndexNumericFieldData fieldData = context.getForField(msmFieldType);
longValuesSource = new FieldValuesSource(fieldData);
} else if (minimumShouldMatchScript != null) {
TermsSetQueryScript.Factory factory = context.compile(minimumShouldMatchScript, TermsSetQueryScript.CONTEXT);
Map<String, Object> params = new HashMap<>();
params.putAll(minimumShouldMatchScript.getParams());
params.put("num_terms", values.size());
longValuesSource = new ScriptLongValueSource(minimumShouldMatchScript, factory.newFactory(params, context.lookup()));
} else {
throw new IllegalStateException("No minimum should match has been specified");
}
return longValuesSource;
}
Aggregations