Skip to content

Commit

Permalink
Add a script field source abstraction (#84737)
Browse files Browse the repository at this point in the history
This change adds a ScriptFieldFactory class with a toScriptField and a DocValuesScriptFieldFactory 
class with a toScriptDocValues method. These classes are intended to facilitate the separation of the 
supplier of values to a script field and the field itself. The two new classes will provide a way for the 
old-style doc values to be accessed directly using a supplier instead of piggybacking off the new Field 
types which makes it easier to have source values for only the Field types moving forward.

Note this change is mostly mechanical where the Fields themselves are the DocValuesFieldFactory's 
for now. This way we can make each field have its own PR to create a supplier for that field type 
making the general change far more manageable.
  • Loading branch information
jdconrad committed Mar 10, 2022
1 parent 22c4a10 commit 4a78b36
Show file tree
Hide file tree
Showing 81 changed files with 647 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import org.elasticsearch.index.mapper.TimeSeriesParams;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.script.field.DocValuesField;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ScaledFloatDocValuesField;
import org.elasticsearch.script.field.ToScriptField;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import org.elasticsearch.search.lookup.SearchLookup;
Expand Down Expand Up @@ -488,16 +488,16 @@ private static class ScaledFloatIndexFieldData extends IndexNumericFieldData {

private final IndexNumericFieldData scaledFieldData;
private final double scalingFactor;
private final ToScriptField<SortedNumericDoubleValues> toScriptField;
private final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;

ScaledFloatIndexFieldData(
IndexNumericFieldData scaledFieldData,
double scalingFactor,
ToScriptField<SortedNumericDoubleValues> toScriptField
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory
) {
this.scaledFieldData = scaledFieldData;
this.scalingFactor = scalingFactor;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -512,12 +512,12 @@ public ValuesSourceType getValuesSourceType() {

@Override
public LeafNumericFieldData load(LeafReaderContext context) {
return new ScaledFloatLeafFieldData(scaledFieldData.load(context), scalingFactor, toScriptField);
return new ScaledFloatLeafFieldData(scaledFieldData.load(context), scalingFactor, toScriptFieldFactory);
}

@Override
public LeafNumericFieldData loadDirect(LeafReaderContext context) throws Exception {
return new ScaledFloatLeafFieldData(scaledFieldData.loadDirect(context), scalingFactor, toScriptField);
return new ScaledFloatLeafFieldData(scaledFieldData.loadDirect(context), scalingFactor, toScriptFieldFactory);
}

@Override
Expand Down Expand Up @@ -546,21 +546,21 @@ private static class ScaledFloatLeafFieldData implements LeafNumericFieldData {

private final LeafNumericFieldData scaledFieldData;
private final double scalingFactorInverse;
private final ToScriptField<SortedNumericDoubleValues> toScriptField;
private final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;

ScaledFloatLeafFieldData(
LeafNumericFieldData scaledFieldData,
double scalingFactor,
ToScriptField<SortedNumericDoubleValues> toScriptField
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory
) {
this.scaledFieldData = scaledFieldData;
this.scalingFactorInverse = 1d / scalingFactor;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
public DocValuesField<?> getScriptField(String name) {
return toScriptField.getScriptField(getDoubleValues(), name);
public DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
return toScriptFieldFactory.getScriptFieldFactory(getDoubleValues(), name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.BooleanFieldScript;
import org.elasticsearch.script.field.DocValuesField;
import org.elasticsearch.script.field.ToScriptField;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

Expand All @@ -24,32 +24,36 @@ public final class BooleanScriptFieldData extends IndexNumericFieldData {
public static class Builder implements IndexFieldData.Builder {
private final String name;
private final BooleanFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

public Builder(String name, BooleanFieldScript.LeafFactory leafFactory, ToScriptField<SortedNumericDocValues> toScriptField) {
public Builder(
String name,
BooleanFieldScript.LeafFactory leafFactory,
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
this.name = name;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
public BooleanScriptFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
return new BooleanScriptFieldData(name, leafFactory, toScriptField);
return new BooleanScriptFieldData(name, leafFactory, toScriptFieldFactory);
}
}

private final String fieldName;
private final BooleanFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

private BooleanScriptFieldData(
String fieldName,
BooleanFieldScript.LeafFactory leafFactory,
ToScriptField<SortedNumericDocValues> toScriptField
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
this.fieldName = fieldName;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -73,7 +77,7 @@ public BooleanScriptLeafFieldData load(LeafReaderContext context) {

@Override
public BooleanScriptLeafFieldData loadDirect(LeafReaderContext context) {
return new BooleanScriptLeafFieldData(new BooleanScriptDocValues(leafFactory.newInstance(context)), toScriptField);
return new BooleanScriptLeafFieldData(new BooleanScriptDocValues(leafFactory.newInstance(context)), toScriptFieldFactory);
}

@Override
Expand All @@ -88,12 +92,15 @@ protected boolean sortRequiresCustomComparator() {

public static class BooleanScriptLeafFieldData extends LeafLongFieldData {
private final BooleanScriptDocValues booleanScriptDocValues;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

BooleanScriptLeafFieldData(BooleanScriptDocValues booleanScriptDocValues, ToScriptField<SortedNumericDocValues> toScriptField) {
BooleanScriptLeafFieldData(
BooleanScriptDocValues booleanScriptDocValues,
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
super(0);
this.booleanScriptDocValues = booleanScriptDocValues;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -105,8 +112,8 @@ public SortedNumericDocValues getLongValues() {
public void close() {}

@Override
public DocValuesField<?> getScriptField(String name) {
return toScriptField.getScriptField(getLongValues(), name);
public DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
return toScriptFieldFactory.getScriptFieldFactory(getLongValues(), name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.DateFieldScript;
import org.elasticsearch.script.field.DocValuesField;
import org.elasticsearch.script.field.ToScriptField;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

Expand All @@ -24,32 +24,36 @@ public final class DateScriptFieldData extends IndexNumericFieldData {
public static class Builder implements IndexFieldData.Builder {
private final String name;
private final DateFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

public Builder(String name, DateFieldScript.LeafFactory leafFactory, ToScriptField<SortedNumericDocValues> toScriptField) {
public Builder(
String name,
DateFieldScript.LeafFactory leafFactory,
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
this.name = name;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
public DateScriptFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
return new DateScriptFieldData(name, leafFactory, toScriptField);
return new DateScriptFieldData(name, leafFactory, toScriptFieldFactory);
}
}

private final String fieldName;
private final DateFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

private DateScriptFieldData(
String fieldName,
DateFieldScript.LeafFactory leafFactory,
ToScriptField<SortedNumericDocValues> toScriptField
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
this.fieldName = fieldName;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -73,7 +77,7 @@ public DateScriptLeafFieldData load(LeafReaderContext context) {

@Override
public DateScriptLeafFieldData loadDirect(LeafReaderContext context) {
return new DateScriptLeafFieldData(new LongScriptDocValues(leafFactory.newInstance(context)), toScriptField);
return new DateScriptLeafFieldData(new LongScriptDocValues(leafFactory.newInstance(context)), toScriptFieldFactory);
}

@Override
Expand All @@ -88,12 +92,15 @@ protected boolean sortRequiresCustomComparator() {

public static class DateScriptLeafFieldData extends LeafLongFieldData {
private final LongScriptDocValues longScriptDocValues;
protected final ToScriptField<SortedNumericDocValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory;

DateScriptLeafFieldData(LongScriptDocValues longScriptDocValues, ToScriptField<SortedNumericDocValues> toScriptField) {
DateScriptLeafFieldData(
LongScriptDocValues longScriptDocValues,
ToScriptFieldFactory<SortedNumericDocValues> toScriptFieldFactory
) {
super(0);
this.longScriptDocValues = longScriptDocValues;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -102,8 +109,8 @@ public SortedNumericDocValues getLongValues() {
}

@Override
public DocValuesField<?> getScriptField(String name) {
return toScriptField.getScriptField(getLongValues(), name);
public DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
return toScriptFieldFactory.getScriptFieldFactory(getLongValues(), name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.elasticsearch.index.fielddata.plain.LeafDoubleFieldData;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.DoubleFieldScript;
import org.elasticsearch.script.field.DocValuesField;
import org.elasticsearch.script.field.ToScriptField;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.ToScriptFieldFactory;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

Expand All @@ -23,32 +23,36 @@ public final class DoubleScriptFieldData extends IndexNumericFieldData {
public static class Builder implements IndexFieldData.Builder {
private final String name;
private final DoubleFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDoubleValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;

public Builder(String name, DoubleFieldScript.LeafFactory leafFactory, ToScriptField<SortedNumericDoubleValues> toScriptField) {
public Builder(
String name,
DoubleFieldScript.LeafFactory leafFactory,
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory
) {
this.name = name;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
public DoubleScriptFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
return new DoubleScriptFieldData(name, leafFactory, toScriptField);
return new DoubleScriptFieldData(name, leafFactory, toScriptFieldFactory);
}
}

private final String fieldName;
DoubleFieldScript.LeafFactory leafFactory;
protected final ToScriptField<SortedNumericDoubleValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;

private DoubleScriptFieldData(
String fieldName,
DoubleFieldScript.LeafFactory leafFactory,
ToScriptField<SortedNumericDoubleValues> toScriptField
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory
) {
this.fieldName = fieldName;
this.leafFactory = leafFactory;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -72,7 +76,7 @@ public DoubleScriptLeafFieldData load(LeafReaderContext context) {

@Override
public DoubleScriptLeafFieldData loadDirect(LeafReaderContext context) {
return new DoubleScriptLeafFieldData(new DoubleScriptDocValues(leafFactory.newInstance(context)), toScriptField);
return new DoubleScriptLeafFieldData(new DoubleScriptDocValues(leafFactory.newInstance(context)), toScriptFieldFactory);
}

@Override
Expand All @@ -87,12 +91,15 @@ protected boolean sortRequiresCustomComparator() {

public static class DoubleScriptLeafFieldData extends LeafDoubleFieldData {
private final DoubleScriptDocValues doubleScriptDocValues;
protected final ToScriptField<SortedNumericDoubleValues> toScriptField;
protected final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;

DoubleScriptLeafFieldData(DoubleScriptDocValues doubleScriptDocValues, ToScriptField<SortedNumericDoubleValues> toScriptField) {
DoubleScriptLeafFieldData(
DoubleScriptDocValues doubleScriptDocValues,
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory
) {
super(0);
this.doubleScriptDocValues = doubleScriptDocValues;
this.toScriptField = toScriptField;
this.toScriptFieldFactory = toScriptFieldFactory;
}

@Override
Expand All @@ -104,8 +111,8 @@ public SortedNumericDoubleValues getDoubleValues() {
public void close() {}

@Override
public DocValuesField<?> getScriptField(String name) {
return toScriptField.getScriptField(getDoubleValues(), name);
public DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
return toScriptFieldFactory.getScriptFieldFactory(getDoubleValues(), name);
}
}
}

0 comments on commit 4a78b36

Please sign in to comment.