6
6
7
7
package org .elasticsearch .xpack .runtimefields .mapper ;
8
8
9
+ import org .elasticsearch .action .fieldcaps .FieldCapabilities ;
10
+ import org .elasticsearch .action .fieldcaps .FieldCapabilitiesResponse ;
9
11
import org .elasticsearch .common .Strings ;
10
12
import org .elasticsearch .common .settings .Settings ;
11
13
import org .elasticsearch .common .xcontent .XContentBuilder ;
12
14
import org .elasticsearch .common .xcontent .XContentFactory ;
13
15
import org .elasticsearch .index .mapper .FieldMapper ;
16
+ import org .elasticsearch .index .mapper .KeywordFieldMapper ;
14
17
import org .elasticsearch .index .mapper .MapperParsingException ;
15
18
import org .elasticsearch .index .mapper .MapperService ;
16
19
import org .elasticsearch .plugins .Plugin ;
22
25
import org .elasticsearch .xpack .runtimefields .RuntimeFields ;
23
26
import org .elasticsearch .xpack .runtimefields .StringScriptFieldScript ;
24
27
28
+ import java .util .Arrays ;
25
29
import java .util .Collection ;
26
30
import java .util .Map ;
27
31
import java .util .Set ;
28
32
33
+ import static org .hamcrest .Matchers .arrayContainingInAnyOrder ;
29
34
import static org .hamcrest .Matchers .instanceOf ;
30
35
31
36
public class ScriptFieldMapperTests extends ESSingleNodeTestCase {
32
37
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
+ }
34
44
35
45
@ Override
36
46
protected Collection <Class <? extends Plugin >> getPlugins () {
@@ -45,7 +55,7 @@ public void testRuntimeTypeIsRequired() throws Exception {
45
55
.startObject ("properties" )
46
56
.startObject ("my_field" )
47
57
.field ("type" , "script" )
48
- .field ("script" , "value ('test')" )
58
+ .field ("script" , "keyword ('test')" )
49
59
.endObject ()
50
60
.endObject ()
51
61
.endObject ()
@@ -63,7 +73,7 @@ public void testScriptIsRequired() throws Exception {
63
73
.startObject ("properties" )
64
74
.startObject ("my_field" )
65
75
.field ("type" , "script" )
66
- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
76
+ .field ("runtime_type" , randomFrom (runtimeTypes ))
67
77
.endObject ()
68
78
.endObject ()
69
79
.endObject ()
@@ -80,7 +90,7 @@ public void testStoredScriptsAreNotSupported() throws Exception {
80
90
.startObject ("properties" )
81
91
.startObject ("my_field" )
82
92
.field ("type" , "script" )
83
- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
93
+ .field ("runtime_type" , randomFrom (runtimeTypes ))
84
94
.startObject ("script" )
85
95
.field ("id" , "test" )
86
96
.endObject ()
@@ -104,7 +114,7 @@ public void testUnsupportedRuntimeType() throws Exception {
104
114
.field ("type" , "script" )
105
115
.field ("runtime_type" , "unsupported" )
106
116
.startObject ("script" )
107
- .field ("source" , "value ('test')" )
117
+ .field ("source" , "keyword ('test')" )
108
118
.field ("lang" , "test" )
109
119
.endObject ()
110
120
.endObject ()
@@ -116,16 +126,63 @@ public void testUnsupportedRuntimeType() throws Exception {
116
126
assertEquals ("Failed to parse mapping: runtime_type [unsupported] not supported" , exc .getMessage ());
117
127
}
118
128
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
+
119
176
public void testDefaultMapping () throws Exception {
120
177
XContentBuilder mapping = XContentFactory .jsonBuilder ()
121
178
.startObject ()
122
179
.startObject ("_doc" )
123
180
.startObject ("properties" )
124
181
.startObject ("field" )
125
182
.field ("type" , "script" )
126
- .field ("runtime_type" , randomFrom (SUPPORTED_RUNTIME_TYPES ))
183
+ .field ("runtime_type" , randomFrom (runtimeTypes ))
127
184
.startObject ("script" )
128
- .field ("source" , "value ('test')" )
185
+ .field ("source" , "keyword ('test')" )
129
186
.field ("lang" , "test" )
130
187
.endObject ()
131
188
.endObject ()
@@ -155,7 +212,7 @@ public <FactoryType> FactoryType compile(
155
212
ScriptContext <FactoryType > context ,
156
213
Map <String , String > paramsMap
157
214
) {
158
- if ("value ('test')" .equals (code )) {
215
+ if ("keyword ('test')" .equals (code )) {
159
216
StringScriptFieldScript .Factory factory = (params , lookup ) -> ctx -> new StringScriptFieldScript (
160
217
params ,
161
218
lookup ,
0 commit comments