Skip to content

Commit 34180f2

Browse files
authored
Scripting: Remove getDate methods from ScriptDocValues (#30690)
The getDate() and getDates() existed prior to 5.x on long fields in scripting. In 5.x, a new Date type for ScriptDocValues was added. The getDate() and getDates() methods were left on long fields and added to date fields to ease the transition. This commit removes those methods for 7.0.
1 parent 67d8fc2 commit 34180f2

File tree

7 files changed

+21
-258
lines changed

7 files changed

+21
-258
lines changed

docs/reference/migration/migrate_7_0.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Elasticsearch 6.x in order to be readable by Elasticsearch 7.x.
3434
* <<breaking_70_api_changes>>
3535
* <<breaking_70_java_changes>>
3636
* <<breaking_70_settings_changes>>
37+
* <<breaking_70_scripting_changes>>
3738

3839

3940
include::migrate_7_0/aggregations.asciidoc[]
@@ -47,3 +48,4 @@ include::migrate_7_0/plugins.asciidoc[]
4748
include::migrate_7_0/api.asciidoc[]
4849
include::migrate_7_0/java.asciidoc[]
4950
include::migrate_7_0/settings.asciidoc[]
51+
include::migrate_7_0/scripting.asciidoc[]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[breaking_70_scripting_changes]]
2+
=== Scripting changes
3+
4+
==== getDate() and getDates() removed
5+
6+
Fields of type `long` and `date` had `getDate()` and `getDates()` methods
7+
(for multi valued fields) to get an object with date specific helper methods
8+
for the current doc value. In 5.3.0, `date` fields were changed to expose
9+
this same date object directly when calling `doc["myfield"].value`, and
10+
the getter methods for date objects were deprecated. These methods have
11+
now been removed. Instead, use `.value` on `date` fields, or explicitly
12+
parse `long` fields into a date object using
13+
`Instance.ofEpochMillis(doc["myfield"].value)`.

modules/lang-painless/src/main/resources/org/elasticsearch/painless/spi/org.elasticsearch.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,12 @@ class org.elasticsearch.index.fielddata.ScriptDocValues$Longs {
7474
Long get(int)
7575
long getValue()
7676
List getValues()
77-
org.joda.time.ReadableDateTime getDate()
78-
List getDates()
7977
}
8078

8179
class org.elasticsearch.index.fielddata.ScriptDocValues$Dates {
8280
org.joda.time.ReadableDateTime get(int)
8381
org.joda.time.ReadableDateTime getValue()
8482
List getValues()
85-
org.joda.time.ReadableDateTime getDate()
86-
List getDates()
8783
}
8884

8985
class org.elasticsearch.index.fielddata.ScriptDocValues$Doubles {

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/50_script_doc_values.yml

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,6 @@ setup:
106106
source: "doc.date.value"
107107
- match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }
108108

109-
- do:
110-
warnings:
111-
- getDate is no longer necessary on date fields as the value is now a date.
112-
search:
113-
body:
114-
script_fields:
115-
field:
116-
script:
117-
source: "doc['date'].date"
118-
- match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }
119-
120-
- do:
121-
warnings:
122-
- getDates is no longer necessary on date fields as the values are now dates.
123-
search:
124-
body:
125-
script_fields:
126-
field:
127-
script:
128-
source: "doc['date'].dates.get(0)"
129-
- match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }
130-
131109
---
132110
"geo_point":
133111
- do:
@@ -213,28 +191,6 @@ setup:
213191
source: "doc['long'].value"
214192
- match: { hits.hits.0.fields.field.0: 12348732141234 }
215193

216-
- do:
217-
warnings:
218-
- getDate on numeric fields is deprecated. Use a date field to get dates.
219-
search:
220-
body:
221-
script_fields:
222-
field:
223-
script:
224-
source: "doc['long'].date"
225-
- match: { hits.hits.0.fields.field.0: '2361-04-26T03:22:21.234Z' }
226-
227-
- do:
228-
warnings:
229-
- getDates on numeric fields is deprecated. Use a date field to get dates.
230-
search:
231-
body:
232-
script_fields:
233-
field:
234-
script:
235-
source: "doc['long'].dates.get(0)"
236-
- match: { hits.hits.0.fields.field.0: '2361-04-26T03:22:21.234Z' }
237-
238194
---
239195
"integer":
240196
- do:

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

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,19 @@ public final void sort(Comparator<? super T> c) {
9494
}
9595

