From b704f0ed3aace44852a86b99581d1de1203ec195 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Tue, 30 Jun 2020 12:44:41 -0700 Subject: [PATCH] Skip over metadata fields in the field retrieval API. (#58710) This avoids unnecessary lookups, since metadata fields don't have _source values. --- .../fetch/subphase/FieldValueRetriever.java | 3 ++- .../subphase/FieldValueRetrieverTests.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FieldValueRetriever.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FieldValueRetriever.java index 49c2619fc0398..45284c67ec6e7 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FieldValueRetriever.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FieldValueRetriever.java @@ -52,7 +52,7 @@ public static FieldValueRetriever create(MapperService mapperService, Collection concreteFields = mapperService.simpleMatchToFullName(fieldPattern); for (String field : concreteFields) { - if (fieldMappers.getMapper(field) != null) { + if (fieldMappers.getMapper(field) != null && mapperService.isMetadataField(field) == false) { Set sourcePath = mapperService.sourcePath(field); fields.add(new FieldContext(field, sourcePath, format)); } @@ -62,6 +62,7 @@ public static FieldValueRetriever create(MapperService mapperService, return new FieldValueRetriever(fieldMappers, fields); } + private FieldValueRetriever(DocumentFieldMappers fieldMappers, List fieldContexts) { this.fieldMappers = fieldMappers; diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldValueRetrieverTests.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldValueRetrieverTests.java index 8b043fd506f33..199df988fd6b6 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldValueRetrieverTests.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldValueRetrieverTests.java @@ -93,6 +93,29 @@ public void testNonExistentField() throws IOException { assertThat(fields.size(), equalTo(0)); } + public void testMetadataFields() throws IOException { + MapperService mapperService = createMapperService(); + XContentBuilder source = XContentFactory.jsonBuilder().startObject() + .field("field", "value") + .endObject(); + + Map fields = retrieveFields(mapperService, source, "_routing"); + assertTrue(fields.isEmpty()); + } + + public void testRetrieveAllFields() throws IOException { + MapperService mapperService = createMapperService(); + XContentBuilder source = XContentFactory.jsonBuilder().startObject() + .field("field", "value") + .startObject("object") + .field("field", "other-value") + .endObject() + .endObject(); + + Map fields = retrieveFields(mapperService, source, "*"); + assertThat(fields.size(), equalTo(2)); + } + public void testArrayValueMappers() throws IOException { MapperService mapperService = createMapperService();