use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.
the class LeafDocLookupTests method createFieldData.
private IndexFieldData<?> createFieldData(ScriptDocValues scriptDocValues) {
LeafFieldData leafFieldData = mock(LeafFieldData.class);
doReturn(scriptDocValues).when(leafFieldData).getScriptValues();
IndexFieldData<?> fieldData = mock(IndexFieldData.class);
when(fieldData.getFieldName()).thenReturn("field");
doReturn(leafFieldData).when(fieldData).load(any());
return fieldData;
}
use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.
the class RandomScoreFunction method getLeafScoreFunction.
@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
final SortedBinaryDocValues values;
if (fieldData != null) {
LeafFieldData leafData = fieldData.load(ctx);
values = leafData.getBytesValues();
if (values == null)
throw new NullPointerException("failed to get fielddata");
} else {
values = null;
}
return new LeafScoreFunction() {
@Override
public double score(int docId, float subQueryScore) throws IOException {
int hash;
if (values == null) {
hash = BitMixer.mix(ctx.docBase + docId, saltedSeed);
} else if (values.advanceExact(docId)) {
hash = StringHelper.murmurhash3_x86_32(values.nextValue(), saltedSeed);
} else {
// field has no value
hash = saltedSeed;
}
// only use the lower 24 bits to construct a float from 0.0-1.0
return (hash & 0x00FFFFFF) / (float) (1 << 24);
}
@Override
public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
String field = fieldData == null ? null : fieldData.getFieldName();
return Explanation.match((float) score(docId, subQueryScore.getValue().floatValue()), "random score function (seed: " + originalSeed + ", field: " + field + Functions.nameOrEmptyArg(functionName) + ")");
}
};
}
use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.
the class QueryShardContextTests method mockFieldType.
private static MappedFieldType mockFieldType(String fieldName, BiFunction<LeafSearchLookup, Integer, String> runtimeDocValues) {
MappedFieldType fieldType = mock(MappedFieldType.class);
when(fieldType.name()).thenReturn(fieldName);
when(fieldType.fielddataBuilder(any(), any())).thenAnswer(builderInv -> {
@SuppressWarnings("unchecked") Supplier<SearchLookup> searchLookup = ((Supplier<SearchLookup>) builderInv.getArguments()[1]);
IndexFieldData<?> indexFieldData = mock(IndexFieldData.class);
when(indexFieldData.load(any())).thenAnswer(loadArgs -> {
LeafReaderContext leafReaderContext = (LeafReaderContext) loadArgs.getArguments()[0];
LeafFieldData leafFieldData = mock(LeafFieldData.class);
when(leafFieldData.getScriptValues()).thenAnswer(scriptValuesArgs -> new ScriptDocValues<String>() {
String value;
@Override
public int size() {
return 1;
}
@Override
public String get(int index) {
assert index == 0;
return value;
}
@Override
public void setNextDocId(int docId) {
assert docId >= 0;
LeafSearchLookup leafLookup = searchLookup.get().getLeafSearchLookup(leafReaderContext);
leafLookup.setDocument(docId);
value = runtimeDocValues.apply(leafLookup, docId);
}
});
return leafFieldData;
});
IndexFieldData.Builder builder = mock(IndexFieldData.Builder.class);
when(builder.build(any(), any())).thenAnswer(buildInv -> indexFieldData);
return builder;
});
return fieldType;
}
Aggregations