Skip to content

Commit 1e61059

Browse files
authored
Move runtime fields to server (#69223)
This commit makes a start on moving runtime fields to server. The runtime field types are now built-in. The dynamic fields builder extension (needed to make dynamic:runtime work) is removed: further simplifications are needed but are left for a follow-up. It is still possible to plug in custom runtime field types through the same extension point that the xpack plugin used to use. The runtime fields script contexts are now also moved to server, and the corresponding whitelists are part of painless-lang with the exception of grok and dissect which will require additional work. The runtime fields xpack plugin still exists to hold the grok and dissect extensions needed, which depend on grok and dissect lib and we will address as a follow-up. Also, the qa tests and yaml tests are yet to be moved. Despite the need for additional work, runtime fields are fully functional and users should not be impacted by this change.
1 parent ae94701 commit 1e61059

File tree

161 files changed

+996
-1087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+996
-1087
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
import org.elasticsearch.repositories.RepositoriesService;
3838
import org.elasticsearch.rest.RestController;
3939
import org.elasticsearch.rest.RestHandler;
40+
import org.elasticsearch.runtimefields.mapper.BooleanFieldScript;
41+
import org.elasticsearch.runtimefields.mapper.DateFieldScript;
42+
import org.elasticsearch.runtimefields.mapper.DoubleFieldScript;
43+
import org.elasticsearch.runtimefields.mapper.GeoPointFieldScript;
44+
import org.elasticsearch.runtimefields.mapper.IpFieldScript;
45+
import org.elasticsearch.runtimefields.mapper.LongFieldScript;
46+
import org.elasticsearch.runtimefields.mapper.StringFieldScript;
4047
import org.elasticsearch.script.IngestScript;
4148
import org.elasticsearch.script.ScoreScript;
4249
import org.elasticsearch.script.ScriptContext;
@@ -88,6 +95,15 @@ public final class PainlessPlugin extends Plugin implements ScriptPlugin, Extens
8895
ingest.add(ingestWhitelist);
8996
map.put(IngestScript.CONTEXT, ingest);
9097

98+
// Functions available to runtime fields
99+
map.put(BooleanFieldScript.CONTEXT, getRuntimeFieldWhitelist("boolean"));
100+
map.put(DateFieldScript.CONTEXT, getRuntimeFieldWhitelist("date"));
101+
map.put(DoubleFieldScript.CONTEXT, getRuntimeFieldWhitelist("double"));
102+
map.put(LongFieldScript.CONTEXT, getRuntimeFieldWhitelist("long"));
103+
map.put(StringFieldScript.CONTEXT, getRuntimeFieldWhitelist("string"));
104+
map.put(GeoPointFieldScript.CONTEXT, getRuntimeFieldWhitelist("geopoint"));
105+
map.put(IpFieldScript.CONTEXT, getRuntimeFieldWhitelist("ip"));
106+
91107
// Execute context gets everything
92108
List<Whitelist> test = new ArrayList<>(Whitelist.BASE_WHITELISTS);
93109
test.add(movFnWhitelist);
@@ -99,6 +115,14 @@ public final class PainlessPlugin extends Plugin implements ScriptPlugin, Extens
99115
whitelists = map;
100116
}
101117

118+
private static List<Whitelist> getRuntimeFieldWhitelist(String fieldType) {
119+
List<Whitelist> scriptField = new ArrayList<>(Whitelist.BASE_WHITELISTS);
120+
Whitelist whitelist = WhitelistLoader.loadFromResourceFiles(Whitelist.class,
121+
"org.elasticsearch.runtimefields." + fieldType + ".txt");
122+
scriptField.add(whitelist);
123+
return scriptField;
124+
}
125+
102126
private final SetOnce<PainlessScriptEngine> painlessScriptEngine = new SetOnce<>();
103127

