Skip to content

Commit 4145db5

Browse files
committed
Make sure aliases can be accessed through 'doc'.
1 parent 2041ca6 commit 4145db5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void setUp() throws Exception {
5151
NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE);
5252
MapperService mapperService = mock(MapperService.class);
5353
when(mapperService.fullName("field")).thenReturn(fieldType);
54+
when(mapperService.fullName("alias")).thenReturn(fieldType);
5455

5556
SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
5657
when(doubleValues.advanceExact(anyInt())).thenReturn(true);
@@ -100,4 +101,12 @@ public void testFieldAccess() throws IOException {
100101
double result = script.runAsDouble();
101102
assertEquals(2.718, result, 0.0);
102103
}
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+
}
103112
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.search.lookup;
20+
21+
import org.elasticsearch.index.fielddata.AtomicFieldData;
22+
import org.elasticsearch.index.fielddata.IndexFieldData;
23+
import org.elasticsearch.index.fielddata.ScriptDocValues;
24+
import org.elasticsearch.index.mapper.MappedFieldType;
25+
import org.elasticsearch.index.mapper.MapperService;
26+
import org.elasticsearch.test.ESTestCase;
27+
import org.junit.Before;
28+
29+
import static org.mockito.AdditionalAnswers.returnsFirstArg;
30+
import static org.mockito.Matchers.anyObject;
31+
import static org.mockito.Mockito.doReturn;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
34+
35+
public class LeafDocLookupTests extends ESTestCase {
36+
private ScriptDocValues<?> docValues;
37+
private LeafDocLookup docLookup;
38+
39+
@Before
40+
public void setUp() throws Exception {
41+
super.setUp();
42+
43+
MappedFieldType fieldType = mock(MappedFieldType.class);
44+
when(fieldType.name()).thenReturn("field");
45+
when(fieldType.valueForDisplay(anyObject())).then(returnsFirstArg());
46+
47+
MapperService mapperService = mock(MapperService.class);
48+
when(mapperService.fullName("field")).thenReturn(fieldType);
49+
when(mapperService.fullName("alias")).thenReturn(fieldType);
50+
51+
docValues = mock(ScriptDocValues.class);
52+
53+
AtomicFieldData atomicFieldData = mock(AtomicFieldData.class);
54+
doReturn(docValues).when(atomicFieldData).getScriptValues();
55+
56+
IndexFieldData<?> fieldData = mock(IndexFieldData.class);
57+
when(fieldData.getFieldName()).thenReturn("field");
58+
doReturn(atomicFieldData).when(fieldData).load(anyObject());
59+
60+
docLookup = new LeafDocLookup(mapperService,
61+
ignored -> fieldData,
62+
new String[] { "type" },
63+
null);
64+
}
65+
66+
public void testBasicLookup() {
67+
ScriptDocValues<?> fetchedDocValues = docLookup.get("field");
68+
assertEquals(docValues, fetchedDocValues);
69+
}
70+
71+
public void testLookupWithFieldAlias() {
72+
ScriptDocValues<?> fetchedDocValues = docLookup.get("alias");
73+
assertEquals(docValues, fetchedDocValues);
74+
}
75+
}

0 commit comments

Comments
 (0)