2020package org .elasticsearch .script .expression ;
2121
2222import 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 ;
2529import org .elasticsearch .script .ScriptException ;
2630import org .elasticsearch .script .SearchScript ;
2731import org .elasticsearch .search .lookup .SearchLookup ;
28- import org .elasticsearch .test .ESSingleNodeTestCase ;
32+ import org .elasticsearch .test .ESTestCase ;
2933
34+ import java .io .IOException ;
3035import java .text .ParseException ;
3136import java .util .Collections ;
3237
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 ;
3646
3747 @ Override
3848 public void setUp () throws Exception {
3949 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+
4167 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 );
4469 }
4570
4671 private SearchScript .LeafFactory compile (String expression ) {
@@ -50,22 +75,38 @@ private SearchScript.LeafFactory compile(String expression) {
5075
5176 public void testNeedsScores () {
5277 assertFalse (compile ("1.2" ).needs_score ());
53- assertFalse (compile ("doc['d '].value" ).needs_score ());
78+ assertFalse (compile ("doc['field '].value" ).needs_score ());
5479 assertTrue (compile ("1/_score" ).needs_score ());
55- assertTrue (compile ("doc['d '].value * _score" ).needs_score ());
80+ assertTrue (compile ("doc['field '].value * _score" ).needs_score ());
5681 }
5782
5883 public void testCompileError () {
5984 ScriptException e = expectThrows (ScriptException .class , () -> {
60- compile ("doc['d '].value * *@#)(@$*@#$ + 4" );
85+ compile ("doc['field '].value * *@#)(@$*@#$ + 4" );
6186 });
6287 assertTrue (e .getCause () instanceof ParseException );
6388 }
6489
6590 public void testLinkError () {
6691 ScriptException e = expectThrows (ScriptException .class , () -> {
67- compile ("doc['e '].value * 5" );
92+ compile ("doc['nonexistent '].value * 5" );
6893 });
6994 assertTrue (e .getCause () instanceof ParseException );
7095 }
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+ }
71112}
0 commit comments