Skip to content

Commit

Permalink
Make sure aliases can be accessed through 'doc'.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Jun 25, 2018
1 parent 2041ca6 commit 4145db5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void setUp() throws Exception {
NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE);
MapperService mapperService = mock(MapperService.class);
when(mapperService.fullName("field")).thenReturn(fieldType);
when(mapperService.fullName("alias")).thenReturn(fieldType);

SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
when(doubleValues.advanceExact(anyInt())).thenReturn(true);
Expand Down Expand Up @@ -100,4 +101,12 @@ public void testFieldAccess() throws IOException {
double result = script.runAsDouble();
assertEquals(2.718, result, 0.0);
}

public void testFieldAccessWithFieldAlias() throws IOException {
SearchScript script = compile("doc['alias'].value").newInstance(null);
script.setDocument(1);

double result = script.runAsDouble();
assertEquals(2.718, result, 0.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.lookup;

import org.elasticsearch.index.fielddata.AtomicFieldData;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LeafDocLookupTests extends ESTestCase {
private ScriptDocValues<?> docValues;
private LeafDocLookup docLookup;

@Before
public void setUp() throws Exception {
super.setUp();

MappedFieldType fieldType = mock(MappedFieldType.class);
when(fieldType.name()).thenReturn("field");
when(fieldType.valueForDisplay(anyObject())).then(returnsFirstArg());

MapperService mapperService = mock(MapperService.class);
when(mapperService.fullName("field")).thenReturn(fieldType);
when(mapperService.fullName("alias")).thenReturn(fieldType);

docValues = mock(ScriptDocValues.class);

AtomicFieldData atomicFieldData = mock(AtomicFieldData.class);
doReturn(docValues).when(atomicFieldData).getScriptValues();

IndexFieldData<?> fieldData = mock(IndexFieldData.class);
when(fieldData.getFieldName()).thenReturn("field");
doReturn(atomicFieldData).when(fieldData).load(anyObject());

docLookup = new LeafDocLookup(mapperService,
ignored -> fieldData,
new String[] { "type" },
null);
}

public void testBasicLookup() {
ScriptDocValues<?> fetchedDocValues = docLookup.get("field");
assertEquals(docValues, fetchedDocValues);
}

public void testLookupWithFieldAlias() {
ScriptDocValues<?> fetchedDocValues = docLookup.get("alias");
assertEquals(docValues, fetchedDocValues);
}
}

0 comments on commit 4145db5

Please sign in to comment.