Merge pull request #14597 from wbowling/tophitsbuilder-addfield

Put method addField on TopHitsBuilder
This commit is contained in:
Martijn van Groningen 2015-11-09 17:40:13 +07:00
commit 72e9bd8506
3 changed files with 20 additions and 1 deletions

View File

@ -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.
*/

View File

@ -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":

View File

@ -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"));