Skip to content

Commit 842118a

Browse files
committed
Remove getValue and getValues from Field (elastic#79516)
With duck-typing in Painless and potentially non-dynamic languages requiring a cast under the current design, this change removes unnecessary methods from the Field base class. The removal of getValue and getValues allows sub classes to return primitives from these methods and opens up potential design space for performance improvements. Note this removes getLong from the unsigned long field; however, this is currently undocumented and unused behavior.
1 parent c993176 commit 842118a

File tree

14 files changed

+31
-87
lines changed

14 files changed

+31
-87
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class org.elasticsearch.script.field.Field @dynamic_type {
1313
String getName()
1414
boolean isEmpty()
1515
int size()
16-
List getValues()
17-
def getValue(def)
18-
def getValue(int, def)
1916
}
2017

2118
class org.elasticsearch.script.DocBasedScript {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface LeafFieldData extends Accountable, Releasable {
2828
/**
2929
* Returns an {@code Field} for use in accessing field values in scripting.
3030
*/
31-
default DocValuesField<?> getScriptField(String name) {
31+
default DocValuesField getScriptField(String name) {
3232
throw new UnsupportedOperationException();
3333
}
3434

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public DocBasedScript(DocReader docReader) {
2323
this.docReader = docReader;
2424
}
2525

26-
public Field<?> field(String fieldName) {
26+
public Field field(String fieldName) {
2727
if (docReader == null) {
28-
return new EmptyField<>(fieldName);
28+
return new EmptyField(fieldName);
2929
}
3030
return docReader.field(fieldName);
3131
}
3232

33-
public Stream<Field<?>> fields(String fieldGlob) {
33+
public Stream<Field> fields(String fieldGlob) {
3434
if (docReader == null) {
3535
return Stream.empty();
3636
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
*/
2323
public interface DocReader {
2424
/** New-style field access */
25-
Field<?> field(String fieldName);
25+
Field field(String fieldName);
2626

2727
/** New-style field iterator */
28-
Stream<Field<?>> fields(String fieldGlob);
28+
Stream<Field> fields(String fieldGlob);
2929

3030
/** Set the underlying docId */
3131
void setDocument(int docID);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ public DocValuesDocReader(SearchLookup searchLookup, LeafReaderContext leafConte
3939
}
4040

4141
@Override
42-
public Field<?> field(String fieldName) {
42+
public Field field(String fieldName) {
4343
LeafDocLookup leafDocLookup = leafSearchLookup.doc();
4444

4545
if (leafDocLookup.containsKey(fieldName) == false) {
46-
return new EmptyField<>(fieldName);
46+
return new EmptyField(fieldName);
4747
}
4848

4949
return leafDocLookup.getScriptField(fieldName);
5050
}
5151

5252

5353
@Override
54-
public Stream<Field<?>> fields(String fieldGlob) {
54+
public Stream<Field> fields(String fieldGlob) {
5555
throw new UnsupportedOperationException("not implemented");
5656
}
5757

server/src/main/java/org/elasticsearch/script/field/DocValuesField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import java.io.IOException;
1212

13-
public interface DocValuesField<T> extends Field<T> {
13+
public interface DocValuesField extends Field {
1414

1515
/** Set the current document ID. */
1616
void setNextDocId(int docId) throws IOException;

server/src/main/java/org/elasticsearch/script/field/EmptyField.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88

99
package org.elasticsearch.script.field;
1010

11-
import java.util.Collections;
12-
import java.util.List;
13-
1411
/**
1512
* Script field with no mapping, always returns {@code defaultValue}.
1613
*/
17-
public class EmptyField<T> implements Field<T> {
14+
public class EmptyField implements Field {
1815

1916
private final String name;
2017

@@ -36,19 +33,4 @@ public boolean isEmpty() {
3633
public int size() {
3734
return 0;
3835
}
39-
40-
@Override
41-
public List<T> getValues() {
42-
return Collections.emptyList();
43-
}
44-
45-
@Override
46-
public T getValue(T defaultValue) {
47-
return defaultValue;
48-
}
49-
50-
@Override
51-
public T getValue(int index, T defaultValue) {
52-
return defaultValue;
53-
}
5436
}

server/src/main/java/org/elasticsearch/script/field/Field.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
package org.elasticsearch.script.field;
1111

12-
import java.util.List;
13-
1412
/** A field in a document accessible via scripting. */
15-
public interface Field<T> {
13+
public interface Field {
1614

1715
/** Returns the name of this field. */
1816
String getName();
@@ -22,13 +20,4 @@ public interface Field<T> {
2220

2321
/** Returns the number of values this field has. */
2422
int size();
25-
26-
/** Get all values of a multivalued field. If {@code isEmpty()} this returns an empty list. */
27-
List<T> getValues();
28-
29-
/** Get the first value of a field, if {@code isEmpty()} return defaultValue instead */
30-
T getValue(T defaultValue);
31-
32-
/** Get the value of a field as the specified index. */
33-
T getValue(int index, T defaultValue);
3423
}

server/src/main/java/org/elasticsearch/search/lookup/LeafDocLookup.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
import java.util.function.Function;
2727

2828
public class LeafDocLookup implements Map<String, ScriptDocValues<?>> {
29-
29+
3030
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(LeafDocLookup.class);
3131
static final String TYPES_DEPRECATION_KEY = "type-field-doc-lookup";
3232
static final String TYPES_DEPRECATION_MESSAGE =
3333
"[types removal] Looking up doc types [_type] in scripts is deprecated.";
3434

35-
private final Map<String, DocValuesField<?>> localCacheScriptFieldData = new HashMap<>(4);
35+
private final Map<String, DocValuesField> localCacheScriptFieldData = new HashMap<>(4);
3636
private final Map<String, ScriptDocValues<?>> localCacheFieldData = new HashMap<>(4);
3737
private final Function<String, MappedFieldType> fieldTypeLookup;
3838
private final Function<MappedFieldType, IndexFieldData<?>> fieldDataLookup;
@@ -52,8 +52,8 @@ public void setDocument(int docId) {
5252
this.docId = docId;
5353
}
5454

55-
public DocValuesField<?> getScriptField(String fieldName) {
56-
DocValuesField<?> field = localCacheScriptFieldData.get(fieldName);
55+
public DocValuesField getScriptField(String fieldName) {
56+
DocValuesField field = localCacheScriptFieldData.get(fieldName);
5757

5858
if (field == null) {
5959
final MappedFieldType fieldType = fieldTypeLookup.apply(fieldName);
@@ -64,9 +64,9 @@ public DocValuesField<?> getScriptField(String fieldName) {
6464

6565
// Load the field data on behalf of the script. Otherwise, it would require
6666
// additional permissions to deal with pagedbytes/ramusagestimator/etc.
67-
field = AccessController.doPrivileged(new PrivilegedAction<DocValuesField<?>>() {
67+
field = AccessController.doPrivileged(new PrivilegedAction<DocValuesField>() {
6868
@Override
69-
public DocValuesField<?> run() {
69+
public DocValuesField run() {
7070
return fieldDataLookup.apply(fieldType).load(reader).getScriptField(fieldName);
7171
}
7272
});

x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongDocValuesField.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static org.elasticsearch.search.DocValueFormat.MASK_2_63;
2121
import static org.elasticsearch.xpack.unsignedlong.UnsignedLongFieldMapper.BIGINTEGER_2_64_MINUS_ONE;
2222

23-
public class UnsignedLongDocValuesField implements UnsignedLongField, DocValuesField<Long> {
23+
public class UnsignedLongDocValuesField implements UnsignedLongField, DocValuesField {
2424

2525
private final SortedNumericDocValues input;
2626
private long[] values = new long[0];
@@ -89,26 +89,12 @@ public List<Long> getValues() {
8989
}
9090

9191
@Override
92-
public Long getValue(Long defaultValue) {
92+
public long getValue(long defaultValue) {
9393
return getValue(0, defaultValue);
9494
}
9595

9696
@Override
97-
public Long getValue(int index, Long defaultValue) {
98-
if (isEmpty() || index < 0 || index >= count) {
99-
return defaultValue;
100-
}
101-
102-
return toFormatted(index);
103-
}
104-
105-
@Override
106-
public long getLong(long defaultValue) {
107-
return getLong(0, defaultValue);
108-
}
109-
110-
@Override
111-
public long getLong(int index, long defaultValue) {
97+
public long getValue(int index, long defaultValue) {
11298
if (isEmpty() || index < 0 || index >= count) {
11399
return defaultValue;
114100
}

x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongField.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
import java.math.BigInteger;
1313
import java.util.List;
1414

15-
public interface UnsignedLongField extends Field<Long> {
15+
public interface UnsignedLongField extends Field {
1616

1717
/** Returns the 0th index value as an {@code long} if it exists, otherwise {@code defaultValue}. */
18-
long getLong(long defaultValue);
18+
long getValue(long defaultValue);
1919

2020
/** Returns the value at {@code index} as an {@code long} if it exists, otherwise {@code defaultValue}. */
21-
long getLong(int index, long defaultValue);
21+
long getValue(int index, long defaultValue);
22+
23+
/** Return all the values as a {@code List}. */
24+
List<Long> getValues();
2225

2326
/** Returns the 0th index value as a {@code BigInteger} if it exists, otherwise {@code defaultValue}. */
2427
BigInteger getBigInteger(BigInteger defaultValue);

x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongLeafFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public ScriptDocValues<?> getScriptValues() {
7979
}
8080

8181
@Override
82-
public DocValuesField<?> getScriptField(String name) {
82+
public DocValuesField getScriptField(String name) {
8383
return new UnsignedLongDocValuesField(getLongValues(), name);
8484
}
8585

x-pack/plugin/mapper-unsigned-long/src/main/resources/org/elasticsearch/xpack/unsignedlong/org.elasticsearch.xpack.unsignedlong.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class org.elasticsearch.xpack.unsignedlong.UnsignedLongScriptDocValues {
1212
}
1313

1414
class org.elasticsearch.xpack.unsignedlong.UnsignedLongField @dynamic_type {
15-
long getLong(long)
16-
long getLong(int, long)
15+
long getValue(long)
16+
long getValue(int, long)
17+
List getValues()
1718
BigInteger getBigInteger(BigInteger)
1819
BigInteger getBigInteger(int, BigInteger)
1920
List getBigIntegers()

x-pack/plugin/mapper-unsigned-long/src/yamlRestTest/resources/rest-api-spec/test/50_script_values.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,7 @@ setup:
4646
- match: { hits.hits.2.fields.scripted_ul.0: -9223372036854775808 }
4747
- match: { hits.hits.3.fields.scripted_ul.0: 9223372036854775807 }
4848
- match: { hits.hits.4.fields.scripted_ul.0: 0 }
49-
- do:
50-
search:
51-
index: test1
52-
body:
53-
sort: [ { ul: desc } ]
54-
script_fields:
55-
scripted_ul:
56-
script:
57-
source: "field('ul').getLong(1000L)"
5849

59-
- match: { hits.hits.0.fields.scripted_ul.0: -1 }
60-
- match: { hits.hits.1.fields.scripted_ul.0: -3 }
61-
- match: { hits.hits.2.fields.scripted_ul.0: -9223372036854775808 }
62-
- match: { hits.hits.3.fields.scripted_ul.0: 9223372036854775807 }
63-
- match: { hits.hits.4.fields.scripted_ul.0: 0 }
6450
- do:
6551
search:
6652
index: test1

0 commit comments

Comments
 (0)