9696
public static final class Longs extends ScriptDocValues<Long> {
97-
protected static final DeprecationLogger deprecationLogger = new DeprecationLogger(ESLoggerFactory.getLogger(Longs.class));
98-
9997
private final SortedNumericDocValues in;
100-
/**
101-
* Callback for deprecated fields. In production this should always point to
102-
* {@link #deprecationLogger} but tests will override it so they can test that
103-
* we use the required permissions when calling it.
104-
*/
105-
private final Consumer<String> deprecationCallback;
10698
private long[] values = new long[0];
10799
private int count;
108-
private Dates dates;
109-
private int docId = -1;
110100

111101
/**
112102
* Standard constructor.
113103
*/
114104
public Longs(SortedNumericDocValues in) {
115-
this(in, deprecationLogger::deprecated);
116-
}
117-
118-
/**
119-
* Constructor for testing the deprecation callback.
120-
*/
121-
Longs(SortedNumericDocValues in, Consumer<String> deprecationCallback) {
122105
this.in = in;
123-
this.deprecationCallback = deprecationCallback;
124106
}
125107

126108
@Override
127109
public void setNextDocId(int docId) throws IOException {
128-
this.docId = docId;
129110
if (in.advanceExact(docId)) {
130111
resize(in.docValueCount());
131112
for (int i = 0; i < count; i++) {
@@ -134,9 +115,6 @@ public void setNextDocId(int docId) throws IOException {
134115
} else {
135116
resize(0);
136117
}
137-
if (dates != null) {
138-
dates.setNextDocId(docId);
139-
}
140118
}
141119

142120
/**
@@ -148,37 +126,13 @@ protected void resize(int newSize) {
148126
values = ArrayUtil.grow(values, count);
149127
}
150128

151-
public SortedNumericDocValues getInternalValues() {
152-
return this.in;
153-
}
154-
155129
public long getValue() {
156130
if (count == 0) {
157131
return 0L;
158132
}
159133
return values[0];
160134
}
161135

162-
@Deprecated
163-
public ReadableDateTime getDate() throws IOException {
164-
deprecated("getDate on numeric fields is deprecated. Use a date field to get dates.");
165-
if (dates == null) {
166-
dates = new Dates(in);
167-
dates.setNextDocId(docId);
168-
}
169-
return dates.getValue();
170-
}
171-
172-
@Deprecated
173-
public List<ReadableDateTime> getDates() throws IOException {
174-
deprecated("getDates on numeric fields is deprecated. Use a date field to get dates.");
175-
if (dates == null) {
176-
dates = new Dates(in);
177-
dates.setNextDocId(docId);
178-
}
179-
return dates;
180-
}
181-
182136
@Override
183137
public Long get(int index) {
184138
return values[index];
@@ -188,22 +142,6 @@ public Long get(int index) {
188142
public int size() {
189143
return count;
190144
}
191-
192-
/**
193-
* Log a deprecation log, with the server's permissions, not the permissions of the
194-
* script calling this method. We need to do this to prevent errors when rolling
195-
* the log file.
196-
*/
197-
private void deprecated(String message) {
198-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
199-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
200-
@Override
201-
public Void run() {
202-
deprecationCallback.accept(message);
203-
return null;
204-
}
205-
});
206-
}
207145
}
208146

