|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
package org.elasticsearch.index.mapper;
|
|
|
|
|
|
+import org.elasticsearch.common.Explicit;
|
|
|
import org.elasticsearch.index.mapper.flattened.FlattenedFieldMapper;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.hamcrest.Matchers;
|
|
@@ -16,6 +17,7 @@ import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import static java.util.Collections.emptyList;
|
|
@@ -30,7 +32,7 @@ import static org.hamcrest.Matchers.hasSize;
|
|
|
public class FieldTypeLookupTests extends ESTestCase {
|
|
|
|
|
|
public void testEmpty() {
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(emptyList(), emptyList());
|
|
|
assertNull(lookup.get("foo"));
|
|
|
Collection<String> names = lookup.getMatchingFieldNames("foo");
|
|
|
assertNotNull(names);
|
|
@@ -39,7 +41,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
|
|
|
public void testAddNewField() {
|
|
|
MockFieldMapper f = new MockFieldMapper("foo");
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(Collections.singletonList(f), emptyList(), Collections.emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(Collections.singletonList(f), emptyList());
|
|
|
assertNull(lookup.get("bar"));
|
|
|
assertEquals(f.fieldType(), lookup.get("foo"));
|
|
|
}
|
|
@@ -48,11 +50,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
MockFieldMapper field = new MockFieldMapper("foo");
|
|
|
FieldAliasMapper alias = new FieldAliasMapper("alias", "alias", "foo");
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
- Collections.singletonList(field),
|
|
|
- Collections.singletonList(alias),
|
|
|
- Collections.emptyList()
|
|
|
- );
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(Collections.singletonList(field), Collections.singletonList(alias));
|
|
|
|
|
|
MappedFieldType aliasType = lookup.get("alias");
|
|
|
assertEquals(field.fieldType(), aliasType);
|
|
@@ -79,6 +77,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
List.of(field1, field2, field3, flattened),
|
|
|
List.of(alias1, alias2),
|
|
|
+ List.of(),
|
|
|
List.of(runtimeField, multi)
|
|
|
);
|
|
|
|
|
@@ -124,7 +123,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
// Adding a subfield that is not multi-field
|
|
|
MockFieldMapper subfield = new MockFieldMapper.Builder("field.subfield4").build(MapperBuilderContext.root(false, false));
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(List.of(field, subfield), emptyList(), emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(List.of(field, subfield), emptyList());
|
|
|
|
|
|
assertEquals(Set.of("field"), lookup.sourcePaths("field"));
|
|
|
assertEquals(Set.of("field"), lookup.sourcePaths("field.subfield1"));
|
|
@@ -148,11 +147,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
.copyTo("field.nested")
|
|
|
.build(MapperBuilderContext.root(false, false));
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
- Arrays.asList(field, nestedField, otherField, otherNestedField),
|
|
|
- emptyList(),
|
|
|
- emptyList()
|
|
|
- );
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(Arrays.asList(field, nestedField, otherField, otherNestedField), emptyList());
|
|
|
|
|
|
assertEquals(Set.of("other_field", "other_field.nested", "field"), lookup.sourcePaths("field"));
|
|
|
assertEquals(Set.of("other_field", "other_field.nested", "field"), lookup.sourcePaths("field.subfield1"));
|
|
@@ -172,7 +167,12 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
)
|
|
|
);
|
|
|
|
|
|
- FieldTypeLookup fieldTypeLookup = new FieldTypeLookup(List.of(concrete), emptyList(), List.of(runtime, runtimeLong, multi));
|
|
|
+ FieldTypeLookup fieldTypeLookup = new FieldTypeLookup(
|
|
|
+ List.of(concrete),
|
|
|
+ emptyList(),
|
|
|
+ emptyList(),
|
|
|
+ List.of(runtime, runtimeLong, multi)
|
|
|
+ );
|
|
|
assertThat(fieldTypeLookup.get("concrete"), instanceOf(MockFieldMapper.FakeFieldType.class));
|
|
|
assertThat(fieldTypeLookup.get("string"), instanceOf(TestRuntimeField.TestRuntimeFieldType.class));
|
|
|
assertThat(fieldTypeLookup.get("string").typeName(), equalTo("type"));
|
|
@@ -202,6 +202,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
FieldTypeLookup fieldTypeLookup = new FieldTypeLookup(
|
|
|
List.of(field, concrete, subfield, flattened),
|
|
|
emptyList(),
|
|
|
+ emptyList(),
|
|
|
List.of(fieldOverride, runtime, subfieldOverride, flattenedRuntime)
|
|
|
);
|
|
|
assertThat(fieldTypeLookup.get("field"), instanceOf(TestRuntimeField.TestRuntimeFieldType.class));
|
|
@@ -223,7 +224,12 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
TestRuntimeField field2 = new TestRuntimeField("field2", "type");
|
|
|
TestRuntimeField subfield = new TestRuntimeField("object.subfield", "type");
|
|
|
|
|
|
- FieldTypeLookup fieldTypeLookup = new FieldTypeLookup(List.of(field1, concrete), emptyList(), List.of(field2, subfield));
|
|
|
+ FieldTypeLookup fieldTypeLookup = new FieldTypeLookup(
|
|
|
+ List.of(field1, concrete),
|
|
|
+ emptyList(),
|
|
|
+ emptyList(),
|
|
|
+ List.of(field2, subfield)
|
|
|
+ );
|
|
|
{
|
|
|
Set<String> sourcePaths = fieldTypeLookup.sourcePaths("field1");
|
|
|
assertEquals(1, sourcePaths.size());
|
|
@@ -245,7 +251,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
String fieldName = "object1.object2.field";
|
|
|
FlattenedFieldMapper mapper = createFlattenedMapper(fieldName);
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(singletonList(mapper), emptyList(), emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(singletonList(mapper), emptyList());
|
|
|
assertEquals(mapper.fieldType(), lookup.get(fieldName));
|
|
|
|
|
|
String objectKey = "key1.key2";
|
|
@@ -271,7 +277,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
String aliasName = "alias";
|
|
|
FieldAliasMapper alias = new FieldAliasMapper(aliasName, aliasName, fieldName);
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(singletonList(mapper), singletonList(alias), emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(singletonList(mapper), singletonList(alias), emptyList(), emptyList());
|
|
|
assertEquals(mapper.fieldType(), lookup.get(aliasName));
|
|
|
|
|
|
String objectKey = "key1.key2";
|
|
@@ -293,35 +299,31 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
FlattenedFieldMapper mapper2 = createFlattenedMapper(field2);
|
|
|
FlattenedFieldMapper mapper3 = createFlattenedMapper(field3);
|
|
|
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(Arrays.asList(mapper1, mapper2), emptyList(), emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(Arrays.asList(mapper1, mapper2), emptyList());
|
|
|
assertNotNull(lookup.get(field1 + ".some.key"));
|
|
|
assertNotNull(lookup.get(field2 + ".some.key"));
|
|
|
|
|
|
- lookup = new FieldTypeLookup(Arrays.asList(mapper1, mapper2, mapper3), emptyList(), emptyList());
|
|
|
+ lookup = new FieldTypeLookup(Arrays.asList(mapper1, mapper2, mapper3), emptyList());
|
|
|
assertNotNull(lookup.get(field1 + ".some.key"));
|
|
|
assertNotNull(lookup.get(field2 + ".some.key"));
|
|
|
assertNotNull(lookup.get(field3 + ".some.key"));
|
|
|
}
|
|
|
|
|
|
public void testUnmappedLookupWithDots() {
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(emptyList(), emptyList(), emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(emptyList(), emptyList());
|
|
|
assertNull(lookup.get("object.child"));
|
|
|
}
|
|
|
|
|
|
public void testMaxDynamicKeyDepth() {
|
|
|
{
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(emptyList(), emptyList());
|
|
|
assertEquals(0, lookup.getMaxParentPathDots());
|
|
|
}
|
|
|
|
|
|
// Add a flattened object field.
|
|
|
{
|
|
|
String name = "object1.object2.field";
|
|
|
- FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
- Collections.singletonList(createFlattenedMapper(name)),
|
|
|
- Collections.emptyList(),
|
|
|
- Collections.emptyList()
|
|
|
- );
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(Collections.singletonList(createFlattenedMapper(name)), emptyList());
|
|
|
assertEquals(2, lookup.getMaxParentPathDots());
|
|
|
}
|
|
|
|
|
@@ -330,8 +332,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
String name = "object1.object2.field";
|
|
|
FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
Collections.singletonList(createFlattenedMapper(name)),
|
|
|
- Collections.singletonList(new FieldAliasMapper("alias", "alias", "object1.object2.field")),
|
|
|
- Collections.emptyList()
|
|
|
+ Collections.singletonList(new FieldAliasMapper("alias", "alias", "object1.object2.field"))
|
|
|
);
|
|
|
assertEquals(2, lookup.getMaxParentPathDots());
|
|
|
}
|
|
@@ -341,8 +342,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
String name = "object1.object2.field";
|
|
|
FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
Collections.singletonList(createFlattenedMapper(name)),
|
|
|
- Collections.singletonList(new FieldAliasMapper("alias", "object1.object2.object3.alias", "object1.object2.field")),
|
|
|
- Collections.emptyList()
|
|
|
+ Collections.singletonList(new FieldAliasMapper("alias", "object1.object2.object3.alias", "object1.object2.field"))
|
|
|
);
|
|
|
assertEquals(2, lookup.getMaxParentPathDots());
|
|
|
}
|
|
@@ -353,8 +353,9 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
IllegalArgumentException iae = expectThrows(
|
|
|
IllegalArgumentException.class,
|
|
|
() -> new FieldTypeLookup(
|
|
|
- Collections.emptySet(),
|
|
|
- Collections.emptySet(),
|
|
|
+ emptyList(),
|
|
|
+ emptyList(),
|
|
|
+ emptyList(),
|
|
|
List.of(new TestRuntimeField("field", "type"), new TestRuntimeField("field", "long"))
|
|
|
)
|
|
|
);
|
|
@@ -368,7 +369,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
TestRuntimeField runtime = new TestRuntimeField("multi.first", "runtime");
|
|
|
IllegalArgumentException iae = expectThrows(
|
|
|
IllegalArgumentException.class,
|
|
|
- () -> new FieldTypeLookup(Collections.emptySet(), Collections.emptySet(), List.of(multi, runtime))
|
|
|
+ () -> new FieldTypeLookup(emptyList(), emptyList(), emptyList(), List.of(multi, runtime))
|
|
|
);
|
|
|
assertEquals(iae.getMessage(), "Found two runtime fields with same name [multi.first]");
|
|
|
}
|
|
@@ -383,7 +384,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
|
|
|
IllegalArgumentException iae = expectThrows(
|
|
|
IllegalArgumentException.class,
|
|
|
- () -> new FieldTypeLookup(Collections.emptySet(), Collections.emptySet(), List.of(multi))
|
|
|
+ () -> new FieldTypeLookup(emptyList(), emptyList(), emptyList(), List.of(multi))
|
|
|
);
|
|
|
assertEquals(iae.getMessage(), "Found two runtime fields with same name [multi]");
|
|
|
}
|
|
@@ -401,7 +402,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
);
|
|
|
IllegalStateException ise = expectThrows(
|
|
|
IllegalStateException.class,
|
|
|
- () -> new FieldTypeLookup(Collections.emptySet(), Collections.emptySet(), Collections.singletonList(multi))
|
|
|
+ () -> new FieldTypeLookup(emptyList(), emptyList(), emptyList(), Collections.singletonList(multi))
|
|
|
);
|
|
|
assertEquals("Found sub-fields with name not belonging to the parent field they are part of [first, second]", ise.getMessage());
|
|
|
}
|
|
@@ -415,7 +416,7 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
);
|
|
|
IllegalStateException ise = expectThrows(
|
|
|
IllegalStateException.class,
|
|
|
- () -> new FieldTypeLookup(Collections.emptySet(), Collections.emptySet(), Collections.singletonList(multi))
|
|
|
+ () -> new FieldTypeLookup(emptyList(), emptyList(), emptyList(), Collections.singletonList(multi))
|
|
|
);
|
|
|
assertEquals("Found sub-fields with name not belonging to the parent field they are part of [multi.]", ise.getMessage());
|
|
|
}
|
|
@@ -424,4 +425,88 @@ public class FieldTypeLookupTests extends ESTestCase {
|
|
|
private static FlattenedFieldMapper createFlattenedMapper(String fieldName) {
|
|
|
return new FlattenedFieldMapper.Builder(fieldName).build(MapperBuilderContext.root(false, false));
|
|
|
}
|
|
|
+
|
|
|
+ private PassThroughObjectMapper createPassThroughMapper(String name, Map<String, Mapper> mappers, int priority) {
|
|
|
+ return new PassThroughObjectMapper(
|
|
|
+ name,
|
|
|
+ name,
|
|
|
+ Explicit.EXPLICIT_TRUE,
|
|
|
+ ObjectMapper.Dynamic.FALSE,
|
|
|
+ mappers,
|
|
|
+ Explicit.EXPLICIT_FALSE,
|
|
|
+ priority
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testAddRootAliasesForPassThroughFields() {
|
|
|
+ MockFieldMapper foo = new MockFieldMapper("attributes.foo");
|
|
|
+ MockFieldMapper bar = new MockFieldMapper(
|
|
|
+ new MockFieldMapper.FakeFieldType("attributes.much.more.deeply.nested.bar"),
|
|
|
+ "much.more.deeply.nested.bar"
|
|
|
+ );
|
|
|
+ PassThroughObjectMapper attributes = createPassThroughMapper(
|
|
|
+ "attributes",
|
|
|
+ Map.of("foo", foo, "much.more.deeply.nested.bar", bar),
|
|
|
+ 0
|
|
|
+ );
|
|
|
+
|
|
|
+ MockFieldMapper baz = new MockFieldMapper("resource.attributes.baz");
|
|
|
+ MockFieldMapper bag = new MockFieldMapper(
|
|
|
+ new MockFieldMapper.FakeFieldType("resource.attributes.much.more.deeply.nested.bag"),
|
|
|
+ "much.more.deeply.nested.bag"
|
|
|
+ );
|
|
|
+ PassThroughObjectMapper resourceAttributes = createPassThroughMapper(
|
|
|
+ "resource.attributes",
|
|
|
+ Map.of("baz", baz, "much.more.deeply.nested.bag", bag),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
+ List.of(foo, bar, baz, bag),
|
|
|
+ List.of(),
|
|
|
+ List.of(attributes, resourceAttributes),
|
|
|
+ List.of()
|
|
|
+ );
|
|
|
+ assertEquals(foo.fieldType(), lookup.get("foo"));
|
|
|
+ assertEquals(bar.fieldType(), lookup.get("much.more.deeply.nested.bar"));
|
|
|
+ assertEquals(baz.fieldType(), lookup.get("baz"));
|
|
|
+ assertEquals(bag.fieldType(), lookup.get("much.more.deeply.nested.bag"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNoPassThroughField() {
|
|
|
+ MockFieldMapper field = new MockFieldMapper("labels.foo");
|
|
|
+ PassThroughObjectMapper attributes = createPassThroughMapper("attributes", Map.of(), 0);
|
|
|
+
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(List.of(field), List.of(), List.of(attributes), List.of());
|
|
|
+ assertNull(lookup.get("foo"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testAddRootAliasForConflictingPassThroughFields() {
|
|
|
+ MockFieldMapper attributeField = new MockFieldMapper("attributes.foo");
|
|
|
+ PassThroughObjectMapper attributes = createPassThroughMapper("attributes", Map.of("foo", attributeField), 1);
|
|
|
+
|
|
|
+ MockFieldMapper resourceAttributeField = new MockFieldMapper("resource.attributes.foo");
|
|
|
+ PassThroughObjectMapper resourceAttributes = createPassThroughMapper(
|
|
|
+ "resource.attributes",
|
|
|
+ Map.of("foo", resourceAttributeField),
|
|
|
+ 0
|
|
|
+ );
|
|
|
+
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(
|
|
|
+ List.of(attributeField, resourceAttributeField),
|
|
|
+ List.of(),
|
|
|
+ List.of(attributes, resourceAttributes),
|
|
|
+ List.of()
|
|
|
+ );
|
|
|
+ assertEquals(attributeField.fieldType(), lookup.get("foo"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNoRootAliasForPassThroughFieldOnConflictingField() {
|
|
|
+ MockFieldMapper attributeFoo = new MockFieldMapper("attributes.foo");
|
|
|
+ MockFieldMapper foo = new MockFieldMapper("foo");
|
|
|
+ PassThroughObjectMapper attributes = createPassThroughMapper("attributes", Map.of("foo", attributeFoo), 0);
|
|
|
+
|
|
|
+ FieldTypeLookup lookup = new FieldTypeLookup(List.of(foo, attributeFoo), List.of(), List.of(attributes), List.of());
|
|
|
+ assertEquals(foo.fieldType(), lookup.get("foo"));
|
|
|
+ }
|
|
|
}
|