|
@@ -37,6 +37,7 @@ import org.elasticsearch.search.aggregations.InternalAggregation.Type;
|
|
|
import org.elasticsearch.search.aggregations.support.AggregationContext;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField;
|
|
|
+import org.elasticsearch.search.fetch.StoredFieldsContext;
|
|
|
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
|
|
import org.elasticsearch.search.sort.ScoreSortBuilder;
|
|
@@ -63,7 +64,7 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
private boolean trackScores = false;
|
|
|
private List<SortBuilder<?>> sorts = null;
|
|
|
private HighlightBuilder highlightBuilder;
|
|
|
- private List<String> fieldNames;
|
|
|
+ private StoredFieldsContext storedFieldsContext;
|
|
|
private List<String> fieldDataFields;
|
|
|
private Set<ScriptField> scriptFields;
|
|
|
private FetchSourceContext fetchSourceContext;
|
|
@@ -86,13 +87,7 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
fieldDataFields.add(in.readString());
|
|
|
}
|
|
|
}
|
|
|
- if (in.readBoolean()) {
|
|
|
- int size = in.readVInt();
|
|
|
- fieldNames = new ArrayList<>(size);
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- fieldNames.add(in.readString());
|
|
|
- }
|
|
|
- }
|
|
|
+ storedFieldsContext = in.readOptionalWriteable(StoredFieldsContext::new);
|
|
|
from = in.readVInt();
|
|
|
highlightBuilder = in.readOptionalWriteable(HighlightBuilder::new);
|
|
|
if (in.readBoolean()) {
|
|
@@ -126,14 +121,7 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
out.writeString(fieldName);
|
|
|
}
|
|
|
}
|
|
|
- boolean hasFieldNames = fieldNames != null;
|
|
|
- out.writeBoolean(hasFieldNames);
|
|
|
- if (hasFieldNames) {
|
|
|
- out.writeVInt(fieldNames.size());
|
|
|
- for (String fieldName : fieldNames) {
|
|
|
- out.writeString(fieldName);
|
|
|
- }
|
|
|
- }
|
|
|
+ out.writeOptionalWriteable(storedFieldsContext);
|
|
|
out.writeVInt(from);
|
|
|
out.writeOptionalWriteable(highlightBuilder);
|
|
|
boolean hasScriptFields = scriptFields != null;
|
|
@@ -355,47 +343,34 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Adds a field to load and return (note, it must be stored) as part of
|
|
|
- * the search request. If none are specified, the source of the document
|
|
|
- * will be return.
|
|
|
+ * Adds a stored field to load and return (note, it must be stored) as part of the search request.
|
|
|
+ * To disable the stored fields entirely (source and metadata fields) use {@code storedField("_none_")}.
|
|
|
*/
|
|
|
- public TopHitsAggregationBuilder field(String field) {
|
|
|
- if (field == null) {
|
|
|
- throw new IllegalArgumentException("[field] must not be null: [" + name + "]");
|
|
|
- }
|
|
|
- if (fieldNames == null) {
|
|
|
- fieldNames = new ArrayList<>();
|
|
|
- }
|
|
|
- fieldNames.add(field);
|
|
|
- return this;
|
|
|
+ public TopHitsAggregationBuilder storedField(String field) {
|
|
|
+ return storedFields(Collections.singletonList(field));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the fields to load and return as part of the search request. If
|
|
|
- * none are specified, the source of the document will be returned.
|
|
|
+ * Sets the stored fields to load and return as part of the search request.
|
|
|
+ * To disable the stored fields entirely (source and metadata fields) use {@code storedField("_none_")}.
|
|
|
*/
|
|
|
- public TopHitsAggregationBuilder fields(List<String> fields) {
|
|
|
+ public TopHitsAggregationBuilder storedFields(List<String> fields) {
|
|
|
if (fields == null) {
|
|
|
throw new IllegalArgumentException("[fields] must not be null: [" + name + "]");
|
|
|
}
|
|
|
- this.fieldNames = fields;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sets no fields to be loaded, resulting in only id and type to be
|
|
|
- * returned per field.
|
|
|
- */
|
|
|
- public TopHitsAggregationBuilder noFields() {
|
|
|
- this.fieldNames = Collections.emptyList();
|
|
|
+ if (storedFieldsContext == null) {
|
|
|
+ storedFieldsContext = StoredFieldsContext.fromList(fields);
|
|
|
+ } else {
|
|
|
+ storedFieldsContext.addFieldNames(fields);
|
|
|
+ }
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Gets the fields to load and return as part of the search request.
|
|
|
+ * Gets the stored fields context
|
|
|
*/
|
|
|
- public List<String> fields() {
|
|
|
- return fieldNames;
|
|
|
+ public StoredFieldsContext storedFields() {
|
|
|
+ return storedFieldsContext;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -552,8 +527,9 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
@Override
|
|
|
protected TopHitsAggregatorFactory doBuild(AggregationContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder)
|
|
|
throws IOException {
|
|
|
- return new TopHitsAggregatorFactory(name, type, from, size, explain, version, trackScores, sorts, highlightBuilder, fieldNames,
|
|
|
- fieldDataFields, scriptFields, fetchSourceContext, context, parent, subfactoriesBuilder, metaData);
|
|
|
+ return new TopHitsAggregatorFactory(name, type, from, size, explain, version, trackScores, sorts, highlightBuilder,
|
|
|
+ storedFieldsContext, fieldDataFields, scriptFields, fetchSourceContext, context,
|
|
|
+ parent, subfactoriesBuilder, metaData);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -566,16 +542,8 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
if (fetchSourceContext != null) {
|
|
|
builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), fetchSourceContext);
|
|
|
}
|
|
|
- if (fieldNames != null) {
|
|
|
- if (fieldNames.size() == 1) {
|
|
|
- builder.field(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), fieldNames.get(0));
|
|
|
- } else {
|
|
|
- builder.startArray(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName());
|
|
|
- for (String fieldName : fieldNames) {
|
|
|
- builder.value(fieldName);
|
|
|
- }
|
|
|
- builder.endArray();
|
|
|
- }
|
|
|
+ if (storedFieldsContext != null) {
|
|
|
+ storedFieldsContext.toXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), builder);
|
|
|
}
|
|
|
if (fieldDataFields != null) {
|
|
|
builder.startArray(SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.getPreferredName());
|
|
@@ -630,9 +598,8 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
|
|
factory.fetchSource(FetchSourceContext.parse(context));
|
|
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.STORED_FIELDS_FIELD)) {
|
|
|
- List<String> fieldNames = new ArrayList<>();
|
|
|
- fieldNames.add(parser.text());
|
|
|
- factory.fields(fieldNames);
|
|
|
+ factory.storedFieldsContext =
|
|
|
+ StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
|
|
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
|
|
factory.sort(parser.text());
|
|
|
} else {
|
|
@@ -696,16 +663,8 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
} else if (token == XContentParser.Token.START_ARRAY) {
|
|
|
|
|
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.STORED_FIELDS_FIELD)) {
|
|
|
- List<String> fieldNames = new ArrayList<>();
|
|
|
- while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
|
- if (token == XContentParser.Token.VALUE_STRING) {
|
|
|
- fieldNames.add(parser.text());
|
|
|
- } else {
|
|
|
- throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING
|
|
|
- + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
|
|
- }
|
|
|
- }
|
|
|
- factory.fields(fieldNames);
|
|
|
+ factory.storedFieldsContext =
|
|
|
+ StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
|
|
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.DOCVALUE_FIELDS_FIELD)) {
|
|
|
List<String> fieldDataFields = new ArrayList<>();
|
|
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
@@ -736,8 +695,8 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
|
|
|
@Override
|
|
|
protected int doHashCode() {
|
|
|
- return Objects.hash(explain, fetchSourceContext, fieldDataFields, fieldNames, from, highlightBuilder, scriptFields, size, sorts,
|
|
|
- trackScores, version);
|
|
|
+ return Objects.hash(explain, fetchSourceContext, fieldDataFields, storedFieldsContext, from, highlightBuilder,
|
|
|
+ scriptFields, size, sorts, trackScores, version);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -746,7 +705,7 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
|
|
|
return Objects.equals(explain, other.explain)
|
|
|
&& Objects.equals(fetchSourceContext, other.fetchSourceContext)
|
|
|
&& Objects.equals(fieldDataFields, other.fieldDataFields)
|
|
|
- && Objects.equals(fieldNames, other.fieldNames)
|
|
|
+ && Objects.equals(storedFieldsContext, other.storedFieldsContext)
|
|
|
&& Objects.equals(from, other.from)
|
|
|
&& Objects.equals(highlightBuilder, other.highlightBuilder)
|
|
|
&& Objects.equals(scriptFields, other.scriptFields)
|