use of org.opensearch.index.fielddata.LeafNumericFieldData 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.LeafNumericFieldData 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.LeafNumericFieldData 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.LeafNumericFieldData in project OpenSearch by opensearch-project.
the class DateFieldTypeTests method testDateNanoDocValues.
public void testDateNanoDocValues() throws IOException {
// Create an index with some docValues
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
Document doc = new Document();
NumericDocValuesField docValuesField = new NumericDocValuesField("my_date", 1444608000000L);
doc.add(docValuesField);
w.addDocument(doc);
docValuesField.setLongValue(1459641600000L);
w.addDocument(doc);
// Create the doc values reader
SortedNumericIndexFieldData fieldData = new SortedNumericIndexFieldData("my_date", IndexNumericFieldData.NumericType.DATE_NANOSECONDS);
// Read index and check the doc values
DirectoryReader reader = DirectoryReader.open(w);
assertTrue(reader.leaves().size() > 0);
LeafNumericFieldData a = fieldData.load(reader.leaves().get(0).reader().getContext());
SortedNumericDocValues docValues = a.getLongValues();
assertEquals(0, docValues.nextDoc());
assertEquals(1, docValues.nextDoc());
assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.nextDoc());
reader.close();
w.close();
dir.close();
}
use of org.opensearch.index.fielddata.LeafNumericFieldData in project OpenSearch by opensearch-project.
the class EmptyMemberValueSource method getValues.
@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
LeafNumericFieldData leafData = (LeafNumericFieldData) fieldData.load(leaf);
final SortedNumericDoubleValues values = leafData.getDoubleValues();
return DoubleValues.withDefault(new DoubleValues() {
@Override
public double doubleValue() {
return 0;
}
@Override
public boolean advanceExact(int doc) throws IOException {
return values.advanceExact(doc);
}
}, 1);
}
Aggregations