SortBuilder#toXContent should render full object

This commit is contained in:
Christoph Büscher 2016-03-23 11:31:45 +01:00
parent b614e3e075
commit f8e7462725
10 changed files with 18 additions and 24 deletions

View File

@ -21,7 +21,6 @@ package org.elasticsearch.action.percolate;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
@ -127,7 +126,7 @@ public class PercolateRequestBuilder extends ActionRequestBuilder<PercolateReque
/**
* Delegates to {@link PercolateSourceBuilder#addSort(SortBuilder)}
*/
public PercolateRequestBuilder addSort(SortBuilder sort) {
public PercolateRequestBuilder addSort(SortBuilder<?> sort) {
sourceBuilder().addSort(sort);
return this;
}

View File

@ -48,13 +48,13 @@ import java.util.Map;
public class PercolateSourceBuilder extends ToXContentToBytes {
private DocBuilder docBuilder;
private QueryBuilder queryBuilder;
private QueryBuilder<?> queryBuilder;
private Integer size;
private List<SortBuilder> sorts;
private List<SortBuilder<?>> sorts;
private Boolean trackScores;
private HighlightBuilder highlightBuilder;
private List<AggregatorBuilder<?>> aggregationBuilders;
private List<PipelineAggregatorBuilder> pipelineAggregationBuilders;
private List<PipelineAggregatorBuilder<?>> pipelineAggregationBuilders;
/**
* Sets the document to run the percolate queries against.
@ -68,7 +68,7 @@ public class PercolateSourceBuilder extends ToXContentToBytes {
* Sets a query to reduce the number of percolate queries to be evaluated and score the queries that match based
* on this query.
*/
public PercolateSourceBuilder setQueryBuilder(QueryBuilder queryBuilder) {
public PercolateSourceBuilder setQueryBuilder(QueryBuilder<?> queryBuilder) {
this.queryBuilder = queryBuilder;
return this;
}
@ -98,7 +98,7 @@ public class PercolateSourceBuilder extends ToXContentToBytes {
*
* By default the matching percolator queries are returned in an undefined order.
*/
public PercolateSourceBuilder addSort(SortBuilder sort) {
public PercolateSourceBuilder addSort(SortBuilder<?> sort) {
if (sorts == null) {
sorts = new ArrayList<>();
}
@ -137,7 +137,7 @@ public class PercolateSourceBuilder extends ToXContentToBytes {
/**
* Add an aggregation definition.
*/
public PercolateSourceBuilder addAggregation(PipelineAggregatorBuilder aggregationBuilder) {
public PercolateSourceBuilder addAggregation(PipelineAggregatorBuilder<?> aggregationBuilder) {
if (pipelineAggregationBuilders == null) {
pipelineAggregationBuilders = new ArrayList<>();
}
@ -160,10 +160,8 @@ public class PercolateSourceBuilder extends ToXContentToBytes {
}
if (sorts != null) {
builder.startArray("sort");
for (SortBuilder sort : sorts) {
builder.startObject();
for (SortBuilder<?> sort : sorts) {
sort.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}
@ -182,7 +180,7 @@ public class PercolateSourceBuilder extends ToXContentToBytes {
}
}
if (pipelineAggregationBuilders != null) {
for (PipelineAggregatorBuilder aggregation : pipelineAggregationBuilders) {
for (PipelineAggregatorBuilder<?> aggregation : pipelineAggregationBuilders) {
aggregation.toXContent(builder, params);
}
}

View File

@ -502,9 +502,7 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
if (sorts != null) {
builder.startArray(SearchSourceBuilder.SORT_FIELD.getPreferredName());
for (SortBuilder<?> sort : sorts) {
builder.startObject();
sort.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}

View File

@ -1051,9 +1051,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
if (sorts != null) {
builder.startArray(SORT_FIELD.getPreferredName());
for (SortBuilder<?> sort : sorts) {
builder.startObject();
sort.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}

View File

@ -42,7 +42,7 @@ import java.util.Objects;
* A sort builder to sort based on a document field.
*/
public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
public static final FieldSortBuilder PROTOTYPE = new FieldSortBuilder("");
public static final FieldSortBuilder PROTOTYPE = new FieldSortBuilder("_na_");
public static final String NAME = "field_sort";
public static final ParseField NESTED_PATH = new ParseField("nested_path");
public static final ParseField NESTED_FILTER = new ParseField("nested_filter");
@ -199,6 +199,7 @@ public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startObject(fieldName);
builder.field(ORDER_FIELD.getPreferredName(), order);
if (missing != null) {
@ -217,6 +218,7 @@ public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
builder.field(NESTED_PATH.getPreferredName(), nestedPath);
}
builder.endObject();
builder.endObject();
return builder;
}

View File

@ -300,6 +300,7 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startObject(NAME);
builder.startArray(fieldName);
@ -325,6 +326,7 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
builder.field(COERCE_FIELD.getPreferredName(), coerce);
builder.field(IGNORE_MALFORMED_FIELD.getPreferredName(), ignoreMalformed);
builder.endObject();
builder.endObject();
return builder;
}

View File

@ -53,9 +53,11 @@ public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startObject(NAME);
builder.field(ORDER_FIELD.getPreferredName(), order);
builder.endObject();
builder.endObject();
return builder;
}

View File

@ -179,6 +179,7 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
builder.startObject();
builder.startObject(NAME);
builder.field(SCRIPT_FIELD.getPreferredName(), script);
builder.field(TYPE_FIELD.getPreferredName(), type);
@ -193,6 +194,7 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
builder.field(NESTED_FILTER_FIELD.getPreferredName(), nestedFilter, builderParams);
}
builder.endObject();
builder.endObject();
return builder;
}

View File

@ -129,10 +129,7 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST
if (randomBoolean()) {
builder.prettyPrint();
}
builder.startObject();
testItem.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser itemParser = XContentHelper.createParser(builder.bytes());
itemParser.nextToken();

View File

@ -160,15 +160,11 @@ public class SortBuilderTests extends ESTestCase {
xContentBuilder.endObject();
break;
case 2:
xContentBuilder.startObject();
builder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
xContentBuilder.endObject();
break;
}
} else {
xContentBuilder.startObject();
builder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
xContentBuilder.endObject();
}
}
if (testBuilders.size() > 1) {