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
+ when (mapperService .fullName ("alias" )).thenReturn (fieldType );
55
+
56
+ SortedNumericDoubleValues doubleValues = mock (SortedNumericDoubleValues .class );
57
+ when (doubleValues .advanceExact (anyInt ())).thenReturn (true );
58
+ when (doubleValues .nextValue ()).thenReturn (2.718 );
59
+
60
+ AtomicNumericFieldData atomicFieldData = mock (AtomicNumericFieldData .class );
61
+ when (atomicFieldData .getDoubleValues ()).thenReturn (doubleValues );
62
+
63
+ IndexNumericFieldData fieldData = mock (IndexNumericFieldData .class );
64
+ when (fieldData .getFieldName ()).thenReturn ("field" );
65
+ when (fieldData .load (anyObject ())).thenReturn (atomicFieldData );
66
+
41
67
service = new ExpressionScriptEngine (Settings .EMPTY );
42
- QueryShardContext shardContext = index .newQueryShardContext (0 , null , () -> 0 , null );
43
- lookup = new SearchLookup (index .mapperService (), shardContext ::getForField , null );
68
+ lookup = new SearchLookup (mapperService , ignored -> fieldData , null );
44
69
}
45
70
46
71
private SearchScript .LeafFactory compile (String expression ) {
@@ -50,22 +75,38 @@ private SearchScript.LeafFactory compile(String expression) {
50
75
51
76
public void testNeedsScores () {
52
77
assertFalse (compile ("1.2" ).needs_score ());
53
- assertFalse (compile ("doc['d '].value" ).needs_score ());
78
+ assertFalse (compile ("doc['field '].value" ).needs_score ());
54
79
assertTrue (compile ("1/_score" ).needs_score ());
55
- assertTrue (compile ("doc['d '].value * _score" ).needs_score ());
80
+ assertTrue (compile ("doc['field '].value * _score" ).needs_score ());
56
81
}
57
82
58
83
public void testCompileError () {
59
84
ScriptException e = expectThrows (ScriptException .class , () -> {
60
- compile ("doc['d '].value * *@#)(@$*@#$ + 4" );
85
+ compile ("doc['field '].value * *@#)(@$*@#$ + 4" );
61
86
});
62
87
assertTrue (e .getCause () instanceof ParseException );
63
88
}
64
89
65
90
public void testLinkError () {
66
91
ScriptException e = expectThrows (ScriptException .class , () -> {
67
- compile ("doc['e '].value * 5" );
92
+ compile ("doc['nonexistent '].value * 5" );
68
93
});
69
94
assertTrue (e .getCause () instanceof ParseException );
70
95
}
96
+
97
+ public void testFieldAccess () throws IOException {
98
+ SearchScript script = compile ("doc['field'].value" ).newInstance (null );
99
+ script .setDocument (1 );
100
+
101
+ double result = script .runAsDouble ();
102
+ assertEquals (2.718 , result , 0.0 );
103
+ }
104
+
105
+ public void testFieldAccessWithFieldAlias () throws IOException {
106
+ SearchScript script = compile ("doc['alias'].value" ).newInstance (null );
107
+ script .setDocument (1 );
108
+
109
+ double result = script .runAsDouble ();
110
+ assertEquals (2.718 , result , 0.0 );
111
+ }
71
112
}
0 commit comments