Skip to content

Commit

Permalink
Add source fallback for keyword fields using operation (#88735)
Browse files Browse the repository at this point in the history
This change adds an operation parameter to FieldDataContext that allows us to specialize the field data that are returned from fielddataBuilder in MappedFieldType. Keyword, integer, and geo point field types now support source fallback where we build a doc values wrapper using source if doc values doesn't exist for this field under the operation SCRIPT. This allows us to have source fallback in scripting for the scripting fields API.
  • Loading branch information
jdconrad authored Jul 28, 2022
1 parent 0ce4a1c commit 5e0701f
Show file tree
Hide file tree
Showing 76 changed files with 1,159 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -83,10 +84,11 @@ public class ScriptScoreBenchmark {
Map.entry("n", new NumberFieldType("n", NumberType.LONG, false, false, true, true, null, Map.of(), null, false, null))
);
private final IndexFieldDataCache fieldDataCache = new IndexFieldDataCache.None();
private final Map<String, Set<String>> sourcePaths = Map.of("n", Set.of("n"));
private final CircuitBreakerService breakerService = new NoneCircuitBreakerService();
private final SearchLookup lookup = new SearchLookup(
fieldTypes::get,
(mft, lookup) -> mft.fielddataBuilder(FieldDataContext.noRuntimeFields("benchmark")).build(fieldDataCache, breakerService)
(mft, lookup, fdo) -> mft.fielddataBuilder(FieldDataContext.noRuntimeFields("benchmark")).build(fieldDataCache, breakerService)
);

@Param({ "expression", "metal", "painless_cast", "painless_def" })
Expand Down Expand Up @@ -152,7 +154,7 @@ private Query scriptScoreQuery(ScoreScript.Factory factory) {
private ScoreScript.Factory bareMetalScript() {
return (params, lookup) -> {
MappedFieldType type = fieldTypes.get("n");
IndexNumericFieldData ifd = (IndexNumericFieldData) lookup.getForField(type);
IndexNumericFieldData ifd = (IndexNumericFieldData) lookup.getForField(type, MappedFieldType.FielddataOperation.SEARCH);
return new ScoreScript.LeafFactory() {
@Override
public ScoreScript newInstance(DocReader docReader) throws IOException {
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog/88735.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88735
summary: Add source fallback for keyword fields using operation
area: Mapping
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private static DoubleValuesSource getDocValueSource(String variable, SearchLooku
throw new ParseException("Field [" + fieldname + "] does not exist in mappings", 5);
}

IndexFieldData<?> fieldData = lookup.getForField(fieldType);
IndexFieldData<?> fieldData = lookup.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
final DoubleValuesSource valueSource;
if (fieldType instanceof GeoPointFieldType) {
// geo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setUp() throws Exception {
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup) -> fieldData);
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup, fdt) -> fieldData);
}

private FieldScript.LeafFactory compile(String expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setUp() throws Exception {
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup) -> fieldData);
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup, fdt) -> fieldData);
}

private NumberSortScript.LeafFactory compile(String expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setUp() throws Exception {
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup) -> fieldData);
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, _lookup, fdt) -> fieldData);
}

private TermsSetQueryScript.LeafFactory compile(String expression) {
Expand Down
Loading

0 comments on commit 5e0701f

Please sign in to comment.