104128
@Override
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for boolean-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.BooleanFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.BooleanFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.BooleanFieldScript, boolean) bound_to org.elasticsearch.runtimefields.mapper.BooleanFieldScript$Emit
20+
}
21+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for date-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.DateFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.DateFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.DateFieldScript, long) bound_to org.elasticsearch.runtimefields.mapper.DateFieldScript$Emit
20+
# Parse a value from the source to millis since epoch
21+
long parse(org.elasticsearch.runtimefields.mapper.DateFieldScript, def) bound_to org.elasticsearch.runtimefields.mapper.DateFieldScript$Parse
22+
}
23+
24+
25+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for double-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.DoubleFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.DoubleFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.DoubleFieldScript, double) bound_to org.elasticsearch.runtimefields.mapper.DoubleFieldScript$Emit
20+
}
21+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for ip-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.GeoPointFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.GeoPointFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.GeoPointFieldScript, double, double) bound_to org.elasticsearch.runtimefields.mapper.GeoPointFieldScript$Emit
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for ip-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.IpFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.IpFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.IpFieldScript, String) bound_to org.elasticsearch.runtimefields.mapper.IpFieldScript$Emit
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for long-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.LongFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.LongFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.LongFieldScript, long) bound_to org.elasticsearch.runtimefields.mapper.LongFieldScript$Emit
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the Elastic License
4+
# 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
# in compliance with, at your election, the Elastic License 2.0 or the Server
6+
# Side Public License, v 1.
7+
#
8+
9+
# The whitelist for string-valued runtime fields
10+
11+
# These two whitelists are required for painless to find the classes
12+
class org.elasticsearch.runtimefields.mapper.StringFieldScript @no_import {
13+
}
14+
class org.elasticsearch.runtimefields.mapper.StringFieldScript$Factory @no_import {
15+
}
16+
17+
static_import {
18+
# The `emit` callback to collect values for the field
19+
void emit(org.elasticsearch.runtimefields.mapper.StringFieldScript, String) bound_to org.elasticsearch.runtimefields.mapper.StringFieldScript$Emit
20+
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
package org.elasticsearch.index.mapper;
1010

11-
import java.io.IOException;
12-
import java.util.ArrayList;
13-
import java.util.Collections;
14-
import java.util.Comparator;
15-
import java.util.Iterator;
16-
import java.util.List;
17-
import java.util.function.Function;
18-
1911
import org.apache.lucene.document.Field;
2012
import org.apache.lucene.index.IndexableField;
2113
import org.apache.lucene.search.Query;
@@ -31,6 +23,14 @@
3123
import org.elasticsearch.common.xcontent.XContentType;
3224
import org.elasticsearch.index.query.SearchExecutionContext;
3325

26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.Comparator;
30+
import java.util.Iterator;
31+
import java.util.List;
32+
import java.util.function.Function;
33+
3434
/** A parser for documents, given mappings from a DocumentMapper */
3535
final class DocumentParser {
3636

@@ -39,11 +39,10 @@ final class DocumentParser {
3939
private final DynamicRuntimeFieldsBuilder dynamicRuntimeFieldsBuilder;
4040

4141
DocumentParser(NamedXContentRegistry xContentRegistry,
42-
Function<DateFormatter, Mapper.TypeParser.ParserContext> dateParserContext,
43-
DynamicRuntimeFieldsBuilder dynamicRuntimeFieldsBuilder) {
42+
Function<DateFormatter, Mapper.TypeParser.ParserContext> dateParserContext) {
4443
this.xContentRegistry = xContentRegistry;
4544
this.dateParserContext = dateParserContext;
46-
this.dynamicRuntimeFieldsBuilder = dynamicRuntimeFieldsBuilder;
45+
this.dynamicRuntimeFieldsBuilder = org.elasticsearch.runtimefields.mapper.DynamicRuntimeFieldsBuilder.INSTANCE;
4746
}
4847

4948
ParsedDocument parseDocument(SourceToParse source,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.elasticsearch.index.mapper;
1010

1111
import org.elasticsearch.common.time.DateFormatter;
12-
import org.elasticsearch.plugins.MapperPlugin;
1312

1413
import static org.elasticsearch.index.mapper.ObjectMapper.Dynamic;
1514

@@ -18,7 +17,6 @@
1817
* Plugins that provide runtime field implementations can also plug in their implementation of this interface
1918
* to define how leaf fields of each supported type can be dynamically created in dynamic runtime mode.
2019
*
21-
* @see MapperPlugin#getDynamicRuntimeFieldsBuilder()
2220
* @see Dynamic
2321
*/
2422
public interface DynamicRuntimeFieldsBuilder {

0 commit comments

Comments
 (0)