diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java index a5202c607a1..32b5d7390d2 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java @@ -88,6 +88,14 @@ public class TopHitsBuilder extends AbstractAggregationBuilder { return this; } + /** + * Adds a field to loaded and returned. + */ + public TopHitsBuilder addField(String field) { + sourceBuilder().field(field); + return this; + } + /** * Sets no fields to be loaded, resulting in only id and type to be returned per field. */ diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsParser.java index 206587ac6a4..50b3482f568 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsParser.java @@ -25,6 +25,7 @@ import org.elasticsearch.search.SearchParseException; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.fetch.FetchPhase; +import org.elasticsearch.search.fetch.FieldsParseElement; import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsParseElement; import org.elasticsearch.search.fetch.script.ScriptFieldsParseElement; import org.elasticsearch.search.fetch.source.FetchSourceParseElement; @@ -46,15 +47,19 @@ public class TopHitsParser implements Aggregator.Parser { private final HighlighterParseElement highlighterParseElement; private final FieldDataFieldsParseElement fieldDataFieldsParseElement; private final ScriptFieldsParseElement scriptFieldsParseElement; + private final FieldsParseElement fieldsParseElement; @Inject - public TopHitsParser(FetchPhase fetchPhase, SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, HighlighterParseElement highlighterParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement, ScriptFieldsParseElement scriptFieldsParseElement) { + public TopHitsParser(FetchPhase fetchPhase, SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, + HighlighterParseElement highlighterParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement, + ScriptFieldsParseElement scriptFieldsParseElement, FieldsParseElement fieldsParseElement) { this.fetchPhase = fetchPhase; this.sortParseElement = sortParseElement; this.sourceParseElement = sourceParseElement; this.highlighterParseElement = highlighterParseElement; this.fieldDataFieldsParseElement = fieldDataFieldsParseElement; this.scriptFieldsParseElement = scriptFieldsParseElement; + this.fieldsParseElement = fieldsParseElement; } @Override @@ -75,6 +80,8 @@ public class TopHitsParser implements Aggregator.Parser { sortParseElement.parse(parser, subSearchContext); } else if ("_source".equals(currentFieldName)) { sourceParseElement.parse(parser, subSearchContext); + } else if ("fields".equals(currentFieldName)) { + fieldsParseElement.parse(parser, subSearchContext); } else if (token.isValue()) { switch (currentFieldName) { case "from": diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java index 985e040a8a0..f9c8c53bed3 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java @@ -533,6 +533,7 @@ public class TopHitsIT extends ESIntegTestCase { .highlighter(new HighlightBuilder().field("text")) .setExplain(true) .addFieldDataField("field1") + .addField("text") .addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap())) .setFetchSource("text", null) .setVersion(true) @@ -568,6 +569,9 @@ public class TopHitsIT extends ESIntegTestCase { SearchHitField field = hit.field("field1"); assertThat(field.getValue().toString(), equalTo("5")); + field = hit.field("text"); + assertThat(field.getValue().toString(), equalTo("some text to entertain")); + field = hit.field("script"); assertThat(field.getValue().toString(), equalTo("5"));