mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-09 14:34:43 +00:00
Remove PROTOTYPE from SortBuilders
This commit is contained in:
parent
6cb82965bf
commit
8a89482555
@ -711,6 +711,16 @@ public abstract class StreamInput extends InputStream {
|
||||
return readNamedWriteable(QueryBuilder.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an optional {@link QueryBuilder}.
|
||||
*/
|
||||
public QueryBuilder<?> readOptionalQuery() throws IOException {
|
||||
if (readBoolean()) {
|
||||
return readNamedWriteable(QueryBuilder.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a {@link ShapeBuilder} from the current stream
|
||||
*/
|
||||
|
@ -689,6 +689,18 @@ public abstract class StreamOutput extends OutputStream {
|
||||
writeNamedWriteable(queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an optional {@link QueryBuilder} to the stream.
|
||||
*/
|
||||
public void writeOptionalQuery(@Nullable QueryBuilder<?> queryBuilder) throws IOException {
|
||||
if (queryBuilder == null) {
|
||||
writeBoolean(false);
|
||||
} else {
|
||||
writeBoolean(true);
|
||||
writeQuery(queryBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a {@link ShapeBuilder} to the current stream
|
||||
*/
|
||||
|
@ -287,6 +287,7 @@ public class SearchModule extends AbstractModule {
|
||||
registerBuiltinFunctionScoreParsers();
|
||||
registerBuiltinQueryParsers();
|
||||
registerBuiltinRescorers();
|
||||
registerBuiltinSorts();
|
||||
}
|
||||
|
||||
public void registerHighlighter(String key, Class<? extends Highlighter> clazz) {
|
||||
@ -351,7 +352,6 @@ public class SearchModule extends AbstractModule {
|
||||
configureSuggesters();
|
||||
configureFetchSubPhase();
|
||||
configureShapes();
|
||||
configureSorts();
|
||||
}
|
||||
|
||||
protected void configureFetchSubPhase() {
|
||||
@ -495,11 +495,11 @@ public class SearchModule extends AbstractModule {
|
||||
namedWriteableRegistry.register(RescoreBuilder.class, QueryRescorerBuilder.NAME, QueryRescorerBuilder::new);
|
||||
}
|
||||
|
||||
private void configureSorts() {
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, GeoDistanceSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScoreSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScriptSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, FieldSortBuilder.PROTOTYPE);
|
||||
private void registerBuiltinSorts() {
|
||||
namedWriteableRegistry.register(SortBuilder.class, GeoDistanceSortBuilder.NAME, GeoDistanceSortBuilder::new);
|
||||
namedWriteableRegistry.register(SortBuilder.class, ScoreSortBuilder.NAME, ScoreSortBuilder::new);
|
||||
namedWriteableRegistry.register(SortBuilder.class, ScriptSortBuilder.NAME, ScriptSortBuilder::new);
|
||||
namedWriteableRegistry.register(SortBuilder.class, FieldSortBuilder.NAME, FieldSortBuilder::new);
|
||||
}
|
||||
|
||||
private void registerBuiltinFunctionScoreParsers() {
|
||||
|
@ -42,7 +42,6 @@ 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("_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");
|
||||
@ -97,6 +96,30 @@ public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from a stream.
|
||||
*/
|
||||
public FieldSortBuilder(StreamInput in) throws IOException {
|
||||
fieldName = in.readString();
|
||||
nestedFilter = in.readOptionalQuery();
|
||||
nestedPath = in.readOptionalString();
|
||||
missing = in.readGenericValue();
|
||||
order = in.readOptionalWriteable(SortOrder::readFromStream);
|
||||
sortMode = in.readOptionalWriteable(SortMode::readFromStream);
|
||||
unmappedType = in.readOptionalString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeString(fieldName);
|
||||
out.writeOptionalQuery(nestedFilter);
|
||||
out.writeOptionalString(nestedPath);
|
||||
out.writeGenericValue(missing);
|
||||
out.writeOptionalWriteable(order);
|
||||
out.writeOptionalWriteable(sortMode);
|
||||
out.writeOptionalString(unmappedType);
|
||||
}
|
||||
|
||||
/** Returns the document field this sort should be based on. */
|
||||
public String getFieldName() {
|
||||
return this.fieldName;
|
||||
@ -292,55 +315,16 @@ public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeString(this.fieldName);
|
||||
if (this.nestedFilter != null) {
|
||||
out.writeBoolean(true);
|
||||
out.writeQuery(this.nestedFilter);
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
}
|
||||
out.writeOptionalString(this.nestedPath);
|
||||
out.writeGenericValue(this.missing);
|
||||
|
||||
if (this.order != null) {
|
||||
out.writeBoolean(true);
|
||||
this.order.writeTo(out);
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
}
|
||||
|
||||
out.writeBoolean(this.sortMode != null);
|
||||
if (this.sortMode != null) {
|
||||
this.sortMode.writeTo(out);
|
||||
}
|
||||
out.writeOptionalString(this.unmappedType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldSortBuilder readFrom(StreamInput in) throws IOException {
|
||||
String fieldName = in.readString();
|
||||
FieldSortBuilder result = new FieldSortBuilder(fieldName);
|
||||
if (in.readBoolean()) {
|
||||
QueryBuilder<?> query = in.readQuery();
|
||||
result.setNestedFilter(query);
|
||||
}
|
||||
result.setNestedPath(in.readOptionalString());
|
||||
result.missing(in.readGenericValue());
|
||||
|
||||
if (in.readBoolean()) {
|
||||
result.order(SortOrder.readOrderFrom(in));
|
||||
}
|
||||
if (in.readBoolean()) {
|
||||
result.sortMode(SortMode.PROTOTYPE.readFrom(in));
|
||||
}
|
||||
result.unmappedType(in.readOptionalString());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
/**
|
||||
* Creates a new {@link FieldSortBuilder} from the query held by the {@link QueryParseContext} in
|
||||
* {@link org.elasticsearch.common.xcontent.XContent} format.
|
||||
*
|
||||
* @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this
|
||||
* method call
|
||||
* @param fieldName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g.
|
||||
* in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument
|
||||
*/
|
||||
public static FieldSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
XContentParser parser = context.parser();
|
||||
|
||||
QueryBuilder<?> nestedFilter = null;
|
||||
|
@ -74,8 +74,6 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
|
||||
public static final ParseField NESTED_PATH_FIELD = new ParseField("nested_path");
|
||||
public static final ParseField NESTED_FILTER_FIELD = new ParseField("nested_filter");
|
||||
|
||||
public static final GeoDistanceSortBuilder PROTOTYPE = new GeoDistanceSortBuilder("_na_", -1, -1);
|
||||
|
||||
private final String fieldName;
|
||||
private final List<GeoPoint> points = new ArrayList<>();
|
||||
|
||||
@ -148,6 +146,37 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
|
||||
this.ignoreMalformed = original.ignoreMalformed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from a stream.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public GeoDistanceSortBuilder(StreamInput in) throws IOException {
|
||||
fieldName = in.readString();
|
||||
points.addAll((List<GeoPoint>) in.readGenericValue());
|
||||
geoDistance = GeoDistance.readGeoDistanceFrom(in);
|
||||
unit = DistanceUnit.readDistanceUnit(in);
|
||||
order = SortOrder.readFromStream(in);
|
||||
sortMode = in.readOptionalWriteable(SortMode::readFromStream);
|
||||
nestedFilter = in.readOptionalQuery();
|
||||
nestedPath = in.readOptionalString();
|
||||
coerce = in.readBoolean();
|
||||
ignoreMalformed =in.readBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeString(fieldName);
|
||||
out.writeGenericValue(points);
|
||||
geoDistance.writeTo(out);
|
||||
unit.writeTo(out);
|
||||
order.writeTo(out);
|
||||
out.writeOptionalWriteable(sortMode);
|
||||
out.writeOptionalQuery(nestedFilter);
|
||||
out.writeOptionalString(nestedPath);
|
||||
out.writeBoolean(coerce);
|
||||
out.writeBoolean(ignoreMalformed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the geo point like field the distance based sort operates on.
|
||||
* */
|
||||
@ -365,53 +394,16 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
|
||||
this.unit, this.sortMode, this.order, this.nestedFilter, this.nestedPath, this.coerce, this.ignoreMalformed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeString(fieldName);
|
||||
out.writeGenericValue(points);
|
||||
|
||||
geoDistance.writeTo(out);
|
||||
unit.writeTo(out);
|
||||
order.writeTo(out);
|
||||
out.writeBoolean(this.sortMode != null);
|
||||
if (this.sortMode != null) {
|
||||
sortMode.writeTo(out);
|
||||
}
|
||||
if (nestedFilter != null) {
|
||||
out.writeBoolean(true);
|
||||
out.writeQuery(nestedFilter);
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
}
|
||||
out.writeOptionalString(nestedPath);
|
||||
out.writeBoolean(coerce);
|
||||
out.writeBoolean(ignoreMalformed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeoDistanceSortBuilder readFrom(StreamInput in) throws IOException {
|
||||
String fieldName = in.readString();
|
||||
|
||||
ArrayList<GeoPoint> points = (ArrayList<GeoPoint>) in.readGenericValue();
|
||||
GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, points.toArray(new GeoPoint[points.size()]));
|
||||
|
||||
result.geoDistance(GeoDistance.readGeoDistanceFrom(in));
|
||||
result.unit(DistanceUnit.readDistanceUnit(in));
|
||||
result.order(SortOrder.readOrderFrom(in));
|
||||
if (in.readBoolean()) {
|
||||
result.sortMode = SortMode.PROTOTYPE.readFrom(in);
|
||||
}
|
||||
if (in.readBoolean()) {
|
||||
result.setNestedFilter(in.readQuery());
|
||||
}
|
||||
result.setNestedPath(in.readOptionalString());
|
||||
result.coerce(in.readBoolean());
|
||||
result.ignoreMalformed(in.readBoolean());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeoDistanceSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
|
||||
/**
|
||||
* Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link QueryParseContext} in
|
||||
* {@link org.elasticsearch.common.xcontent.XContent} format.
|
||||
*
|
||||
* @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this
|
||||
* method call
|
||||
* @param elementName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g.
|
||||
* in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument
|
||||
*/
|
||||
public static GeoDistanceSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
|
||||
XContentParser parser = context.parser();
|
||||
ParseFieldMatcher parseFieldMatcher = context.parseFieldMatcher();
|
||||
String fieldName = null;
|
||||
|
@ -39,17 +39,30 @@ import java.util.Objects;
|
||||
public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> {
|
||||
|
||||
public static final String NAME = "_score";
|
||||
public 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 static final SortField SORT_SCORE = new SortField(null, SortField.Type.SCORE);
|
||||
private static final SortField SORT_SCORE_REVERSE = new SortField(null, SortField.Type.SCORE, true);
|
||||
|
||||
/**
|
||||
* Build a ScoreSortBuilder default to descending sort order.
|
||||
*/
|
||||
public ScoreSortBuilder() {
|
||||
// order defaults to desc when sorting on the _score
|
||||
order(SortOrder.DESC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from a stream.
|
||||
*/
|
||||
public ScoreSortBuilder(StreamInput in) throws IOException {
|
||||
order(SortOrder.readFromStream(in));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
order.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
@ -61,8 +74,16 @@ public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> {
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScoreSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
|
||||
/**
|
||||
* Creates a new {@link ScoreSortBuilder} from the query held by the {@link QueryParseContext} in
|
||||
* {@link org.elasticsearch.common.xcontent.XContent} format.
|
||||
*
|
||||
* @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this
|
||||
* method call
|
||||
* @param fieldName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g.
|
||||
* in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument
|
||||
*/
|
||||
public static ScoreSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
XContentParser parser = context.parser();
|
||||
ParseFieldMatcher matcher = context.parseFieldMatcher();
|
||||
|
||||
@ -116,17 +137,6 @@ public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> {
|
||||
return Objects.hash(this.order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
order.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScoreSortBuilder readFrom(StreamInput in) throws IOException {
|
||||
ScoreSortBuilder builder = new ScoreSortBuilder().order(SortOrder.readOrderFrom(in));
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWriteableName() {
|
||||
return NAME;
|
||||
|
@ -67,7 +67,6 @@ import java.util.Objects;
|
||||
public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
|
||||
public static final String NAME = "_script";
|
||||
public static final ScriptSortBuilder PROTOTYPE = new ScriptSortBuilder(new Script("_na_"), ScriptSortType.STRING);
|
||||
public static final ParseField TYPE_FIELD = new ParseField("type");
|
||||
public static final ParseField SCRIPT_FIELD = new ParseField("script");
|
||||
public static final ParseField SORTMODE_FIELD = new ParseField("mode");
|
||||
@ -110,6 +109,28 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
this.nestedPath = original.nestedPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from a stream.
|
||||
*/
|
||||
public ScriptSortBuilder(StreamInput in) throws IOException {
|
||||
script = Script.readScript(in);
|
||||
type = ScriptSortType.readFromStream(in);
|
||||
order = SortOrder.readFromStream(in);
|
||||
sortMode = in.readOptionalWriteable(SortMode::readFromStream);
|
||||
nestedPath = in.readOptionalString();
|
||||
nestedFilter = in.readOptionalQuery();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
script.writeTo(out);
|
||||
type.writeTo(out);
|
||||
order.writeTo(out);
|
||||
out.writeOptionalWriteable(sortMode);
|
||||
out.writeOptionalString(nestedPath);
|
||||
out.writeOptionalQuery(nestedFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the script used in this sort.
|
||||
*/
|
||||
@ -198,8 +219,16 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
|
||||
/**
|
||||
* Creates a new {@link ScriptSortBuilder} from the query held by the {@link QueryParseContext} in
|
||||
* {@link org.elasticsearch.common.xcontent.XContent} format.
|
||||
*
|
||||
* @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this
|
||||
* method call
|
||||
* @param elementName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g.
|
||||
* in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument
|
||||
*/
|
||||
public static ScriptSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
|
||||
ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
|
||||
XContentParser parser = context.parser();
|
||||
ParseFieldMatcher parseField = context.parseFieldMatcher();
|
||||
@ -362,37 +391,6 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
return Objects.hash(script, type, order, sortMode, nestedFilter, nestedPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
script.writeTo(out);
|
||||
type.writeTo(out);
|
||||
order.writeTo(out);
|
||||
out.writeBoolean(sortMode != null);
|
||||
if (sortMode != null) {
|
||||
sortMode.writeTo(out);
|
||||
}
|
||||
out.writeOptionalString(nestedPath);
|
||||
boolean hasNestedFilter = nestedFilter != null;
|
||||
out.writeBoolean(hasNestedFilter);
|
||||
if (hasNestedFilter) {
|
||||
out.writeQuery(nestedFilter);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptSortBuilder readFrom(StreamInput in) throws IOException {
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(Script.readScript(in), ScriptSortType.PROTOTYPE.readFrom(in));
|
||||
builder.order(SortOrder.readOrderFrom(in));
|
||||
if (in.readBoolean()) {
|
||||
builder.sortMode(SortMode.PROTOTYPE.readFrom(in));
|
||||
}
|
||||
builder.nestedPath = in.readOptionalString();
|
||||
if (in.readBoolean()) {
|
||||
builder.nestedFilter = in.readQuery();
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWriteableName() {
|
||||
return NAME;
|
||||
@ -404,15 +402,15 @@ public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
/** script sort for a numeric value **/
|
||||
NUMBER;
|
||||
|
||||
static ScriptSortType PROTOTYPE = STRING;
|
||||
|
||||
@Override
|
||||
public void writeTo(final StreamOutput out) throws IOException {
|
||||
out.writeVInt(ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptSortType readFrom(final StreamInput in) throws IOException {
|
||||
/**
|
||||
* Read from a stream.
|
||||
*/
|
||||
static ScriptSortType readFromStream(final StreamInput in) throws IOException {
|
||||
int ordinal = in.readVInt();
|
||||
if (ordinal < 0 || ordinal >= values().length) {
|
||||
throw new IOException("Unknown ScriptSortType ordinal [" + ordinal + "]");
|
||||
|
@ -24,7 +24,6 @@ import org.apache.lucene.search.Sort;
|
||||
import org.apache.lucene.search.SortField;
|
||||
import org.apache.lucene.search.join.BitSetProducer;
|
||||
import org.elasticsearch.action.support.ToXContentToBytes;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteable;
|
||||
import org.elasticsearch.common.lucene.search.Queries;
|
||||
@ -55,33 +54,17 @@ public abstract class SortBuilder<T extends SortBuilder<?>> extends ToXContentTo
|
||||
protected SortOrder order = SortOrder.ASC;
|
||||
public static final ParseField ORDER_FIELD = new ParseField("order");
|
||||
|
||||
private static final Map<String, SortBuilder<?>> PARSERS;
|
||||
|
||||
private static final Map<String, Parser<?>> PARSERS;
|
||||
static {
|
||||
Map<String, SortBuilder<?>> parsers = new HashMap<>();
|
||||
parsers.put(ScriptSortBuilder.NAME, ScriptSortBuilder.PROTOTYPE);
|
||||
parsers.put(GeoDistanceSortBuilder.NAME, new GeoDistanceSortBuilder("_na_", -1, -1));
|
||||
parsers.put(GeoDistanceSortBuilder.ALTERNATIVE_NAME, new GeoDistanceSortBuilder("_na_", -1, -1));
|
||||
parsers.put(ScoreSortBuilder.NAME, ScoreSortBuilder.PROTOTYPE);
|
||||
Map<String, Parser<?>> parsers = new HashMap<>();
|
||||
parsers.put(ScriptSortBuilder.NAME, ScriptSortBuilder::fromXContent);
|
||||
parsers.put(GeoDistanceSortBuilder.NAME, GeoDistanceSortBuilder::fromXContent);
|
||||
parsers.put(GeoDistanceSortBuilder.ALTERNATIVE_NAME, GeoDistanceSortBuilder::fromXContent);
|
||||
parsers.put(ScoreSortBuilder.NAME, ScoreSortBuilder::fromXContent);
|
||||
// FieldSortBuilder gets involved if the user specifies a name that isn't one of these.
|
||||
PARSERS = unmodifiableMap(parsers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link SortBuilder} from the query held by the {@link QueryParseContext}
|
||||
* in {@link org.elasticsearch.common.xcontent.XContent} format
|
||||
*
|
||||
* @param parseContext
|
||||
* the input parse context. The state on the parser contained in
|
||||
* this context will be changed as a side effect of this method call
|
||||
* @param fieldName
|
||||
* in some sort syntax variations the field name precedes the xContent object that
|
||||
* specifies further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When
|
||||
* parsing the inner object, the field name can be passed in via this argument
|
||||
*
|
||||
* @return the new sort builder instance
|
||||
*/
|
||||
protected abstract T fromXContent(QueryParseContext parseContext, @Nullable String fieldName) throws IOException;
|
||||
|
||||
/**
|
||||
* Create a @link {@link SortField} from this builder.
|
||||
*/
|
||||
@ -153,7 +136,7 @@ public abstract class SortBuilder<T extends SortBuilder<?>> extends ToXContentTo
|
||||
if (PARSERS.containsKey(fieldName)) {
|
||||
sortFields.add(PARSERS.get(fieldName).fromXContent(context, fieldName));
|
||||
} else {
|
||||
sortFields.add(FieldSortBuilder.PROTOTYPE.fromXContent(context, fieldName));
|
||||
sortFields.add(FieldSortBuilder.fromXContent(context, fieldName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -218,4 +201,9 @@ public abstract class SortBuilder<T extends SortBuilder<?>> extends ToXContentTo
|
||||
}
|
||||
return nested;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface Parser<T extends SortBuilder<?>> {
|
||||
T fromXContent(QueryParseContext context, String elementName) throws IOException;
|
||||
}
|
||||
}
|
||||
|
@ -50,15 +50,12 @@ public enum SortMode implements Writeable<SortMode> {
|
||||
/** Use the median of all values as sort value. Only applicable for number based array fields. **/
|
||||
MEDIAN;
|
||||
|
||||
static SortMode PROTOTYPE = MIN;
|
||||
|
||||
@Override
|
||||
public void writeTo(final StreamOutput out) throws IOException {
|
||||
out.writeVInt(ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortMode readFrom(final StreamInput in) throws IOException {
|
||||
public static SortMode readFromStream(StreamInput in) throws IOException {
|
||||
int ordinal = in.readVInt();
|
||||
if (ordinal < 0 || ordinal >= values().length) {
|
||||
throw new IOException("Unknown SortMode ordinal [" + ordinal + "]");
|
||||
|
@ -50,11 +50,8 @@ public enum SortOrder implements Writeable<SortOrder> {
|
||||
return "desc";
|
||||
}
|
||||
};
|
||||
|
||||
private static final SortOrder PROTOTYPE = ASC;
|
||||
|
||||
@Override
|
||||
public SortOrder readFrom(StreamInput in) throws IOException {
|
||||
static SortOrder readFromStream(StreamInput in) throws IOException {
|
||||
int ordinal = in.readVInt();
|
||||
if (ordinal < 0 || ordinal >= values().length) {
|
||||
throw new IOException("Unknown SortOrder ordinal [" + ordinal + "]");
|
||||
@ -62,10 +59,6 @@ public enum SortOrder implements Writeable<SortOrder> {
|
||||
return values()[ordinal];
|
||||
}
|
||||
|
||||
public static SortOrder readOrderFrom(StreamInput in) throws IOException {
|
||||
return PROTOTYPE.readFrom(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeVInt(this.ordinal());
|
||||
|
@ -100,10 +100,6 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST
|
||||
};
|
||||
|
||||
namedWriteableRegistry = new NamedWriteableRegistry();
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, GeoDistanceSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScoreSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScriptSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, FieldSortBuilder.PROTOTYPE);
|
||||
indicesQueriesRegistry = new SearchModule(Settings.EMPTY, namedWriteableRegistry).buildQueryParserRegistry();
|
||||
}
|
||||
|
||||
@ -118,6 +114,9 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST
|
||||
/** Returns mutated version of original so the returned sort is different in terms of equals/hashcode */
|
||||
protected abstract T mutate(T original) throws IOException;
|
||||
|
||||
/** Parse the sort from xContent. Just delegate to the SortBuilder's static fromXContent method. */
|
||||
protected abstract T fromXContent(QueryParseContext context, String fieldName) throws IOException;
|
||||
|
||||
/**
|
||||
* Test that creates new sort from a random test sort and checks both for equality
|
||||
*/
|
||||
@ -142,7 +141,7 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST
|
||||
|
||||
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
|
||||
context.reset(itemParser);
|
||||
T parsedItem = testItem.fromXContent(context, elementName);
|
||||
T parsedItem = fromXContent(context, elementName);
|
||||
assertNotSame(testItem, parsedItem);
|
||||
assertEquals(testItem, parsedItem);
|
||||
assertEquals(testItem.hashCode(), parsedItem.hashCode());
|
||||
|
@ -20,6 +20,7 @@ x * Licensed to Elasticsearch under one or more contributor
|
||||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.apache.lucene.search.SortField;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -103,4 +104,9 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
|
||||
assertEquals(builder.getFieldName(), sortField.getField());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FieldSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
return FieldSortBuilder.fromXContent(context, fieldName);
|
||||
}
|
||||
}
|
||||
|
@ -221,12 +221,8 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
|
||||
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
|
||||
context.reset(itemParser);
|
||||
|
||||
try {
|
||||
GeoDistanceSortBuilder.PROTOTYPE.fromXContent(context, "");
|
||||
fail("sort mode sum should not be supported");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// all good
|
||||
}
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> GeoDistanceSortBuilder.fromXContent(context, ""));
|
||||
assertEquals("sort_mode [sum] isn't supported for sorting by geo distance", e.getMessage());
|
||||
}
|
||||
|
||||
public void testGeoDistanceSortCanBeParsedFromGeoHash() throws IOException {
|
||||
@ -253,7 +249,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
|
||||
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
|
||||
context.reset(itemParser);
|
||||
|
||||
GeoDistanceSortBuilder result = GeoDistanceSortBuilder.PROTOTYPE.fromXContent(context, json);
|
||||
GeoDistanceSortBuilder result = GeoDistanceSortBuilder.fromXContent(context, json);
|
||||
assertEquals("[-19.700583312660456, -2.8225036337971687, "
|
||||
+ "31.537466906011105, -74.63590376079082, "
|
||||
+ "43.71844606474042, -5.548660643398762, "
|
||||
@ -261,4 +257,9 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
|
||||
+ "-69.44606635719538, 84.25200328230858, "
|
||||
+ "-39.03717711567879, 44.74099852144718]", Arrays.toString(result.points()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GeoDistanceSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
return GeoDistanceSortBuilder.fromXContent(context, fieldName);
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder
|
||||
parser.nextToken();
|
||||
|
||||
context.reset(parser);
|
||||
ScoreSortBuilder scoreSort = ScoreSortBuilder.PROTOTYPE.fromXContent(context, "_score");
|
||||
ScoreSortBuilder scoreSort = ScoreSortBuilder.fromXContent(context, "_score");
|
||||
assertEquals(order, scoreSort.order());
|
||||
}
|
||||
|
||||
@ -86,4 +86,9 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder
|
||||
assertEquals(SortField.Type.SCORE, sortField.getType());
|
||||
assertEquals(builder.order() == SortOrder.DESC ? false : true, sortField.getReverse());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScoreSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
return ScoreSortBuilder.fromXContent(context, fieldName);
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
parser.nextToken();
|
||||
|
||||
context.reset(parser);
|
||||
ScriptSortBuilder builder = ScriptSortBuilder.PROTOTYPE.fromXContent(context, null);
|
||||
ScriptSortBuilder builder = ScriptSortBuilder.fromXContent(context, null);
|
||||
assertEquals("doc['field_name'].value * factor", builder.script().getScript());
|
||||
assertNull(builder.script().getLang());
|
||||
assertEquals(1.1, builder.script().getParams().get("factor"));
|
||||
@ -211,7 +211,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
parser.nextToken();
|
||||
|
||||
context.reset(parser);
|
||||
ScriptSortBuilder builder = ScriptSortBuilder.PROTOTYPE.fromXContent(context, null);
|
||||
ScriptSortBuilder builder = ScriptSortBuilder.fromXContent(context, null);
|
||||
assertEquals("doc['field_name'].value * factor", builder.script().getScript());
|
||||
assertNull(builder.script().getLang());
|
||||
assertEquals(1.1, builder.script().getParams().get("factor"));
|
||||
@ -235,7 +235,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
context.reset(parser);
|
||||
exceptionRule.expect(ParsingException.class);
|
||||
exceptionRule.expectMessage("failed to parse field [bad_field]");
|
||||
ScriptSortBuilder.PROTOTYPE.fromXContent(context, null);
|
||||
ScriptSortBuilder.fromXContent(context, null);
|
||||
}
|
||||
|
||||
public void testParseBadFieldNameExceptionsOnStartObject() throws IOException {
|
||||
@ -251,7 +251,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
context.reset(parser);
|
||||
exceptionRule.expect(ParsingException.class);
|
||||
exceptionRule.expectMessage("failed to parse field [bad_field]");
|
||||
ScriptSortBuilder.PROTOTYPE.fromXContent(context, null);
|
||||
ScriptSortBuilder.fromXContent(context, null);
|
||||
}
|
||||
|
||||
public void testParseUnexpectedToken() throws IOException {
|
||||
@ -267,7 +267,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
context.reset(parser);
|
||||
exceptionRule.expect(ParsingException.class);
|
||||
exceptionRule.expectMessage("unexpected token [START_ARRAY]");
|
||||
ScriptSortBuilder.PROTOTYPE.fromXContent(context, null);
|
||||
ScriptSortBuilder.fromXContent(context, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -279,4 +279,9 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
||||
exceptionRule.expectMessage("script sort of type [string] doesn't support mode");
|
||||
builder.sortMode(SortMode.fromString(randomFrom(new String[]{"avg", "median", "sum"})));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException {
|
||||
return ScriptSortBuilder.fromXContent(context, fieldName);
|
||||
}
|
||||
}
|
||||
|
@ -51,10 +51,6 @@ public class SortBuilderTests extends ESTestCase {
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
namedWriteableRegistry = new NamedWriteableRegistry();
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, GeoDistanceSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScoreSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, ScriptSortBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(SortBuilder.class, FieldSortBuilder.PROTOTYPE);
|
||||
indicesQueriesRegistry = new SearchModule(Settings.EMPTY, namedWriteableRegistry).buildQueryParserRegistry();
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ public class SortOrderTests extends ESTestCase {
|
||||
try (BytesStreamOutput out = new BytesStreamOutput()) {
|
||||
unit.writeTo(out);
|
||||
try (StreamInput in = StreamInput.wrap(out.bytes())) {
|
||||
assertThat("Roundtrip serialisation failed.", SortOrder.readOrderFrom(in), equalTo(unit));
|
||||
assertThat("Roundtrip serialisation failed.", SortOrder.readFromStream(in), equalTo(unit));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,6 +140,6 @@ public class SortParserTests extends ESSingleNodeTestCase {
|
||||
parser.setParseFieldMatcher(ParseFieldMatcher.STRICT);
|
||||
parseContext.reset(parser);
|
||||
parser.nextToken();
|
||||
GeoDistanceSortBuilder.PROTOTYPE.fromXContent(parseContext, null);
|
||||
GeoDistanceSortBuilder.fromXContent(parseContext, null);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user