Skip to content

Commit 4c68dfe

Browse files
Handle missing values in painless (#32207)
Throw an exception for doc['field'].value if this document is missing a value for the field. After deprecation changes have been backported to 6.x, make this a default behaviour in 7.0 Closes #29286
1 parent 9ae6905 commit 4c68dfe

File tree

6 files changed

+14
-266
lines changed

6 files changed

+14
-266
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,6 @@ class BuildPlugin implements Plugin<Project> {
750750
systemProperty 'tests.task', path
751751
systemProperty 'tests.security.manager', 'true'
752752
systemProperty 'jna.nosys', 'true'
753-
systemProperty 'es.scripting.exception_for_missing_value', 'true'
754753
// TODO: remove setting logging level via system property
755754
systemProperty 'tests.logger.level', 'WARN'
756755
for (Map.Entry<String, String> property : System.properties.entrySet()) {

docs/painless/painless-getting-started.asciidoc

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,8 @@ GET hockey/_search
123123
[float]
124124
===== Missing values
125125

126-
If you request the value from a field `field` that isn’t in
127-
the document, `doc['field'].value` for this document returns:
128-
129-
- `0` if a `field` has a numeric datatype (long, double etc.)
130-
- `false` is a `field` has a boolean datatype
131-
- epoch date if a `field` has a date datatype
132-
- `null` if a `field` has a string datatype
133-
- `null` if a `field` has a geo datatype
134-
- `""` if a `field` has a binary datatype
135-
136-
IMPORTANT: Starting in 7.0, `doc['field'].value` throws an exception if
137-
the field is missing in a document. To enable this behavior now,
138-
set a {ref}/jvm-options.html[`jvm.option`]
139-
`-Des.scripting.exception_for_missing_value=true` on a node. If you do not enable
140-
this behavior, a deprecation warning is logged on start up.
126+
`doc['field'].value` throws an exception if
127+
the field is missing in a document.
141128

142129
To check if a document is missing a value, you can call
143130
`doc['field'].size() == 0`.

server/build.gradle

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,6 @@ if (isEclipse) {
156156
compileJava.options.compilerArgs << "-Xlint:-cast,-deprecation,-rawtypes,-try,-unchecked"
157157
compileTestJava.options.compilerArgs << "-Xlint:-cast,-deprecation,-rawtypes,-try,-unchecked"
158158

159-
// TODO: remove ScriptDocValuesMissingV6BehaviourTests in 7.0
160-
additionalTest('testScriptDocValuesMissingV6Behaviour'){
161-
include '**/ScriptDocValuesMissingV6BehaviourTests.class'
162-
systemProperty 'es.scripting.exception_for_missing_value', 'false'
163-
}
164-
test {
165-
// these are tested explicitly in separate test tasks
166-
exclude '**/*ScriptDocValuesMissingV6BehaviourTests.class'
167-
}
168-
169159
forbiddenPatterns {
170160
exclude '**/*.json'
171161
exclude '**/*.jmx'

server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.elasticsearch.common.geo.GeoUtils;
3030
import org.elasticsearch.common.logging.DeprecationLogger;
3131
import org.elasticsearch.common.logging.ESLoggerFactory;
32-
import org.elasticsearch.script.ScriptModule;
3332
import org.joda.time.DateTime;
3433
import org.joda.time.DateTimeZone;
3534
import org.joda.time.MutableDateTime;
@@ -126,11 +125,8 @@ protected void resize(int newSize) {
126125

127126
public long getValue() {
128127
if (count == 0) {
129-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
130-
throw new IllegalStateException("A document doesn't have a value for a field! " +
131-
"Use doc[<field>].size()==0 to check if a document is missing a field!");
132-
}
133-
return 0L;
128+
throw new IllegalStateException("A document doesn't have a value for a field! " +
129+
"Use doc[<field>].size()==0 to check if a document is missing a field!");
134130
}
135131
return values[0];
136132
}
@@ -172,11 +168,8 @@ public Dates(SortedNumericDocValues in) {
172168
*/
173169
public ReadableDateTime getValue() {
174170
if (count == 0) {
175-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
176-
throw new IllegalStateException("A document doesn't have a value for a field! " +
177-
"Use doc[<field>].size()==0 to check if a document is missing a field!");
178-
}
179-
return EPOCH;
171+
throw new IllegalStateException("A document doesn't have a value for a field! " +
172+
"Use doc[<field>].size()==0 to check if a document is missing a field!");
180173
}
181174
return get(0);
182175
}
@@ -277,11 +270,8 @@ public SortedNumericDoubleValues getInternalValues() {
277270

278271
public double getValue() {
279272
if (count == 0) {
280-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
281-
throw new IllegalStateException("A document doesn't have a value for a field! " +
282-
"Use doc[<field>].size()==0 to check if a document is missing a field!");
283-
}
284-
return 0d;
273+
throw new IllegalStateException("A document doesn't have a value for a field! " +
274+
"Use doc[<field>].size()==0 to check if a document is missing a field!");
285275
}
286276
return values[0];
287277
}
@@ -337,11 +327,8 @@ protected void resize(int newSize) {
337327

338328
public GeoPoint getValue() {
339329
if (count == 0) {
340-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
341-
throw new IllegalStateException("A document doesn't have a value for a field! " +
330+
throw new IllegalStateException("A document doesn't have a value for a field! " +
342331
"Use doc[<field>].size()==0 to check if a document is missing a field!");
343-
}
344-
return null;
345332
}
346333
return values[0];
347334
}
@@ -454,11 +441,8 @@ protected void resize(int newSize) {
454441

455442
public boolean getValue() {
456443
if (count == 0) {
457-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
458-
throw new IllegalStateException("A document doesn't have a value for a field! " +
459-
"Use doc[<field>].size()==0 to check if a document is missing a field!");
460-
}
461-
return false;
444+
throw new IllegalStateException("A document doesn't have a value for a field! " +
445+
"Use doc[<field>].size()==0 to check if a document is missing a field!");
462446
}
463447
return values[0];
464448
}
@@ -544,11 +528,8 @@ public String get(int index) {
544528

545529
public String getValue() {
546530
if (count == 0) {
547-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
548-
throw new IllegalStateException("A document doesn't have a value for a field! " +
531+
throw new IllegalStateException("A document doesn't have a value for a field! " +
549532
"Use doc[<field>].size()==0 to check if a document is missing a field!");
550-
}
551-
return null;
552533
}
553534
return get(0);
554535
}
@@ -572,11 +553,8 @@ public BytesRef get(int index) {
572553

573554
public BytesRef getValue() {
574555
if (count == 0) {
575-
if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
576-
throw new IllegalStateException("A document doesn't have a value for a field! " +
556+
throw new IllegalStateException("A document doesn't have a value for a field! " +
577557
"Use doc[<field>].size()==0 to check if a document is missing a field!");
578-
}
579-
return new BytesRef();
580558
}
581559
return get(0);
582560
}

server/src/main/java/org/elasticsearch/script/ScriptModule.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import org.elasticsearch.common.settings.Settings;
3232
import org.elasticsearch.plugins.ScriptPlugin;
3333
import org.elasticsearch.search.aggregations.pipeline.movfn.MovingFunctionScript;
34-
import org.elasticsearch.common.Booleans;
35-
import org.elasticsearch.common.logging.DeprecationLogger;
36-
import org.elasticsearch.common.logging.Loggers;
34+
3735

3836
/**
3937
* Manages building {@link ScriptService}.
@@ -64,11 +62,6 @@ public class ScriptModule {
6462
).collect(Collectors.toMap(c -> c.name, Function.identity()));
6563
}
6664

67-
public static final boolean EXCEPTION_FOR_MISSING_VALUE =
68-
Booleans.parseBoolean(System.getProperty("es.scripting.exception_for_missing_value", "false"));
69-
70-
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(ScriptModule.class));
71-
7265
private final ScriptService scriptService;
7366

7467
public ScriptModule(Settings settings, List<ScriptPlugin> scriptPlugins) {
@@ -92,10 +85,6 @@ public ScriptModule(Settings settings, List<ScriptPlugin> scriptPlugins) {
9285
}
9386
}
9487
}
95-
if (EXCEPTION_FOR_MISSING_VALUE == false)
96-
DEPRECATION_LOGGER.deprecated("Script: returning default values for missing document values is deprecated. " +
97-
"Set system property '-Des.scripting.exception_for_missing_value=true' " +
98-
"to make behaviour compatible with future major versions.");
9988
scriptService = new ScriptService(settings, Collections.unmodifiableMap(engines), Collections.unmodifiableMap(contexts));
10089
}
10190

server/src/test/java/org/elasticsearch/index/fielddata/ScriptDocValuesMissingV6BehaviourTests.java

Lines changed: 0 additions & 195 deletions
This file was deleted.

0 commit comments

Comments
 (0)