66
77package org .elasticsearch .xpack .runtimefields .mapper ;
88
9+ import org .elasticsearch .action .fieldcaps .FieldCapabilities ;
10+ import org .elasticsearch .action .fieldcaps .FieldCapabilitiesResponse ;
911import org .elasticsearch .common .Strings ;
1012import org .elasticsearch .common .settings .Settings ;
1113import org .elasticsearch .common .xcontent .XContentBuilder ;
1214import org .elasticsearch .common .xcontent .XContentFactory ;
1315import org .elasticsearch .index .mapper .FieldMapper ;
16+ import org .elasticsearch .index .mapper .KeywordFieldMapper ;
1417import org .elasticsearch .index .mapper .MapperParsingException ;
1518import org .elasticsearch .index .mapper .MapperService ;
1619import org .elasticsearch .plugins .Plugin ;
2225import org .elasticsearch .xpack .runtimefields .RuntimeFields ;
2326import org .elasticsearch .xpack .runtimefields .StringScriptFieldScript ;
2427
28+ import java .util .Arrays ;
2529import java .util .Collection ;
2630import java .util .Map ;
2731import java .util .Set ;
2832
33+ import static org .hamcrest .Matchers .arrayContainingInAnyOrder ;
2934import static org .hamcrest .Matchers .instanceOf ;
3035
3136public class ScriptFieldMapperTests extends ESSingleNodeTestCase {
3237
33- private static final String [] SUPPORTED_RUNTIME_TYPES = new String [] { "keyword" };
38+ private final String [] runtimeTypes ;
39+
40+ public ScriptFieldMapperTests () {
41+ this .runtimeTypes = ScriptFieldMapper .Builder .FIELD_TYPE_RESOLVER .keySet ().toArray (new String [0 ]);
42+ Arrays .sort (runtimeTypes );
43+ }
3444
3545 @ Override
3646 protected Collection <Class <? extends Plugin >> getPlugins () {
@@ -45,7 +55,7 @@ public void testRuntimeTypeIsRequired() throws Exception {
4555 .startObject ("properties" )
4656 .startObject ("my_field" )
4757 .field ("type" , "script" )
48- .field ("script" , "value ('test')" )
58+ .field ("script" , "keyword ('test')" )
4959 .endObject ()
5060 .endObject ()
5161 .endObject ()
@@ -63,7 +73,7 @@ public void testScriptIsRequired() throws Exception {
6373 .startObject ("properties" )
6474 .startObject ("my_field" )
6575 .field ("type" , "script" )
66- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
76+ .field ("runtime_type" , randomFrom (runtimeTypes ))
6777 .endObject ()
6878 .endObject ()
6979 .endObject ()
@@ -80,7 +90,7 @@ public void testStoredScriptsAreNotSupported() throws Exception {
8090 .startObject ("properties" )
8191 .startObject ("my_field" )
8292 .field ("type" , "script" )
83- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
93+ .field ("runtime_type" , randomFrom (runtimeTypes ))
8494 .startObject ("script" )
8595 .field ("id" , "test" )
8696 .endObject ()
@@ -104,7 +114,7 @@ public void testUnsupportedRuntimeType() throws Exception {
104114 .field ("type" , "script" )
105115 .field ("runtime_type" , "unsupported" )
106116 .startObject ("script" )
107- .field ("source" , "value ('test')" )
117+ .field ("source" , "keyword ('test')" )
108118 .field ("lang" , "test" )
109119 .endObject ()
110120 .endObject ()
@@ -116,16 +126,63 @@ public void testUnsupportedRuntimeType() throws Exception {
116126 assertEquals ("Failed to parse mapping: runtime_type [unsupported] not supported" , exc .getMessage ());
117127 }
118128
129+ public void testFieldCaps () throws Exception {
130+ for (String runtimeType : runtimeTypes ) {
131+ {
132+ XContentBuilder mapping = XContentFactory .jsonBuilder ()
133+ .startObject ()
134+ .startObject ("_doc" )
135+ .startObject ("properties" )
136+ .startObject ("field" )
137+ .field ("type" , "script" )
138+ .field ("runtime_type" , runtimeType )
139+ .startObject ("script" )
140+ .field ("source" , runtimeType + "('test')" )
141+ .field ("lang" , "test" )
142+ .endObject ()
143+ .endObject ()
144+ .endObject ()
145+ .endObject ()
146+ .endObject ();
147+ createIndex ("test_script" , Settings .EMPTY , mapping );
148+ }
149+ {
150+ XContentBuilder mapping = XContentFactory .jsonBuilder ()
151+ .startObject ()
152+ .startObject ("_doc" )
153+ .startObject ("properties" )
154+ .startObject ("field" )
155+ .field ("type" , runtimeType )
156+ .endObject ()
157+ .endObject ()
158+ .endObject ()
159+ .endObject ();
160+ createIndex ("test_concrete" , Settings .EMPTY , mapping );
161+ }
162+ FieldCapabilitiesResponse response = client ().prepareFieldCaps ("test_*" ).setFields ("field" ).get ();
163+ assertThat (response .getIndices (), arrayContainingInAnyOrder ("test_script" , "test_concrete" ));
164+ Map <String , FieldCapabilities > field = response .getField ("field" );
165+ assertEquals (1 , field .size ());
166+ FieldCapabilities fieldCapabilities = field .get (KeywordFieldMapper .CONTENT_TYPE );
167+ assertTrue (fieldCapabilities .isSearchable ());
168+ assertTrue (fieldCapabilities .isAggregatable ());
169+ assertEquals (runtimeType , fieldCapabilities .getType ());
170+ assertNull (fieldCapabilities .nonAggregatableIndices ());
171+ assertNull (fieldCapabilities .nonSearchableIndices ());
172+ assertEquals ("field" , fieldCapabilities .getName ());
173+ }
174+ }
175+
119176 public void testDefaultMapping () throws Exception {
120177 XContentBuilder mapping = XContentFactory .jsonBuilder ()
121178 .startObject ()
122179 .startObject ("_doc" )
123180 .startObject ("properties" )
124181 .startObject ("field" )
125182 .field ("type" , "script" )
126- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
183+ .field ("runtime_type" , randomFrom (runtimeTypes ))
127184 .startObject ("script" )
128- .field ("source" , "value ('test')" )
185+ .field ("source" , "keyword ('test')" )
129186 .field ("lang" , "test" )
130187 .endObject ()
131188 .endObject ()
@@ -155,7 +212,7 @@ public <FactoryType> FactoryType compile(
155212 ScriptContext <FactoryType > context ,
156213 Map <String , String > paramsMap
157214 ) {
158- if ("value ('test')" .equals (code )) {
215+ if ("keyword ('test')" .equals (code )) {
159216 StringScriptFieldScript .Factory factory = (params , lookup ) -> ctx -> new StringScriptFieldScript (
160217 params ,
161218 lookup ,
0 commit comments