22
22
import static java .util .Collections .singletonList ;
23
23
import static org .hamcrest .CoreMatchers .equalTo ;
24
24
import static org .hamcrest .CoreMatchers .instanceOf ;
25
+ import static org .hamcrest .CoreMatchers .nullValue ;
25
26
import static org .hamcrest .Matchers .contains ;
26
27
import static org .hamcrest .Matchers .containsInAnyOrder ;
27
28
import static org .hamcrest .Matchers .hasSize ;
@@ -55,6 +56,7 @@ public void testAddFieldAlias() {
55
56
}
56
57
57
58
public void testGetMatchingFieldNames () {
59
+ FlattenedFieldMapper flattened = createFlattenedMapper ("flattened" );
58
60
MockFieldMapper field1 = new MockFieldMapper ("foo" );
59
61
MockFieldMapper field2 = new MockFieldMapper ("bar" );
60
62
MockFieldMapper field3 = new MockFieldMapper ("baz" );
@@ -63,18 +65,24 @@ public void testGetMatchingFieldNames() {
63
65
FieldAliasMapper alias2 = new FieldAliasMapper ("barometer" , "barometer" , "bar" );
64
66
65
67
TestRuntimeField runtimeField = new TestRuntimeField ("baz" , "type" );
68
+ TestRuntimeField multi = new TestRuntimeField ("flat" , "multi" ,
69
+ List .of (new TestRuntimeField .TestRuntimeFieldType ("flat.first" , "first" ),
70
+ new TestRuntimeField .TestRuntimeFieldType ("flat.second" , "second" )));
66
71
67
- FieldTypeLookup lookup = new FieldTypeLookup ("_doc" , List .of (field1 , field2 , field3 ), List .of (alias1 , alias2 ),
68
- List .of (runtimeField ));
69
-
72
+ FieldTypeLookup lookup = new FieldTypeLookup ("_doc" , List .of (field1 , field2 , field3 , flattened ), List .of (alias1 , alias2 ),
73
+ List .of (runtimeField , multi ));
70
74
{
71
75
Collection <String > names = lookup .getMatchingFieldNames ("*" );
72
- assertThat (names , containsInAnyOrder ("foo" , "food" , "bar" , "baz" , "barometer" ));
76
+ assertThat (names , containsInAnyOrder ("foo" , "food" , "bar" , "baz" , "barometer" , "flattened" , "flat.first" , "flat.second" ));
73
77
}
74
78
{
75
79
Collection <String > names = lookup .getMatchingFieldNames ("b*" );
76
80
assertThat (names , containsInAnyOrder ("bar" , "baz" , "barometer" ));
77
81
}
82
+ {
83
+ Collection <String > names = lookup .getMatchingFieldNames ("fl*" );
84
+ assertThat (names , containsInAnyOrder ("flattened" , "flat.first" , "flat.second" ));
85
+ }
78
86
{
79
87
Collection <String > names = lookup .getMatchingFieldNames ("baro.any*" );
80
88
assertThat (names , hasSize (0 ));
@@ -83,6 +91,18 @@ public void testGetMatchingFieldNames() {
83
91
Collection <String > names = lookup .getMatchingFieldNames ("foo*" );
84
92
assertThat (names , containsInAnyOrder ("foo" , "food" ));
85
93
}
94
+ {
95
+ Collection <String > names = lookup .getMatchingFieldNames ("flattened.anything" );
96
+ assertThat (names , containsInAnyOrder ("flattened.anything" ));
97
+ }
98
+ {
99
+ Collection <String > names = lookup .getMatchingFieldNames ("flat.first" );
100
+ assertThat (names , containsInAnyOrder ("flat.first" ));
101
+ }
102
+ {
103
+ Collection <String > names = lookup .getMatchingFieldNames ("flat.second" );
104
+ assertThat (names , containsInAnyOrder ("flat.second" ));
105
+ }
86
106
}
87
107
88
108
public void testSourcePathWithMultiFields () {
@@ -123,28 +143,48 @@ public void testTypeLookup() {
123
143
124
144
public void testRuntimeFieldsLookup () {
125
145
MockFieldMapper concrete = new MockFieldMapper ("concrete" );
126
- TestRuntimeField runtime = new TestRuntimeField ("runtime" , "type" );
146
+ TestRuntimeField runtimeLong = new TestRuntimeField ("multi.outside" , "date" );
147
+ TestRuntimeField runtime = new TestRuntimeField ("string" , "type" );
148
+ TestRuntimeField multi = new TestRuntimeField ("multi" , "multi" , List .of (
149
+ new TestRuntimeField .TestRuntimeFieldType ("multi.string" , "string" ),
150
+ new TestRuntimeField .TestRuntimeFieldType ("multi.long" , "long" )));
127
151
128
- FieldTypeLookup fieldTypeLookup = new FieldTypeLookup ("_doc" , List .of (concrete ), emptyList (), List .of (runtime ));
152
+ FieldTypeLookup fieldTypeLookup = new FieldTypeLookup ("_doc" , List .of (concrete ), emptyList (), List .of (runtime , runtimeLong , multi ));
129
153
assertThat (fieldTypeLookup .get ("concrete" ), instanceOf (MockFieldMapper .FakeFieldType .class ));
130
- assertThat (fieldTypeLookup .get ("runtime" ), instanceOf (TestRuntimeField .class ));
154
+ assertThat (fieldTypeLookup .get ("string" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
155
+ assertThat (fieldTypeLookup .get ("string" ).typeName (), equalTo ("type" ));
156
+ assertThat (fieldTypeLookup .get ("multi" ), nullValue ());
157
+ assertThat (fieldTypeLookup .get ("multi.string" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
158
+ assertThat (fieldTypeLookup .get ("multi.string" ).typeName (), equalTo ("string" ));
159
+ assertThat (fieldTypeLookup .get ("multi.long" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
160
+ assertThat (fieldTypeLookup .get ("multi.long" ).typeName (), equalTo ("long" ));
161
+ assertThat (fieldTypeLookup .get ("multi.outside" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
162
+ assertThat (fieldTypeLookup .get ("multi.outside" ).typeName (), equalTo ("date" ));
163
+ assertThat (fieldTypeLookup .get ("multi.anything" ), nullValue ());
131
164
}
132
165
133
166
public void testRuntimeFieldsOverrideConcreteFields () {
134
167
FlattenedFieldMapper flattened = createFlattenedMapper ("flattened" );
135
168
MockFieldMapper field = new MockFieldMapper ("field" );
136
169
MockFieldMapper subfield = new MockFieldMapper ("object.subfield" );
137
170
MockFieldMapper concrete = new MockFieldMapper ("concrete" );
138
- TestRuntimeField fieldOverride = new TestRuntimeField ("field" , "type" );
139
- TestRuntimeField subfieldOverride = new TestRuntimeField ("object.subfield" , "type" );
171
+ TestRuntimeField fieldOverride = new TestRuntimeField ("field" , "string" );
172
+ TestRuntimeField subfieldOverride = new TestRuntimeField ("object" , "multi" ,
173
+ Collections .singleton (new TestRuntimeField .TestRuntimeFieldType ("object.subfield" , "leaf" )));
140
174
TestRuntimeField runtime = new TestRuntimeField ("runtime" , "type" );
175
+ TestRuntimeField flattenedRuntime = new TestRuntimeField ("flattened.runtime" , "type" );
141
176
142
177
FieldTypeLookup fieldTypeLookup = new FieldTypeLookup ("_doc" , List .of (field , concrete , subfield , flattened ), emptyList (),
143
- List .of (fieldOverride , runtime , subfieldOverride ));
144
- assertThat (fieldTypeLookup .get ("field" ), instanceOf (TestRuntimeField .class ));
145
- assertThat (fieldTypeLookup .get ("object.subfield" ), instanceOf (TestRuntimeField .class ));
178
+ List .of (fieldOverride , runtime , subfieldOverride , flattenedRuntime ));
179
+ assertThat (fieldTypeLookup .get ("field" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
180
+ assertThat (fieldTypeLookup .get ("field" ).typeName (), equalTo ("string" ));
181
+ assertThat (fieldTypeLookup .get ("object.subfield" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
182
+ assertThat (fieldTypeLookup .get ("object.subfield" ).typeName (), equalTo ("leaf" ));
146
183
assertThat (fieldTypeLookup .get ("concrete" ), instanceOf (MockFieldMapper .FakeFieldType .class ));
147
- assertThat (fieldTypeLookup .get ("runtime" ), instanceOf (TestRuntimeField .class ));
184
+ assertThat (fieldTypeLookup .get ("runtime" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
185
+ assertThat (fieldTypeLookup .get ("runtime" ).typeName (), equalTo ("type" ));
186
+ assertThat (fieldTypeLookup .get ("flattened.anything" ), instanceOf (FlattenedFieldMapper .KeyedFlattenedFieldType .class ));
187
+ assertThat (fieldTypeLookup .get ("flattened.runtime" ), instanceOf (TestRuntimeField .TestRuntimeFieldType .class ));
148
188
}
149
189
150
190
public void testRuntimeFieldsSourcePaths () {
@@ -283,6 +323,54 @@ public void testMaxDynamicKeyDepth() {
283
323
}
284
324
}
285
325
326
+ public void testRuntimeFieldNameClashes () {
327
+ {
328
+ IllegalArgumentException iae = expectThrows (IllegalArgumentException .class , () -> new FieldTypeLookup (
329
+ "_doc" , Collections .emptySet (), Collections .emptySet (),
330
+ List .of (new TestRuntimeField ("field" , "type" ), new TestRuntimeField ("field" , "long" ))));
331
+ assertEquals (iae .getMessage (), "Found two runtime fields with same name [field]" );
332
+ }
333
+ {
334
+ TestRuntimeField multi = new TestRuntimeField ("multi" , "multi" ,
335
+ Collections .singleton (new TestRuntimeField .TestRuntimeFieldType ("multi.first" , "leaf" )));
336
+ TestRuntimeField runtime = new TestRuntimeField ("multi.first" , "runtime" );
337
+ IllegalArgumentException iae = expectThrows (IllegalArgumentException .class , () -> new FieldTypeLookup (
338
+ "_doc" , Collections .emptySet (), Collections .emptySet (), List .of (multi , runtime )));
339
+ assertEquals (iae .getMessage (), "Found two runtime fields with same name [multi.first]" );
340
+ }
341
+ {
342
+ TestRuntimeField multi = new TestRuntimeField ("multi" , "multi" ,
343
+ List .of (new TestRuntimeField .TestRuntimeFieldType ("multi" , "leaf" ),
344
+ new TestRuntimeField .TestRuntimeFieldType ("multi" , "leaf" )));
345
+
346
+ IllegalArgumentException iae = expectThrows (IllegalArgumentException .class , () -> new FieldTypeLookup (
347
+ "_doc" , Collections .emptySet (), Collections .emptySet (), List .of (multi )));
348
+ assertEquals (iae .getMessage (), "Found two runtime fields with same name [multi]" );
349
+ }
350
+ }
351
+
352
+ public void testRuntimeFieldNameOutsideContext () {
353
+ {
354
+ TestRuntimeField multi = new TestRuntimeField ("multi" , "multi" ,
355
+ List .of (new TestRuntimeField .TestRuntimeFieldType ("first" , "leaf" ),
356
+ new TestRuntimeField .TestRuntimeFieldType ("second" , "leaf" ),
357
+ new TestRuntimeField .TestRuntimeFieldType ("multi.third" , "leaf" )));
358
+ IllegalStateException ise = expectThrows (IllegalStateException .class , () -> new FieldTypeLookup (
359
+ "_doc" , Collections .emptySet (), Collections .emptySet (), Collections .singletonList (multi )));
360
+ assertEquals ("Found sub-fields with name not belonging to the parent field they are part of [first, second]" ,
361
+ ise .getMessage ());
362
+ }
363
+ {
364
+ TestRuntimeField multi = new TestRuntimeField ("multi" , "multi" ,
365
+ List .of (new TestRuntimeField .TestRuntimeFieldType ("multi." , "leaf" ),
366
+ new TestRuntimeField .TestRuntimeFieldType ("multi.f" , "leaf" )));
367
+ IllegalStateException ise = expectThrows (IllegalStateException .class , () -> new FieldTypeLookup (
368
+ "_doc" , Collections .emptySet (), Collections .emptySet (), Collections .singletonList (multi )));
369
+ assertEquals ("Found sub-fields with name not belonging to the parent field they are part of [multi.]" ,
370
+ ise .getMessage ());
371
+ }
372
+ }
373
+
286
374
private static FlattenedFieldMapper createFlattenedMapper (String fieldName ) {
287
375
return new FlattenedFieldMapper .Builder (fieldName ).build (new ContentPath ());
288
376
}
0 commit comments