Sort: Move up `order` field to SortBuilder

Currently all SortBuilder implementations have their separate order
field. This PR moves this up to SortBuilder, together with setter
and getter and makes sure the default is set to SortOrder.ASC
except for `_score` sorting where the default is SortOrder.DESC.
This commit is contained in:
Christoph Büscher 2016-03-08 18:37:20 +01:00
parent 7621c8875f
commit b4db26eaf9
6 changed files with 61 additions and 111 deletions

View File

@ -27,12 +27,10 @@ import java.io.IOException;
/**
* A sort builder to sort based on a document field.
*/
public class FieldSortBuilder extends SortBuilder {
public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
private final String fieldName;
private SortOrder order;
private Object missing;
private String unmappedType;
@ -55,15 +53,6 @@ public class FieldSortBuilder extends SortBuilder {
this.fieldName = fieldName;
}
/**
* The order of sorting. Defaults to {@link SortOrder#ASC}.
*/
@Override
public FieldSortBuilder order(SortOrder order) {
this.order = order;
return this;
}
/**
* Sets the value when a field is missing in a doc. Can also be set to <tt>_last</tt> or
* <tt>_first</tt> to sort missing last or first respectively.
@ -118,9 +107,7 @@ public class FieldSortBuilder extends SortBuilder {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(fieldName);
if (order != null) {
builder.field("order", order.toString());
}
builder.field(ORDER_FIELD.getPreferredName(), order);
if (missing != null) {
builder.field("missing", missing);
}

View File

@ -44,7 +44,7 @@ import java.util.Objects;
/**
* A geo distance based sorting on a geo point like field.
*/
public class GeoDistanceSortBuilder extends SortBuilder
public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
implements ToXContent, NamedWriteable<GeoDistanceSortBuilder>, SortElementParserTemp<GeoDistanceSortBuilder> {
public static final String NAME = "_geo_distance";
public static final boolean DEFAULT_COERCE = false;
@ -57,7 +57,6 @@ public class GeoDistanceSortBuilder extends SortBuilder
private GeoDistance geoDistance = GeoDistance.DEFAULT;
private DistanceUnit unit = DistanceUnit.DEFAULT;
private SortOrder order = SortOrder.ASC;
// TODO there is an enum that covers that parameter which we should be using here
private String sortMode = null;
@ -204,20 +203,6 @@ public class GeoDistanceSortBuilder extends SortBuilder
return this.unit;
}
/**
* The order of sorting. Defaults to {@link SortOrder#ASC}.
*/
@Override
public GeoDistanceSortBuilder order(SortOrder order) {
this.order = order;
return this;
}
/** Returns the order of sorting. */
public SortOrder order() {
return this.order;
}
/**
* Defines which distance to use for sorting in the case a document contains multiple geo points.
* Possible values: min and max
@ -240,7 +225,7 @@ public class GeoDistanceSortBuilder extends SortBuilder
* Sets the nested filter that the nested objects should match with in order to be taken into account
* for sorting.
*/
public GeoDistanceSortBuilder setNestedFilter(QueryBuilder nestedFilter) {
public GeoDistanceSortBuilder setNestedFilter(QueryBuilder<?> nestedFilter) {
this.nestedFilter = nestedFilter;
return this;
}
@ -249,7 +234,7 @@ public class GeoDistanceSortBuilder extends SortBuilder
* Returns the nested filter that the nested objects should match with in order to be taken into account
* for sorting.
**/
public QueryBuilder getNestedFilter() {
public QueryBuilder<?> getNestedFilter() {
return this.nestedFilter;
}
@ -302,11 +287,7 @@ public class GeoDistanceSortBuilder extends SortBuilder
builder.field("unit", unit);
builder.field("distance_type", geoDistance.name().toLowerCase(Locale.ROOT));
if (order == SortOrder.DESC) {
builder.field("reverse", true);
} else {
builder.field("reverse", false);
}
builder.field(ORDER_FIELD.getPreferredName(), order);
if (sortMode != null) {
builder.field("mode", sortMode);
@ -409,9 +390,9 @@ public class GeoDistanceSortBuilder extends SortBuilder
List<GeoPoint> geoPoints = new ArrayList<>();
DistanceUnit unit = DistanceUnit.DEFAULT;
GeoDistance geoDistance = GeoDistance.DEFAULT;
boolean reverse = false;
SortOrder order = SortOrder.ASC;
MultiValueMode sortMode = null;
QueryBuilder nestedFilter = null;
QueryBuilder<?> nestedFilter = null;
String nestedPath = null;
boolean coerce = GeoDistanceSortBuilder.DEFAULT_COERCE;
@ -441,9 +422,9 @@ public class GeoDistanceSortBuilder extends SortBuilder
}
} else if (token.isValue()) {
if ("reverse".equals(currentName)) {
reverse = parser.booleanValue();
order = parser.booleanValue() ? SortOrder.DESC : SortOrder.ASC;
} else if ("order".equals(currentName)) {
reverse = "desc".equals(parser.text());
order = SortOrder.fromString(parser.text());
} else if ("unit".equals(currentName)) {
unit = DistanceUnit.fromString(parser.text());
} else if ("distance_type".equals(currentName) || "distanceType".equals(currentName)) {
@ -474,11 +455,7 @@ public class GeoDistanceSortBuilder extends SortBuilder
GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, geoPoints.toArray(new GeoPoint[geoPoints.size()]));
result.geoDistance(geoDistance);
result.unit(unit);
if (reverse) {
result.order(SortOrder.DESC);
} else {
result.order(SortOrder.ASC);
}
result.order(order);
if (sortMode != null) {
result.sortMode(sortMode.name());
}

View File

@ -35,38 +35,24 @@ import java.util.Objects;
/**
* A sort builder allowing to sort by score.
*/
public class ScoreSortBuilder extends SortBuilder implements NamedWriteable<ScoreSortBuilder>,
public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> implements NamedWriteable<ScoreSortBuilder>,
SortElementParserTemp<ScoreSortBuilder> {
private static final String NAME = "_score";
static final ScoreSortBuilder PROTOTYPE = new ScoreSortBuilder();
public static final ParseField REVERSE_FIELD = new ParseField("reverse");
public static final ParseField ORDER_FIELD = new ParseField("order");
private SortOrder order = SortOrder.DESC;
/**
* The order of sort scoring. By default, its {@link SortOrder#DESC}.
*/
@Override
public ScoreSortBuilder order(SortOrder order) {
Objects.requireNonNull(order, "sort order cannot be null.");
this.order = order;
return this;
public ScoreSortBuilder() {
// order defaults to desc when sorting on the _score
order(SortOrder.DESC);
}
/**
* Get the order of sort scoring. By default, its {@link SortOrder#DESC}.
*/
public SortOrder order() {
return this.order;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (order == SortOrder.ASC) {
builder.field(REVERSE_FIELD.getPreferredName(), true);
}
builder.field(ORDER_FIELD.getPreferredName(), order);
builder.endObject();
return builder;
}
@ -124,7 +110,8 @@ public class ScoreSortBuilder extends SortBuilder implements NamedWriteable<Scor
@Override
public ScoreSortBuilder readFrom(StreamInput in) throws IOException {
return new ScoreSortBuilder().order(SortOrder.readOrderFrom(in));
ScoreSortBuilder builder = new ScoreSortBuilder().order(SortOrder.readOrderFrom(in));
return builder;
}
@Override

View File

@ -28,14 +28,12 @@ import java.io.IOException;
/**
* Script sort builder allows to sort based on a custom script expression.
*/
public class ScriptSortBuilder extends SortBuilder {
public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
private Script script;
private final String type;
private SortOrder order;
private String sortMode;
private QueryBuilder nestedFilter;
@ -53,15 +51,6 @@ public class ScriptSortBuilder extends SortBuilder {
this.type = type;
}
/**
* Sets the sort order.
*/
@Override
public ScriptSortBuilder order(SortOrder order) {
this.order = order;
return this;
}
/**
* Defines which distance to use for sorting in the case a document contains multiple geo points.
* Possible values: min and max
@ -75,7 +64,7 @@ public class ScriptSortBuilder extends SortBuilder {
* Sets the nested filter that the nested objects should match with in order to be taken into account
* for sorting.
*/
public ScriptSortBuilder setNestedFilter(QueryBuilder nestedFilter) {
public ScriptSortBuilder setNestedFilter(QueryBuilder<?> nestedFilter) {
this.nestedFilter = nestedFilter;
return this;
}
@ -94,9 +83,7 @@ public class ScriptSortBuilder extends SortBuilder {
builder.startObject("_script");
builder.field("script", script);
builder.field("type", type);
if (order == SortOrder.DESC) {
builder.field("reverse", true);
}
builder.field(ORDER_FIELD.getPreferredName(), order);
if (sortMode != null) {
builder.field("mode", sortMode);
}

View File

@ -20,14 +20,20 @@
package org.elasticsearch.search.sort;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.util.Objects;
/**
*
*/
public abstract class SortBuilder implements ToXContent {
public abstract class SortBuilder<T extends SortBuilder<?>> implements ToXContent {
protected SortOrder order = SortOrder.ASC;
public static final ParseField ORDER_FIELD = new ParseField("order");
@Override
public String toString() {
@ -42,7 +48,19 @@ public abstract class SortBuilder implements ToXContent {
}
/**
* The order of sorting. Defaults to {@link SortOrder#ASC}.
* Set the order of sorting.
*/
public abstract SortBuilder order(SortOrder order);
@SuppressWarnings("unchecked")
public T order(SortOrder order) {
Objects.requireNonNull(order, "sort order cannot be null.");
this.order = order;
return (T) this;
}
/**
* Return the {@link SortOrder} used for this {@link SortBuilder}.
*/
public SortOrder order() {
return this.order;
}
}

View File

@ -49,15 +49,9 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.env.EnvironmentModule;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.query.AbstractQueryTestCase;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.EmptyQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WrapperQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionParser;
import org.elasticsearch.indices.IndicesModule;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
@ -554,7 +548,7 @@ public class SearchSourceBuilderTests extends ESTestCase {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser),
aggParsers);
assertEquals(1, searchSourceBuilder.sorts().size());
assertEquals("{\"foo\":{}}", searchSourceBuilder.sorts().get(0).toUtf8());
assertEquals("{\"foo\":{\"order\":\"asc\"}}", searchSourceBuilder.sorts().get(0).toUtf8());
}
}