209147
public static final class Dates extends ScriptDocValues<ReadableDateTime> {
@@ -212,12 +150,6 @@ public static final class Dates extends ScriptDocValues<ReadableDateTime> {
212150
private static final ReadableDateTime EPOCH = new DateTime(0, DateTimeZone.UTC);
213151

214152
private final SortedNumericDocValues in;
215-
/**
216-
* Callback for deprecated fields. In production this should always point to
217-
* {@link #deprecationLogger} but tests will override it so they can test that
218-
* we use the required permissions when calling it.
219-
*/
220-
private final Consumer<String> deprecationCallback;
221153
/**
222154
* Values wrapped in {@link MutableDateTime}. Null by default an allocated on first usage so we allocate a reasonably size. We keep
223155
* this array so we don't have allocate new {@link MutableDateTime}s on every usage. Instead we reuse them for every document.
@@ -229,15 +161,7 @@ public static final class Dates extends ScriptDocValues<ReadableDateTime> {
229161
* Standard constructor.
230162
*/
231163
public Dates(SortedNumericDocValues in) {
232-
this(in, deprecationLogger::deprecated);
233-
}
234-
235-
/**
236-
* Constructor for testing deprecation logging.
237-
*/
238-
Dates(SortedNumericDocValues in, Consumer<String> deprecationCallback) {
239164
this.in = in;
240-
this.deprecationCallback = deprecationCallback;
241165
}
242166

243167
/**
@@ -251,24 +175,6 @@ public ReadableDateTime getValue() {
251175
return get(0);
252176
}
253177

254-
/**
255-
* Fetch the first value. Added for backwards compatibility with 5.x when date fields were {@link Longs}.
256-
*/
257-
@Deprecated
258-
public ReadableDateTime getDate() {
259-
deprecated("getDate is no longer necessary on date fields as the value is now a date.");
260-
return getValue();
261-
}
262-
263-
/**
264-
* Fetch all the values. Added for backwards compatibility with 5.x when date fields were {@link Longs}.
265-
*/
266-
@Deprecated
267-
public List<ReadableDateTime> getDates() {
268-
deprecated("getDates is no longer necessary on date fields as the values are now dates.");
269-
return this;
270-
}
271-
272178
@Override
273179
public ReadableDateTime get(int index) {
274180
if (index >= count) {
@@ -326,22 +232,6 @@ void refreshArray() throws IOException {
326232
dates[i] = new MutableDateTime(in.nextValue(), DateTimeZone.UTC);
327233
}
328234
}
329-
330-
/**
331-
* Log a deprecation log, with the server's permissions, not the permissions of the
332-
* script calling this method. We need to do this to prevent errors when rolling
333-
* the log file.
334-
*/
335-
private void deprecated(String message) {
336-
// Intentionally not calling SpecialPermission.check because this is supposed to be called by scripts
337-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
338-
@Override
339-
public Void run() {
340-
deprecationCallback.accept(message);
341-
return null;
342-
}
343-
});
344-
}
345235
}
346236

347237
public static final class Doubles extends ScriptDocValues<Double> {

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

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,11 @@ public void test() throws IOException {
5050
values[d][i] = expectedDates[d][i].getMillis();
5151
}
5252
}
53-
Set<String> warnings = new HashSet<>();
54-
Dates dates = wrap(values, deprecationMessage -> {
55-
warnings.add(deprecationMessage);
56-
/* Create a temporary directory to prove we are running with the
57-
* server's permissions. */
58-
createTempDir();
59-
});
60-
53+
Dates dates = wrap(values);
6154
for (int round = 0; round < 10; round++) {
6255
int d = between(0, values.length - 1);
6356
dates.setNextDocId(d);
6457
assertEquals(expectedDates[d].length > 0 ? expectedDates[d][0] : new DateTime(0, DateTimeZone.UTC), dates.getValue());
65-
assertEquals(expectedDates[d].length > 0 ? expectedDates[d][0] : new DateTime(0, DateTimeZone.UTC), dates.getDate());
6658

6759
assertEquals(values[d].length, dates.size());
6860
for (int i = 0; i < values[d].length; i++) {
@@ -72,33 +64,9 @@ public void test() throws IOException {
7264
Exception e = expectThrows(UnsupportedOperationException.class, () -> dates.add(new DateTime()));
7365
assertEquals("doc values are unmodifiable", e.getMessage());
7466
}
75-
76-
/*
77-
* Invoke getDates without any privileges to verify that
78-
* it still works without any. In particularly, this
79-
* verifies that the callback that we've configured
80-
* above works. That callback creates a temporary
81-
* directory which is not possible with "noPermissions".
82-
*/
83-
PermissionCollection noPermissions = new Permissions();
84-
AccessControlContext noPermissionsAcc = new AccessControlContext(
85-
new ProtectionDomain[] {
86-
new ProtectionDomain(null, noPermissions)
87-
}
88-
);
89-
AccessController.doPrivileged(new PrivilegedAction<Void>() {
90-
public Void run() {
91-
dates.getDates();
92-
return null;
93-
}
94-
}, noPermissionsAcc);
95-
96-
assertThat(warnings, containsInAnyOrder(
97-
"getDate is no longer necessary on date fields as the value is now a date.",
98-
"getDates is no longer necessary on date fields as the values are now dates."));
9967
}
10068

101-
private Dates wrap(long[][] values, Consumer<String> deprecationHandler) {
69+
private Dates wrap(long[][] values) {
10270
return new Dates(new AbstractSortedNumericDocValues() {
10371
long[] current;
10472
int i;
@@ -117,6 +85,6 @@ public int docValueCount() {
11785
public long nextValue() {
11886
return current[i++];
11987
}
120-
}, deprecationHandler);
88+
});
12189
}
12290
}

0 commit comments

Comments
 (0)