20
20
package org .elasticsearch .index .fielddata ;
21
21
22
22
import org .elasticsearch .index .fielddata .ScriptDocValues .Dates ;
23
+ import org .elasticsearch .script .ScriptModule ;
23
24
import org .elasticsearch .test .ESTestCase ;
24
25
import org .joda .time .DateTime ;
25
26
import org .joda .time .DateTimeZone ;
32
33
import java .security .Permissions ;
33
34
import java .security .PrivilegedAction ;
34
35
import java .security .ProtectionDomain ;
36
+ import java .util .Arrays ;
37
+ import java .util .Collections ;
35
38
import java .util .HashSet ;
36
39
import java .util .Set ;
37
40
import java .util .function .Consumer ;
@@ -43,7 +46,28 @@ public void test() throws IOException {
43
46
long [][] values = new long [between (3 , 10 )][];
44
47
ReadableDateTime [][] expectedDates = new ReadableDateTime [values .length ][];
45
48
for (int d = 0 ; d < values .length ; d ++) {
46
- values [d ] = new long [randomBoolean () ? randomBoolean () ? 0 : 1 : between (2 , 100 )];
49
+ switch (d ) {
50
+ case 0 :
51
+ // empty
52
+ values [d ] = new long [0 ];
53
+ break ;
54
+ case 1 :
55
+ // single value
56
+ values [d ] = new long [1 ];
57
+ break ;
58
+ case 2 :
59
+ // multivalued
60
+ values [d ] = new long [between (2 , 100 )];
61
+ break ;
62
+ default :
63
+ // random
64
+ values [d ] = new long [between (0 , 5 )];
65
+ break ;
66
+ }
67
+ }
68
+ Collections .shuffle (Arrays .asList (values ), random ());
69
+
70
+ for (int d = 0 ; d < values .length ; d ++) {
47
71
expectedDates [d ] = new ReadableDateTime [values [d ].length ];
48
72
for (int i = 0 ; i < values [d ].length ; i ++) {
49
73
expectedDates [d ][i ] = new DateTime (randomNonNegativeLong (), DateTimeZone .UTC );
@@ -58,16 +82,17 @@ public void test() throws IOException {
58
82
createTempDir ();
59
83
});
60
84
61
- for (int round = 0 ; round < 10 ; round ++) {
62
- int d = between (0 , values .length - 1 );
85
+ for (int d = 0 ; d < values .length ; d ++) {
63
86
dates .setNextDocId (d );
64
87
if (expectedDates [d ].length > 0 ) {
65
88
assertEquals (expectedDates [d ][0 ] , dates .getValue ());
66
89
assertEquals (expectedDates [d ][0 ] , dates .getDate ());
67
- } else {
90
+ } else if ( ScriptModule . EXCEPTION_FOR_MISSING_VALUE ) {
68
91
Exception e = expectThrows (IllegalStateException .class , () -> dates .getValue ());
69
92
assertEquals ("A document doesn't have a value for a field! " +
70
93
"Use doc[<field>].size()==0 to check if a document is missing a field!" , e .getMessage ());
94
+ } else {
95
+ assertEquals (0 , dates .getValue ().getMillis ()); // Epoch
71
96
}
72
97
assertEquals (values [d ].length , dates .size ());
73
98
for (int i = 0 ; i < values [d ].length ; i ++) {
0 commit comments