Skip to content

Commit 7bf3780

Browse files
authored
Simplify runtime fields factory function (elastic#71027)
We currently use a TriFunction that takes the field name, the script params and the search lookup and returns a LeafFactory. Name and script params never change though and are available when the function is created, so the tri function can become a function search lookup -> leaf factory which is much easier to reason about.
1 parent 3e5e367 commit 7bf3780

File tree

8 files changed

+29
-29
lines changed

8 files changed

+29
-29
lines changed

server/src/main/java/org/elasticsearch/index/mapper/AbstractScriptFieldType.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
1515
import org.apache.lucene.search.spans.SpanQuery;
1616
import org.elasticsearch.ElasticsearchException;
17-
import org.elasticsearch.common.TriFunction;
1817
import org.elasticsearch.common.geo.ShapeRelation;
1918
import org.elasticsearch.common.time.DateMathParser;
2019
import org.elasticsearch.common.unit.Fuzziness;
@@ -42,12 +41,12 @@
4241
*/
4342
abstract class AbstractScriptFieldType<LeafFactory> extends MappedFieldType implements RuntimeField {
4443
protected final Script script;
45-
private final TriFunction<String, Map<String, Object>, SearchLookup, LeafFactory> factory;
44+
private final Function<SearchLookup, LeafFactory> factory;
4645
private final ToXContent toXContent;
4746

4847
AbstractScriptFieldType(
4948
String name,
50-
TriFunction<String, Map<String, Object>, SearchLookup, LeafFactory> factory,
49+
Function<SearchLookup, LeafFactory> factory,
5150
Script script,
5251
Map<String, String> meta,
5352
ToXContent toXContent
@@ -188,7 +187,7 @@ public final ValueFetcher valueFetcher(SearchExecutionContext context, String fo
188187
* Create a script leaf factory.
189188
*/
190189
protected final LeafFactory leafFactory(SearchLookup searchLookup) {
191-
return factory.apply(name(), script.getParams(), searchLookup);
190+
return factory.apply(searchLookup);
192191
}
193192

194193
/**

server/src/main/java/org/elasticsearch/index/mapper/BooleanScriptFieldType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import org.elasticsearch.common.lucene.search.Queries;
1717
import org.elasticsearch.common.time.DateMathParser;
1818
import org.elasticsearch.common.xcontent.ToXContent;
19-
import org.elasticsearch.index.query.SearchExecutionContext;
2019
import org.elasticsearch.index.fielddata.BooleanScriptFieldData;
20+
import org.elasticsearch.index.query.SearchExecutionContext;
2121
import org.elasticsearch.script.BooleanFieldScript;
22-
import org.elasticsearch.search.runtime.BooleanScriptFieldExistsQuery;
23-
import org.elasticsearch.search.runtime.BooleanScriptFieldTermQuery;
2422
import org.elasticsearch.script.Script;
2523
import org.elasticsearch.search.DocValueFormat;
2624
import org.elasticsearch.search.lookup.SearchLookup;
25+
import org.elasticsearch.search.runtime.BooleanScriptFieldExistsQuery;
26+
import org.elasticsearch.search.runtime.BooleanScriptFieldTermQuery;
2727

2828
import java.time.ZoneId;
2929
import java.util.Collection;
@@ -76,7 +76,7 @@ public BooleanScriptFieldType(String name) {
7676
Map<String, String> meta,
7777
ToXContent toXContent
7878
) {
79-
super(name, scriptFactory::newFactory, script, meta, toXContent);
79+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
8080
}
8181

8282
@Override

server/src/main/java/org/elasticsearch/index/mapper/DateScriptFieldType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public DateScriptFieldType(String name, DateFormatter dateTimeFormatter) {
127127
Map<String, String> meta,
128128
ToXContent toXContent
129129
) {
130-
super(name, (n, params, ctx) -> scriptFactory.newFactory(n, params, ctx, dateTimeFormatter), script, meta, toXContent);
130+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup, dateTimeFormatter),
131+
script, meta, toXContent);
131132
this.dateTimeFormatter = dateTimeFormatter;
132133
}
133134

server/src/main/java/org/elasticsearch/index/mapper/DoubleScriptFieldType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import org.elasticsearch.common.lucene.search.Queries;
1515
import org.elasticsearch.common.time.DateMathParser;
1616
import org.elasticsearch.common.xcontent.ToXContent;
17+
import org.elasticsearch.index.fielddata.DoubleScriptFieldData;
1718
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
1819
import org.elasticsearch.index.query.SearchExecutionContext;
19-
import org.elasticsearch.index.fielddata.DoubleScriptFieldData;
2020
import org.elasticsearch.script.DoubleFieldScript;
21+
import org.elasticsearch.script.Script;
22+
import org.elasticsearch.search.DocValueFormat;
23+
import org.elasticsearch.search.lookup.SearchLookup;
2124
import org.elasticsearch.search.runtime.DoubleScriptFieldExistsQuery;
2225
import org.elasticsearch.search.runtime.DoubleScriptFieldRangeQuery;
2326
import org.elasticsearch.search.runtime.DoubleScriptFieldTermQuery;
2427
import org.elasticsearch.search.runtime.DoubleScriptFieldTermsQuery;
25-
import org.elasticsearch.script.Script;
26-
import org.elasticsearch.search.DocValueFormat;
27-
import org.elasticsearch.search.lookup.SearchLookup;
2828

2929
import java.time.ZoneId;
3030
import java.util.Collection;
@@ -77,7 +77,7 @@ public DoubleScriptFieldType(String name) {
7777
Map<String, String> meta,
7878
ToXContent toXContent
7979
) {
80-
super(name, scriptFactory::newFactory, script, meta, toXContent);
80+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
8181
}
8282

8383
@Override

server/src/main/java/org/elasticsearch/index/mapper/GeoPointScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ RuntimeField newRuntimeField(GeoPointFieldScript.Factory scriptFactory) {
9292
Map<String, String> meta,
9393
ToXContent toXContent
9494
) {
95-
super(name, scriptFactory::newFactory, script, meta, toXContent);
95+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
9696
}
9797

9898
@Override

server/src/main/java/org/elasticsearch/index/mapper/IpScriptFieldType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
import org.elasticsearch.common.util.BigArrays;
2121
import org.elasticsearch.common.util.BytesRefHash;
2222
import org.elasticsearch.common.xcontent.ToXContent;
23-
import org.elasticsearch.index.query.SearchExecutionContext;
2423
import org.elasticsearch.index.fielddata.IpScriptFieldData;
24+
import org.elasticsearch.index.query.SearchExecutionContext;
2525
import org.elasticsearch.script.IpFieldScript;
26+
import org.elasticsearch.script.Script;
27+
import org.elasticsearch.search.DocValueFormat;
28+
import org.elasticsearch.search.lookup.SearchLookup;
2629
import org.elasticsearch.search.runtime.IpScriptFieldExistsQuery;
2730
import org.elasticsearch.search.runtime.IpScriptFieldRangeQuery;
2831
import org.elasticsearch.search.runtime.IpScriptFieldTermQuery;
2932
import org.elasticsearch.search.runtime.IpScriptFieldTermsQuery;
30-
import org.elasticsearch.script.Script;
31-
import org.elasticsearch.search.DocValueFormat;
32-
import org.elasticsearch.search.lookup.SearchLookup;
3333

3434
import java.net.InetAddress;
3535
import java.time.ZoneId;
@@ -79,7 +79,7 @@ RuntimeField newRuntimeField(IpFieldScript.Factory scriptFactory) {
7979
Map<String, String> meta,
8080
ToXContent toXContent
8181
) {
82-
super(name, scriptFactory::newFactory, script, meta, toXContent);
82+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
8383
}
8484

8585
@Override

server/src/main/java/org/elasticsearch/index/mapper/KeywordScriptFieldType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import org.elasticsearch.common.time.DateMathParser;
1616
import org.elasticsearch.common.unit.Fuzziness;
1717
import org.elasticsearch.common.xcontent.ToXContent;
18-
import org.elasticsearch.index.query.SearchExecutionContext;
1918
import org.elasticsearch.index.fielddata.StringScriptFieldData;
19+
import org.elasticsearch.index.query.SearchExecutionContext;
20+
import org.elasticsearch.script.Script;
2021
import org.elasticsearch.script.StringFieldScript;
22+
import org.elasticsearch.search.lookup.SearchLookup;
2123
import org.elasticsearch.search.runtime.StringScriptFieldExistsQuery;
2224
import org.elasticsearch.search.runtime.StringScriptFieldFuzzyQuery;
2325
import org.elasticsearch.search.runtime.StringScriptFieldPrefixQuery;
@@ -26,8 +28,6 @@
2628
import org.elasticsearch.search.runtime.StringScriptFieldTermQuery;
2729
import org.elasticsearch.search.runtime.StringScriptFieldTermsQuery;
2830
import org.elasticsearch.search.runtime.StringScriptFieldWildcardQuery;
29-
import org.elasticsearch.script.Script;
30-
import org.elasticsearch.search.lookup.SearchLookup;
3131

3232
import java.time.ZoneId;
3333
import java.util.Collection;
@@ -78,7 +78,7 @@ public KeywordScriptFieldType(String name) {
7878
Map<String, String> meta,
7979
ToXContent toXContent
8080
) {
81-
super(name, scriptFactory::newFactory, script, meta, toXContent);
81+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
8282
}
8383

8484
@Override

server/src/main/java/org/elasticsearch/index/mapper/LongScriptFieldType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import org.elasticsearch.common.lucene.search.Queries;
1515
import org.elasticsearch.common.time.DateMathParser;
1616
import org.elasticsearch.common.xcontent.ToXContent;
17+
import org.elasticsearch.index.fielddata.LongScriptFieldData;
1718
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
1819
import org.elasticsearch.index.query.SearchExecutionContext;
19-
import org.elasticsearch.index.fielddata.LongScriptFieldData;
2020
import org.elasticsearch.script.LongFieldScript;
21+
import org.elasticsearch.script.Script;
22+
import org.elasticsearch.search.DocValueFormat;
23+
import org.elasticsearch.search.lookup.SearchLookup;
2124
import org.elasticsearch.search.runtime.LongScriptFieldExistsQuery;
2225
import org.elasticsearch.search.runtime.LongScriptFieldRangeQuery;
2326
import org.elasticsearch.search.runtime.LongScriptFieldTermQuery;
2427
import org.elasticsearch.search.runtime.LongScriptFieldTermsQuery;
25-
import org.elasticsearch.script.Script;
26-
import org.elasticsearch.search.DocValueFormat;
27-
import org.elasticsearch.search.lookup.SearchLookup;
2828

2929
import java.time.ZoneId;
3030
import java.util.Collection;
@@ -73,7 +73,7 @@ public LongScriptFieldType(String name) {
7373
Map<String, String> meta,
7474
ToXContent toXContent
7575
) {
76-
super(name, scriptFactory::newFactory, script, meta, toXContent);
76+
super(name, searchLookup -> scriptFactory.newFactory(name, script.getParams(), searchLookup), script, meta, toXContent);
7777
}
7878

7979
@Override

0 commit comments

Comments
 (0)