17
17
import org .elasticsearch .common .geo .GeoUtils ;
18
18
import org .elasticsearch .common .time .DateUtils ;
19
19
import org .elasticsearch .geometry .utils .Geohash ;
20
+ import org .elasticsearch .script .Field ;
21
+ import org .elasticsearch .script .FieldValues ;
22
+ import org .elasticsearch .script .InvalidConversion ;
20
23
import org .elasticsearch .script .JodaCompatibleZonedDateTime ;
21
24
22
25
import java .io .IOException ;
25
28
import java .util .AbstractList ;
26
29
import java .util .Arrays ;
27
30
import java .util .Comparator ;
31
+ import java .util .List ;
32
+ import java .util .concurrent .TimeUnit ;
28
33
import java .util .function .UnaryOperator ;
29
34
30
35
/**
35
40
* return as a single {@link ScriptDocValues} instance can be reused to return
36
41
* values form multiple documents.
37
42
*/
38
- public abstract class ScriptDocValues <T > extends AbstractList <T > {
43
+ public abstract class ScriptDocValues <T > extends AbstractList <T > implements FieldValues < T > {
39
44
40
45
/**
41
46
* Set the current doc ID.
@@ -68,6 +73,31 @@ public final void sort(Comparator<? super T> c) {
68
73
throw new UnsupportedOperationException ("doc values are unmodifiable" );
69
74
}
70
75
76
+ public abstract Field <T > toField (String fieldName );
77
+
78
+ public List <T > getValues () {
79
+ return this ;
80
+ }
81
+
82
+ public T getNonPrimitiveValue () {
83
+ return get (0 );
84
+ }
85
+
86
+ public long getLongValue () {
87
+ throw new InvalidConversion (this .getClass (), long .class );
88
+ }
89
+
90
+ public double getDoubleValue () {
91
+ throw new InvalidConversion (this .getClass (), double .class );
92
+ }
93
+
94
+ protected void throwIfEmpty () {
95
+ if (size () == 0 ) {
96
+ throw new IllegalStateException ("A document doesn't have a value for a field! " +
97
+ "Use doc[<field>].size()==0 to check if a document is missing a field!" );
98
+ }
99
+ }
100
+
71
101
public static final class Longs extends ScriptDocValues <Long > {
72
102
private final SortedNumericDocValues in ;
73
103
private long [] values = new long [0 ];
@@ -107,17 +137,31 @@ public long getValue() {
107
137
108
138
@ Override
109
139
public Long get (int index ) {
110
- if (count == 0 ) {
111
- throw new IllegalStateException ("A document doesn't have a value for a field! " +
112
- "Use doc[<field>].size()==0 to check if a document is missing a field!" );
113
- }
140
+ throwIfEmpty ();
114
141
return values [index ];
115
142
}
116
143
117
144
@ Override
118
145
public int size () {
119
146
return count ;
120
147
}
148
+
149
+ @ Override
150
+ public long getLongValue () {
151
+ throwIfEmpty ();
152
+ return values [0 ];
153
+ }
154
+
155
+ @ Override
156
+ public double getDoubleValue () {
157
+ throwIfEmpty ();
158
+ return values [0 ];
159
+ }
160
+
161
+ @ Override
162
+ public Field <Long > toField (String fieldName ) {
163
+ return new Field .LongField (fieldName , this );
164
+ }
121
165
}
122
166
123
167
public static final class Dates extends ScriptDocValues <JodaCompatibleZonedDateTime > {
@@ -192,6 +236,29 @@ void refreshArray() throws IOException {
192
236
}
193
237
}
194
238
}
239
+
240
+ @ Override
241
+ public long getLongValue () {
242
+ throwIfEmpty ();
243
+ Instant dt = dates [0 ].toInstant ();
244
+ if (isNanos ) {
245
+ return TimeUnit .SECONDS .toNanos (dt .getEpochSecond ()) + dt .getNano ();
246
+ }
247
+ return dt .toEpochMilli ();
248
+ }
249
+
250
+ @ Override
251
+ public double getDoubleValue () {
252
+ return getLongValue ();
253
+ }
254
+
255
+ @ Override
256
+ public Field <JodaCompatibleZonedDateTime > toField (String fieldName ) {
257
+ if (isNanos ) {
258
+ return new Field .DateNanosField (fieldName , this );
259
+ }
260
+ return new Field .DateMillisField (fieldName , this );
261
+ }
195
262
}
196
263
197
264
public static final class Doubles extends ScriptDocValues <Double > {
@@ -246,6 +313,22 @@ public Double get(int index) {
246
313
public int size () {
247
314
return count ;
248
315
}
316
+
317
+ @ Override
318
+ public long getLongValue () {
319
+ return (long ) getDoubleValue ();
320
+ }
321
+
322
+ @ Override
323
+ public double getDoubleValue () {
324
+ throwIfEmpty ();
325
+ return values [0 ];
326
+ }
327
+
328
+ @ Override
329
+ public Field <Double > toField (String fieldName ) {
330
+ return new Field .DoubleField (fieldName , this );
331
+ }
249
332
}
250
333
251
334
public abstract static class Geometry <T > extends ScriptDocValues <T > {
@@ -436,6 +519,11 @@ public double getMercatorHeight() {
436
519
public GeoBoundingBox getBoundingBox () {
437
520
return size () == 0 ? null : boundingBox ;
438
521
}
522
+
523
+ @ Override
524
+ public Field <GeoPoint > toField (String fieldName ) {
525
+ return new Field .GeoPointField (fieldName , this );
526
+ }
439
527
}
440
528
441
529
public static final class Booleans extends ScriptDocValues <Boolean > {
@@ -496,6 +584,22 @@ private static boolean[] grow(boolean[] array, int minSize) {
496
584
return array ;
497
585
}
498
586
587
+ @ Override
588
+ public long getLongValue () {
589
+ throwIfEmpty ();
590
+ return values [0 ] ? 1L : 0L ;
591
+ }
592
+
593
+ @ Override
594
+ public double getDoubleValue () {
595
+ throwIfEmpty ();
596
+ return values [0 ] ? 1.0D : 0.0D ;
597
+ }
598
+
599
+ @ Override
600
+ public Field <Boolean > toField (String fieldName ) {
601
+ return new Field .BooleanField (fieldName , this );
602
+ }
499
603
}
500
604
501
605
abstract static class BinaryScriptDocValues <T > extends ScriptDocValues <T > {
@@ -568,6 +672,21 @@ protected String bytesToString(BytesRef bytes) {
568
672
public final String getValue () {
569
673
return get (0 );
570
674
}
675
+
676
+ @ Override
677
+ public long getLongValue () {
678
+ return Long .parseLong (get (0 ));
679
+ }
680
+
681
+ @ Override
682
+ public double getDoubleValue () {
683
+ return Double .parseDouble (get (0 ));
684
+ }
685
+
686
+ @ Override
687
+ public Field <String > toField (String fieldName ) {
688
+ return new Field .StringField (fieldName , this );
689
+ }
571
690
}
572
691
573
692
public static final class BytesRefs extends BinaryScriptDocValues <BytesRef > {
@@ -594,5 +713,9 @@ public BytesRef getValue() {
594
713
return get (0 );
595
714
}
596
715
716
+ @ Override
717
+ public Field <BytesRef > toField (String fieldName ) {
718
+ return new Field .BytesRefField (fieldName , this );
719
+ }
597
720
}
598
721
}
0 commit comments