|
@@ -10,7 +10,6 @@ package org.elasticsearch.search.fetch.subphase;
|
|
|
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetadata;
|
|
|
-import org.elasticsearch.common.Nullable;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.document.DocumentField;
|
|
@@ -29,7 +28,6 @@ import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
import static java.util.Collections.emptyMap;
|
|
|
import static org.elasticsearch.common.xcontent.ObjectPath.eval;
|
|
@@ -51,7 +49,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
List<FieldAndFormat> fieldAndFormats = List.of(
|
|
|
new FieldAndFormat("field", null),
|
|
|
new FieldAndFormat("object.field", null));
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormats, null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormats);
|
|
|
assertThat(fields.size(), equalTo(2));
|
|
|
|
|
|
DocumentField field = fields.get("field");
|
|
@@ -279,8 +277,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
|
|
|
Map<String, DocumentField> fields = fetchFields(mapperService, source, List.of(
|
|
|
new FieldAndFormat("field", null),
|
|
|
- new FieldAndFormat("date_field", "yyyy/MM/dd")),
|
|
|
- null);
|
|
|
+ new FieldAndFormat("date_field", "yyyy/MM/dd")));
|
|
|
assertThat(fields.size(), equalTo(2));
|
|
|
|
|
|
DocumentField field = fields.get("field");
|
|
@@ -290,6 +287,16 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
assertNotNull(dateField);
|
|
|
assertThat(dateField.getValues().size(), equalTo(1));
|
|
|
assertThat(dateField.getValue(), equalTo("1990/12/29"));
|
|
|
+
|
|
|
+ // check that badly formed dates in source are just ignored when fetching
|
|
|
+ source = XContentFactory.jsonBuilder().startObject()
|
|
|
+ .field("field", "value")
|
|
|
+ .array("date_field", "1990-12-29T00:00:00.000Z", "baddate", "1991-12-29T00:00:00.000Z")
|
|
|
+ .endObject();
|
|
|
+ DocumentField dates
|
|
|
+ = fetchFields(mapperService, source, List.of(new FieldAndFormat("date_field", "yyyy/MM/dd"))).get("date_field");
|
|
|
+ assertThat(dates.getValues().size(), equalTo(2));
|
|
|
+ assertThat(dates, containsInAnyOrder(equalTo("1990/12/29"), equalTo("1991/12/29")));
|
|
|
}
|
|
|
|
|
|
public void testIgnoreAbove() throws IOException {
|
|
@@ -451,26 +458,26 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.field("object.b", "bar")
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_f*", null, true), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_f*", null, true));
|
|
|
assertThat(fields.size(), equalTo(3));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("unmapped_f1", "unmapped_f2", "unmapped_f3"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("un*1", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("un*1", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("unmapped_f1"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("*thing*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("*thing*", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("something_else"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("null*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("null*", null, true));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("object.a", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("object.a", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertEquals("foo", fields.get("object.a").getValues().get(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("object.b", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("object.b", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertEquals("bar", fields.get("object.b").getValues().get(0));
|
|
|
}
|
|
@@ -480,7 +487,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
|
|
|
XContentBuilder source = XContentFactory.jsonBuilder().startObject().array("unmapped_field", "foo", "bar").endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field", null, true), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("unmapped_field"));
|
|
|
DocumentField field = fields.get("unmapped_field");
|
|
@@ -503,10 +510,10 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field", null, true), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field", null, true));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f*", null, true));
|
|
|
assertThat(fields.size(), equalTo(2));
|
|
|
assertThat(fields.get("unmapped_field.f1").getValue(), equalTo("a"));
|
|
|
assertThat(fields.get("unmapped_field.f2").getValue(), equalTo("b"));
|
|
@@ -526,19 +533,19 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f1", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f1", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
DocumentField field = fields.get("unmapped_field.f1");
|
|
|
assertThat(field.getValues().size(), equalTo(2));
|
|
|
assertThat(field.getValues(), hasItems("a", "b"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f2", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f2", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
field = fields.get("unmapped_field.f2");
|
|
|
assertThat(field.getValues().size(), equalTo(4));
|
|
|
assertThat(field.getValues(), hasItems(1, 2, 3, 4));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f3", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_field.f3", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
field = fields.get("unmapped_field.f3");
|
|
|
assertThat(field.getValues().size(), equalTo(3));
|
|
@@ -590,7 +597,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false));
|
|
|
assertEquals(2, fields.size());
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("f1", "obj"));
|
|
|
assertEquals("value1", fields.get("f1").getValue());
|
|
@@ -608,7 +615,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
assertEquals("value3b", eval("f3.0", obj1));
|
|
|
assertEquals("value4b", eval("inner_nested.0.f4.0", obj1));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("obj*", null, false), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("obj*", null, false));
|
|
|
assertEquals(1, fields.size());
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("obj"));
|
|
|
obj = fields.get("obj").getValues();
|
|
@@ -625,7 +632,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
assertEquals("value3b", eval("f3.0", obj1));
|
|
|
assertEquals("value4b", eval("inner_nested.0.f4.0", obj1));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("obj*", null, false), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("obj*", null, false));
|
|
|
assertEquals(1, fields.size());
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("obj"));
|
|
|
obj = fields.get("obj").getValues();
|
|
@@ -660,13 +667,13 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.field("obj.innerObj.f4", "unmapped_value_f4")
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false));
|
|
|
|
|
|
// without unmapped fields this should only return "obj.f1"
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("obj.f1"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, true));
|
|
|
assertThat(fields.size(), equalTo(4));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("obj.f1", "obj.f2", "obj.innerObj.f3", "obj.innerObj.f4"));
|
|
|
}
|
|
@@ -697,11 +704,11 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, false));
|
|
|
// without unmapped fields this should return nothing
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("*", null, true));
|
|
|
assertThat(fields.size(), equalTo(2));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("obj", "obj.a"));
|
|
|
|
|
@@ -736,15 +743,15 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.field("f1", "malformed")
|
|
|
.endObject();
|
|
|
|
|
|
- // this should not return a field bc. f1 is in the ignored fields
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("*", null, true)), Set.of("f1"));
|
|
|
+ // this should not return a field bc. f1 is malformed
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("*", null, true)));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
// and this should neither
|
|
|
- fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("*", null, true)), Set.of("f1"));
|
|
|
+ fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("*", null, true)));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("f1", null, true)), Set.of("f1"));
|
|
|
+ fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("f1", null, true)));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
// check this also does not overwrite with arrays
|
|
@@ -752,7 +759,7 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.array("f1", "malformed")
|
|
|
.endObject();
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("f1", null, true)), Set.of("f1"));
|
|
|
+ fields = fetchFields(mapperService, source, List.of(new FieldAndFormat("f1", null, true)));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
}
|
|
|
|
|
@@ -766,24 +773,24 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object", null, true), null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object", null, true));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmap*object", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmap*object", null, true));
|
|
|
assertThat(fields.size(), equalTo(0));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.*", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.*", null, true));
|
|
|
assertThat(fields.size(), equalTo(2));
|
|
|
assertThat(fields.keySet(), containsInAnyOrder("unmapped_object.a", "unmapped_object.b"));
|
|
|
|
|
|
assertThat(fields.get("unmapped_object.a").getValue(), equalTo("foo"));
|
|
|
assertThat(fields.get("unmapped_object.b").getValue(), equalTo("bar"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.a", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.a", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.get("unmapped_object.a").getValue(), equalTo("foo"));
|
|
|
|
|
|
- fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.b", null, true), null);
|
|
|
+ fields = fetchFields(mapperService, source, fieldAndFormatList("unmapped_object.b", null, true));
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.get("unmapped_object.b").getValue(), equalTo("bar"));
|
|
|
}
|
|
@@ -800,14 +807,14 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
|
|
|
List<FieldAndFormat> ff = new ArrayList<>();
|
|
|
ff.add(new FieldAndFormat("date_field", "year", false));
|
|
|
- Map<String, DocumentField> fields = fetchFields(mapperService, source, ff, null);
|
|
|
+ Map<String, DocumentField> fields = fetchFields(mapperService, source, ff);
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.get("date_field").getValues().size(), equalTo(2));
|
|
|
assertThat(fields.get("date_field").getValues().get(0), equalTo("2011"));
|
|
|
assertThat(fields.get("date_field").getValues().get(1), equalTo("2012"));
|
|
|
|
|
|
ff.add(new FieldAndFormat("date_field", "hour", false));
|
|
|
- fields = fetchFields(mapperService, source, ff, null);
|
|
|
+ fields = fetchFields(mapperService, source, ff);
|
|
|
assertThat(fields.size(), equalTo(1));
|
|
|
assertThat(fields.get("date_field").getValues().size(), equalTo(2));
|
|
|
assertThat(fields.get("date_field").getValues().get(0), equalTo("11"));
|
|
@@ -820,21 +827,20 @@ public class FieldFetcherTests extends MapperServiceTestCase {
|
|
|
|
|
|
private Map<String, DocumentField> fetchFields(MapperService mapperService, XContentBuilder source, String fieldPattern)
|
|
|
throws IOException {
|
|
|
- return fetchFields(mapperService, source, fieldAndFormatList(fieldPattern, null, false), null);
|
|
|
+ return fetchFields(mapperService, source, fieldAndFormatList(fieldPattern, null, false));
|
|
|
}
|
|
|
|
|
|
private static Map<String, DocumentField> fetchFields(
|
|
|
MapperService mapperService,
|
|
|
XContentBuilder source,
|
|
|
- List<FieldAndFormat> fields,
|
|
|
- @Nullable Set<String> ignoreFields
|
|
|
+ List<FieldAndFormat> fields
|
|
|
) throws IOException {
|
|
|
|
|
|
SourceLookup sourceLookup = new SourceLookup();
|
|
|
sourceLookup.setSource(BytesReference.bytes(source));
|
|
|
|
|
|
FieldFetcher fieldFetcher = FieldFetcher.create(newSearchExecutionContext(mapperService), fields);
|
|
|
- return fieldFetcher.fetch(sourceLookup, ignoreFields != null ? ignoreFields : Collections.emptySet());
|
|
|
+ return fieldFetcher.fetch(sourceLookup);
|
|
|
}
|
|
|
|
|
|
public MapperService createMapperService() throws IOException {
|