20
20
package org .elasticsearch .script .expression ;
21
21
22
22
import org .elasticsearch .common .settings .Settings ;
23
- import org .elasticsearch .index .IndexService ;
24
- import org .elasticsearch .index .query .QueryShardContext ;
23
+ import org .elasticsearch .index .fielddata .AtomicNumericFieldData ;
24
+ import org .elasticsearch .index .fielddata .IndexNumericFieldData ;
25
+ import org .elasticsearch .index .fielddata .SortedNumericDoubleValues ;
26
+ import org .elasticsearch .index .mapper .MapperService ;
27
+ import org .elasticsearch .index .mapper .NumberFieldMapper .NumberFieldType ;
28
+ import org .elasticsearch .index .mapper .NumberFieldMapper .NumberType ;
25
29
import org .elasticsearch .script .ScriptException ;
26
30
import org .elasticsearch .script .SearchScript ;
27
31
import org .elasticsearch .search .lookup .SearchLookup ;
28
- import org .elasticsearch .test .ESSingleNodeTestCase ;
32
+ import org .elasticsearch .test .ESTestCase ;
29
33
34
+ import java .io .IOException ;
30
35
import java .text .ParseException ;
31
36
import java .util .Collections ;
32
37
33
- public class ExpressionTests extends ESSingleNodeTestCase {
34
- ExpressionScriptEngine service ;
35
- SearchLookup lookup ;
38
+ import static org .mockito .Matchers .anyInt ;
39
+ import static org .mockito .Matchers .anyObject ;
40
+ import static org .mockito .Mockito .mock ;
41
+ import static org .mockito .Mockito .when ;
42
+
43
+ public class ExpressionTests extends ESTestCase {
44
+ private ExpressionScriptEngine service ;
45
+ private SearchLookup lookup ;
36
46
37
47
@ Override
38
48
public void setUp () throws Exception {
39
49
super .setUp ();
40
- IndexService index = createIndex ("test" , Settings .EMPTY , "type" , "d" , "type=double" );
50
+
51
+ NumberFieldType fieldType = new NumberFieldType (NumberType .DOUBLE );
52
+ MapperService mapperService = mock (MapperService .class );
53
+ when (mapperService .fullName ("field" )).thenReturn (fieldType );
54
+
55
+ SortedNumericDoubleValues doubleValues = mock (SortedNumericDoubleValues .class );
56
+ when (doubleValues .advanceExact (anyInt ())).thenReturn (true );
57
+ when (doubleValues .nextValue ()).thenReturn (2.718 );
58
+
59
+ AtomicNumericFieldData atomicFieldData = mock (AtomicNumericFieldData .class );
60
+ when (atomicFieldData .getDoubleValues ()).thenReturn (doubleValues );
61
+
62
+ IndexNumericFieldData fieldData = mock (IndexNumericFieldData .class );
63
+ when (fieldData .getFieldName ()).thenReturn ("field" );
64
+ when (fieldData .load (anyObject ())).thenReturn (atomicFieldData );
65
+
41
66
service = new ExpressionScriptEngine (Settings .EMPTY );
42
- QueryShardContext shardContext = index .newQueryShardContext (0 , null , () -> 0 , null );
43
- lookup = new SearchLookup (index .mapperService (), shardContext ::getForField , null );
67
+ lookup = new SearchLookup (mapperService , ignored -> fieldData , null );
44
68
}
45
69
46
70
private SearchScript .LeafFactory compile (String expression ) {
@@ -50,22 +74,30 @@ private SearchScript.LeafFactory compile(String expression) {
50
74
51
75
public void testNeedsScores () {
52
76
assertFalse (compile ("1.2" ).needs_score ());
53
- assertFalse (compile ("doc['d '].value" ).needs_score ());
77
+ assertFalse (compile ("doc['field '].value" ).needs_score ());
54
78
assertTrue (compile ("1/_score" ).needs_score ());
55
- assertTrue (compile ("doc['d '].value * _score" ).needs_score ());
79
+ assertTrue (compile ("doc['field '].value * _score" ).needs_score ());
56
80
}
57
81
58
82
public void testCompileError () {
59
83
ScriptException e = expectThrows (ScriptException .class , () -> {
60
- compile ("doc['d '].value * *@#)(@$*@#$ + 4" );
84
+ compile ("doc['field '].value * *@#)(@$*@#$ + 4" );
61
85
});
62
86
assertTrue (e .getCause () instanceof ParseException );
63
87
}
64
88
65
89
public void testLinkError () {
66
90
ScriptException e = expectThrows (ScriptException .class , () -> {
67
- compile ("doc['e '].value * 5" );
91
+ compile ("doc['nonexistent '].value * 5" );
68
92
});
69
93
assertTrue (e .getCause () instanceof ParseException );
70
94
}
95
+
96
+ public void testFieldAccess () throws IOException {
97
+ SearchScript script = compile ("doc['field'].value" ).newInstance (null );
98
+ script .setDocument (1 );
99
+
100
+ double result = script .runAsDouble ();
101
+ assertEquals (2.718 , result , 0.0 );
102
+ }
71
103
}
0 commit comments