66
77package org .elasticsearch .xpack .runtimefields .mapper ;
88
9- import org .elasticsearch .action .fieldcaps .FieldCapabilities ;
10- import org .elasticsearch .action .fieldcaps .FieldCapabilitiesResponse ;
119import org .elasticsearch .common .CheckedConsumer ;
1210import org .elasticsearch .common .CheckedSupplier ;
1311import org .elasticsearch .common .Strings ;
1412import org .elasticsearch .common .settings .Settings ;
1513import org .elasticsearch .common .xcontent .XContentBuilder ;
1614import org .elasticsearch .common .xcontent .XContentFactory ;
1715import org .elasticsearch .index .mapper .FieldMapper ;
16+ import org .elasticsearch .index .mapper .MappedFieldType ;
1817import org .elasticsearch .index .mapper .MapperParsingException ;
1918import org .elasticsearch .index .mapper .MapperService ;
19+ import org .elasticsearch .index .mapper .MapperTestCase ;
2020import org .elasticsearch .plugins .Plugin ;
2121import org .elasticsearch .plugins .ScriptPlugin ;
2222import org .elasticsearch .script .ScriptContext ;
2323import org .elasticsearch .script .ScriptEngine ;
24- import org .elasticsearch .test .ESSingleNodeTestCase ;
25- import org .elasticsearch .test .InternalSettingsPlugin ;
2624import org .elasticsearch .xpack .runtimefields .BooleanScriptFieldScript ;
2725import org .elasticsearch .xpack .runtimefields .BooleanScriptFieldScriptTests ;
2826import org .elasticsearch .xpack .runtimefields .DateScriptFieldScript ;
4139import java .io .IOException ;
4240import java .util .Arrays ;
4341import java .util .Collection ;
44- import java .util .Map ;
42+ import java .util .List ;
4543import java .util .Set ;
4644
47- import static org .hamcrest .Matchers .arrayContainingInAnyOrder ;
4845import static org .hamcrest .Matchers .equalTo ;
4946import static org .hamcrest .Matchers .instanceOf ;
5047
51- public class RuntimeScriptFieldMapperTests extends ESSingleNodeTestCase {
48+ public class RuntimeScriptFieldMapperTests extends MapperTestCase {
5249
5350 private final String [] runtimeTypes ;
5451
@@ -58,8 +55,9 @@ public RuntimeScriptFieldMapperTests() {
5855 }
5956
6057 @ Override
61- protected Collection <Class <? extends Plugin >> getPlugins () {
62- return pluginList (InternalSettingsPlugin .class , RuntimeFields .class , TestScriptPlugin .class );
58+ protected void minimalMapping (XContentBuilder b ) throws IOException {
59+ b .field ("type" , "runtime_script" ).field ("runtime_type" , "keyword" );
60+ b .startObject ("script" ).field ("source" , "dummy_source" ).field ("lang" , "test" ).endObject ();
6361 }
6462
6563 public void testRuntimeTypeIsRequired () throws Exception {
@@ -75,7 +73,7 @@ public void testRuntimeTypeIsRequired() throws Exception {
7573 .endObject ()
7674 .endObject ();
7775
78- MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createIndex ( "test" , Settings . EMPTY , mapping ));
76+ MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createMapperService ( mapping ));
7977 assertEquals ("Failed to parse mapping: runtime_type must be specified for runtime_script field [my_field]" , exception .getMessage ());
8078 }
8179
@@ -92,7 +90,7 @@ public void testScriptIsRequired() throws Exception {
9290 .endObject ()
9391 .endObject ();
9492
95- MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createIndex ( "test" , Settings . EMPTY , mapping ));
93+ MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createMapperService ( mapping ));
9694 assertEquals ("Failed to parse mapping: script must be specified for runtime_script field [my_field]" , exception .getMessage ());
9795 }
9896
@@ -110,7 +108,7 @@ public void testCopyToIsNotSupported() throws IOException {
110108 .endObject ()
111109 .endObject ()
112110 .endObject ();
113- MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createIndex ( "test" , Settings . EMPTY , mapping ));
111+ MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createMapperService ( mapping ));
114112 assertEquals ("Failed to parse mapping: runtime_script field does not support [copy_to]" , exception .getMessage ());
115113 }
116114
@@ -132,7 +130,7 @@ public void testMultiFieldsIsNotSupported() throws IOException {
132130 .endObject ()
133131 .endObject ()
134132 .endObject ();
135- MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createIndex ( "test" , Settings . EMPTY , mapping ));
133+ MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createMapperService ( mapping ));
136134 assertEquals ("Failed to parse mapping: runtime_script field does not support [fields]" , exception .getMessage ());
137135 }
138136
@@ -151,77 +149,74 @@ public void testStoredScriptsAreNotSupported() throws Exception {
151149 .endObject ()
152150 .endObject ()
153151 .endObject ();
154- MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createIndex ( "test" , Settings . EMPTY , mapping ));
152+ MapperParsingException exception = expectThrows (MapperParsingException .class , () -> createMapperService ( mapping ));
155153 assertEquals (
156154 "Failed to parse mapping: stored scripts specified but not supported for runtime_script field [my_field]" ,
157155 exception .getMessage ()
158156 );
159157 }
160158
161159 public void testUnsupportedRuntimeType () {
162- MapperParsingException exc = expectThrows (
163- MapperParsingException .class ,
164- () -> createIndex ("test" , Settings .EMPTY , mapping ("unsupported" ))
165- );
160+ MapperParsingException exc = expectThrows (MapperParsingException .class , () -> createMapperService (mapping ("unsupported" )));
166161 assertEquals (
167162 "Failed to parse mapping: runtime_type [unsupported] not supported for runtime_script field [field]" ,
168163 exc .getMessage ()
169164 );
170165 }
171166
172167 public void testBoolean () throws IOException {
173- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("boolean" )). mapperService ( );
168+ MapperService mapperService = createMapperService ( mapping ("boolean" ));
174169 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
175170 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
176171 assertEquals (Strings .toString (mapping ("boolean" )), Strings .toString (mapperService .documentMapper ()));
177172 }
178173
179174 public void testDouble () throws IOException {
180- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("double" )). mapperService ( );
175+ MapperService mapperService = createMapperService ( mapping ("double" ));
181176 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
182177 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
183178 assertEquals (Strings .toString (mapping ("double" )), Strings .toString (mapperService .documentMapper ()));
184179 }
185180
186181 public void testIp () throws IOException {
187- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("ip" )). mapperService ( );
182+ MapperService mapperService = createMapperService ( mapping ("ip" ));
188183 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
189184 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
190185 assertEquals (Strings .toString (mapping ("ip" )), Strings .toString (mapperService .documentMapper ()));
191186 }
192187
193188 public void testKeyword () throws IOException {
194- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("keyword" )). mapperService ( );
189+ MapperService mapperService = createMapperService ( mapping ("keyword" ));
195190 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
196191 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
197192 assertEquals (Strings .toString (mapping ("keyword" )), Strings .toString (mapperService .documentMapper ()));
198193 }
199194
200195 public void testLong () throws IOException {
201- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("long" )). mapperService ( );
196+ MapperService mapperService = createMapperService ( mapping ("long" ));
202197 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
203198 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
204199 assertEquals (Strings .toString (mapping ("long" )), Strings .toString (mapperService .documentMapper ()));
205200 }
206201
207202 public void testDate () throws IOException {
208- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping ("date" )). mapperService ( );
203+ MapperService mapperService = createMapperService ( mapping ("date" ));
209204 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
210205 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
211206 assertEquals (Strings .toString (mapping ("date" )), Strings .toString (mapperService .documentMapper ()));
212207 }
213208
214209 public void testDateWithFormat () throws IOException {
215210 CheckedSupplier <XContentBuilder , IOException > mapping = () -> mapping ("date" , b -> b .field ("format" , "yyyy-MM-dd" ));
216- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping .get ()). mapperService ( );
211+ MapperService mapperService = createMapperService ( mapping .get ());
217212 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
218213 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
219214 assertEquals (Strings .toString (mapping .get ()), Strings .toString (mapperService .documentMapper ()));
220215 }
221216
222217 public void testDateWithLocale () throws IOException {
223218 CheckedSupplier <XContentBuilder , IOException > mapping = () -> mapping ("date" , b -> b .field ("locale" , "en_GB" ));
224- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping .get ()). mapperService ( );
219+ MapperService mapperService = createMapperService ( mapping .get ());
225220 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
226221 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
227222 assertEquals (Strings .toString (mapping .get ()), Strings .toString (mapperService .documentMapper ()));
@@ -232,7 +227,7 @@ public void testDateWithLocaleAndFormat() throws IOException {
232227 "date" ,
233228 b -> b .field ("format" , "yyyy-MM-dd" ).field ("locale" , "en_GB" )
234229 );
235- MapperService mapperService = createIndex ( "test" , Settings . EMPTY , mapping .get ()). mapperService ( );
230+ MapperService mapperService = createMapperService ( mapping .get ());
236231 FieldMapper mapper = (FieldMapper ) mapperService .documentMapper ().mappers ().getMapper ("field" );
237232 assertThat (mapper , instanceOf (RuntimeScriptFieldMapper .class ));
238233 assertEquals (Strings .toString (mapping .get ()), Strings .toString (mapperService .documentMapper ()));
@@ -242,7 +237,7 @@ public void testNonDateWithFormat() throws IOException {
242237 String runtimeType = randomValueOtherThan ("date" , () -> randomFrom (runtimeTypes ));
243238 Exception e = expectThrows (
244239 MapperParsingException .class ,
245- () -> createIndex ( "test" , Settings . EMPTY , mapping (runtimeType , b -> b .field ("format" , "yyyy-MM-dd" )))
240+ () -> createMapperService ( mapping (runtimeType , b -> b .field ("format" , "yyyy-MM-dd" )))
246241 );
247242 assertThat (e .getMessage (), equalTo ("Failed to parse mapping: format can not be specified for runtime_type [" + runtimeType + "]" ));
248243 }
@@ -251,16 +246,15 @@ public void testNonDateWithLocale() throws IOException {
251246 String runtimeType = randomValueOtherThan ("date" , () -> randomFrom (runtimeTypes ));
252247 Exception e = expectThrows (
253248 MapperParsingException .class ,
254- () -> createIndex ( "test" , Settings . EMPTY , mapping (runtimeType , b -> b .field ("locale" , "en_GB" )))
249+ () -> createMapperService ( mapping (runtimeType , b -> b .field ("locale" , "en_GB" )))
255250 );
256251 assertThat (e .getMessage (), equalTo ("Failed to parse mapping: locale can not be specified for runtime_type [" + runtimeType + "]" ));
257252 }
258253
259254 public void testFieldCaps () throws Exception {
260255 for (String runtimeType : runtimeTypes ) {
261- String scriptIndex = "test_" + runtimeType + "_script" ;
262- String concreteIndex = "test_" + runtimeType + "_concrete" ;
263- createIndex (scriptIndex , Settings .EMPTY , mapping (runtimeType ));
256+ MapperService scriptIndexMapping = createMapperService (mapping (runtimeType ));
257+ MapperService concreteIndexMapping ;
264258 {
265259 XContentBuilder mapping = XContentFactory .jsonBuilder ()
266260 .startObject ()
@@ -272,19 +266,13 @@ public void testFieldCaps() throws Exception {
272266 .endObject ()
273267 .endObject ()
274268 .endObject ();
275- createIndex ( concreteIndex , Settings . EMPTY , mapping );
269+ concreteIndexMapping = createMapperService ( mapping );
276270 }
277- FieldCapabilitiesResponse response = client ().prepareFieldCaps ("test_" + runtimeType + "_*" ).setFields ("field" ).get ();
278- assertThat (response .getIndices (), arrayContainingInAnyOrder (scriptIndex , concreteIndex ));
279- Map <String , FieldCapabilities > field = response .getField ("field" );
280- assertEquals (1 , field .size ());
281- FieldCapabilities fieldCapabilities = field .get (runtimeType );
282- assertTrue (fieldCapabilities .isSearchable ());
283- assertTrue (fieldCapabilities .isAggregatable ());
284- assertEquals (runtimeType , fieldCapabilities .getType ());
285- assertNull (fieldCapabilities .nonAggregatableIndices ());
286- assertNull (fieldCapabilities .nonSearchableIndices ());
287- assertEquals ("field" , fieldCapabilities .getName ());
271+ MappedFieldType scriptFieldType = scriptIndexMapping .fieldType ("field" );
272+ MappedFieldType concreteIndexType = concreteIndexMapping .fieldType ("field" );
273+ assertEquals (concreteIndexType .familyTypeName (), scriptFieldType .familyTypeName ());
274+ assertEquals (concreteIndexType .isSearchable (), scriptFieldType .isSearchable ());
275+ assertEquals (concreteIndexType .isAggregatable (), scriptFieldType .isAggregatable ());
288276 }
289277 }
290278
@@ -318,7 +306,12 @@ private XContentBuilder mapping(String type, CheckedConsumer<XContentBuilder, IO
318306 return mapping .endObject ();
319307 }
320308
321- public static class TestScriptPlugin extends Plugin implements ScriptPlugin {
309+ @ Override
310+ protected Collection <? extends Plugin > getPlugins () {
311+ return List .of (new RuntimeFields (), new TestScriptPlugin ());
312+ }
313+
314+ private class TestScriptPlugin extends Plugin implements ScriptPlugin {
322315 @ Override
323316 public ScriptEngine getScriptEngine (Settings settings , Collection <ScriptContext <?>> contexts ) {
324317 return new TestScriptEngine () {
@@ -342,15 +335,15 @@ protected Object buildScriptFactory(ScriptContext<?> context) {
342335 if (context == StringScriptFieldScript .CONTEXT ) {
343336 return StringScriptFieldScriptTests .DUMMY ;
344337 }
345- throw new IllegalArgumentException ("No test script for [ " + context + "]" );
346- }
338+ throw new IllegalArgumentException ("Unsupported context: " + context );
339+ };
347340
348- @ Override
349341 public Set <ScriptContext <?>> getSupportedContexts () {
350342 return Set .of (
351343 BooleanScriptFieldScript .CONTEXT ,
352344 DateScriptFieldScript .CONTEXT ,
353345 DoubleScriptFieldScript .CONTEXT ,
346+ IpScriptFieldScript .CONTEXT ,
354347 StringScriptFieldScript .CONTEXT ,
355348 LongScriptFieldScript .CONTEXT
356349 );
0 commit comments