Merge pull request #17248 from cbuescher/sort-add-parse-list
Moving the current top level parsing for the "sort" element into a static "fromXContent" method in SortBuilder.
This commit is contained in:
commit
ececff357b
|
@ -33,7 +33,7 @@ public interface QueryParser<QB extends QueryBuilder<QB>> {
|
|||
String[] names();
|
||||
|
||||
/**
|
||||
* Creates a new {@link QueryBuilder} from the query held by the {@link QueryShardContext}
|
||||
* Creates a new {@link QueryBuilder} from the query held by the {@link QueryParseContext}
|
||||
* in {@link org.elasticsearch.common.xcontent.XContent} format
|
||||
*
|
||||
* @param parseContext
|
||||
|
|
|
@ -43,7 +43,7 @@ import java.util.Objects;
|
|||
/**
|
||||
* A sort builder to sort based on a document field.
|
||||
*/
|
||||
public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> implements SortBuilderParser<FieldSortBuilder> {
|
||||
public class FieldSortBuilder extends SortBuilder<FieldSortBuilder> {
|
||||
static final FieldSortBuilder PROTOTYPE = new FieldSortBuilder("");
|
||||
public static final String NAME = "field_sort";
|
||||
public static final ParseField NESTED_PATH = new ParseField("nested_path");
|
||||
|
|
|
@ -60,8 +60,9 @@ import java.util.Objects;
|
|||
/**
|
||||
* A geo distance based sorting on a geo point like field.
|
||||
*/
|
||||
public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder> implements SortBuilderParser<GeoDistanceSortBuilder> {
|
||||
public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder> {
|
||||
public static final String NAME = "_geo_distance";
|
||||
public static final String ALTERNATIVE_NAME = "_geoDistance";
|
||||
public static final boolean DEFAULT_COERCE = false;
|
||||
public static final boolean DEFAULT_IGNORE_MALFORMED = false;
|
||||
public static final ParseField UNIT_FIELD = new ParseField("unit");
|
||||
|
|
|
@ -36,9 +36,9 @@ import java.util.Objects;
|
|||
/**
|
||||
* A sort builder allowing to sort by score.
|
||||
*/
|
||||
public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> implements SortBuilderParser<ScoreSortBuilder> {
|
||||
public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> {
|
||||
|
||||
private static final String NAME = "_score";
|
||||
public 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");
|
||||
|
@ -88,6 +88,7 @@ public class ScoreSortBuilder extends SortBuilder<ScoreSortBuilder> implements S
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortField build(QueryShardContext context) {
|
||||
if (order == SortOrder.DESC) {
|
||||
return SORT_SCORE;
|
||||
|
|
|
@ -64,9 +64,9 @@ import java.util.Objects;
|
|||
/**
|
||||
* Script sort builder allows to sort based on a custom script expression.
|
||||
*/
|
||||
public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> implements SortBuilderParser<ScriptSortBuilder> {
|
||||
public class ScriptSortBuilder extends SortBuilder<ScriptSortBuilder> {
|
||||
|
||||
private static final String NAME = "_script";
|
||||
public static final String NAME = "_script";
|
||||
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");
|
||||
|
|
|
@ -20,26 +20,142 @@
|
|||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.SortField;
|
||||
import org.apache.lucene.search.join.BitSetProducer;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
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;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
|
||||
import org.elasticsearch.index.mapper.object.ObjectMapper;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.index.query.QueryShardContext;
|
||||
import org.elasticsearch.index.query.QueryShardException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.util.Collections.unmodifiableMap;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public abstract class SortBuilder<T extends SortBuilder<?>> implements ToXContent {
|
||||
public abstract class SortBuilder<T extends SortBuilder<?>> extends ToXContentToBytes implements NamedWriteable<T> {
|
||||
|
||||
protected SortOrder order = SortOrder.ASC;
|
||||
public static final ParseField ORDER_FIELD = new ParseField("order");
|
||||
|
||||
private static final Map<String, SortBuilder<?>> 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);
|
||||
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.
|
||||
*/
|
||||
protected abstract SortField build(QueryShardContext context) throws IOException;
|
||||
|
||||
/**
|
||||
* Set the order of sorting.
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
public static List<SortBuilder<?>> fromXContent(QueryParseContext context) throws IOException {
|
||||
List<SortBuilder<?>> sortFields = new ArrayList<>(2);
|
||||
XContentParser parser = context.parser();
|
||||
XContentParser.Token token = parser.currentToken();
|
||||
if (token == XContentParser.Token.START_ARRAY) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
if (token == XContentParser.Token.START_OBJECT) {
|
||||
parseCompoundSortField(parser, context, sortFields);
|
||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||
String fieldName = parser.text();
|
||||
sortFields.add(fieldOrScoreSort(fieldName));
|
||||
} else {
|
||||
throw new IllegalArgumentException("malformed sort format, "
|
||||
+ "within the sort array, an object, or an actual string are allowed");
|
||||
}
|
||||
}
|
||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||
String fieldName = parser.text();
|
||||
sortFields.add(fieldOrScoreSort(fieldName));
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
parseCompoundSortField(parser, context, sortFields);
|
||||
} else {
|
||||
throw new IllegalArgumentException("malformed sort format, either start with array, object, or an actual string");
|
||||
}
|
||||
return sortFields;
|
||||
}
|
||||
|
||||
private static SortBuilder<?> fieldOrScoreSort(String fieldName) {
|
||||
if (fieldName.equals(ScoreSortBuilder.NAME)) {
|
||||
return new ScoreSortBuilder();
|
||||
} else {
|
||||
return new FieldSortBuilder(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseCompoundSortField(XContentParser parser, QueryParseContext context, List<SortBuilder<?>> sortFields)
|
||||
throws IOException {
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
String fieldName = parser.currentName();
|
||||
token = parser.nextToken();
|
||||
if (token == XContentParser.Token.VALUE_STRING) {
|
||||
SortOrder order = SortOrder.fromString(parser.text());
|
||||
sortFields.add(fieldOrScoreSort(fieldName).order(order));
|
||||
} else {
|
||||
if (PARSERS.containsKey(fieldName)) {
|
||||
sortFields.add(PARSERS.get(fieldName).fromXContent(context, fieldName));
|
||||
} else {
|
||||
sortFields.add(FieldSortBuilder.PROTOTYPE.fromXContent(context, fieldName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder<?> nestedFilter) throws IOException {
|
||||
Nested nested = null;
|
||||
|
@ -64,36 +180,4 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements ToXConten
|
|||
}
|
||||
return nested;
|
||||
}
|
||||
|
||||
protected SortOrder order = SortOrder.ASC;
|
||||
public static final ParseField ORDER_FIELD = new ParseField("order");
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.prettyPrint();
|
||||
toXContent(builder, EMPTY_PARAMS);
|
||||
return builder.string();
|
||||
} catch (Exception e) {
|
||||
throw new ElasticsearchException("Failed to build query", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the order of sorting.
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.apache.lucene.search.SortField;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteable;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.index.query.QueryShardContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface SortBuilderParser<T extends ToXContent> extends NamedWriteable<T>, ToXContent {
|
||||
/**
|
||||
* Creates a new item from the json held by the {@link SortBuilderParser}
|
||||
* 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
|
||||
* @return the new item
|
||||
*/
|
||||
T fromXContent(QueryParseContext context, String elementName) throws IOException;
|
||||
|
||||
/**
|
||||
* Create a @link {@link SortField} from this builder.
|
||||
*/
|
||||
SortField build(QueryShardContext context) throws IOException;
|
||||
}
|
|
@ -72,7 +72,7 @@ import java.util.Map;
|
|||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
public abstract class AbstractSortTestCase<T extends SortBuilder<T> & SortBuilderParser<T>> extends ESTestCase {
|
||||
public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends ESTestCase {
|
||||
|
||||
protected static NamedWriteableRegistry namedWriteableRegistry;
|
||||
|
||||
|
|
|
@ -25,10 +25,14 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
|
|||
|
||||
@Override
|
||||
protected FieldSortBuilder createTestItem() {
|
||||
return randomFieldSortBuilder();
|
||||
}
|
||||
|
||||
public static FieldSortBuilder randomFieldSortBuilder() {
|
||||
String fieldName = rarely() ? SortParseElement.DOC_FIELD_NAME : randomAsciiOfLengthBetween(1, 10);
|
||||
FieldSortBuilder builder = new FieldSortBuilder(fieldName);
|
||||
if (randomBoolean()) {
|
||||
builder.order(RandomSortDataGenerator.order(builder.order()));
|
||||
builder.order(RandomSortDataGenerator.order(null));
|
||||
}
|
||||
|
||||
if (randomBoolean()) {
|
||||
|
|
|
@ -38,6 +38,10 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
|
|||
|
||||
@Override
|
||||
protected GeoDistanceSortBuilder createTestItem() {
|
||||
return randomGeoDistanceSortBuilder();
|
||||
}
|
||||
|
||||
public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {
|
||||
String fieldName = randomAsciiOfLengthBetween(1, 10);
|
||||
GeoDistanceSortBuilder result = null;
|
||||
|
||||
|
@ -70,7 +74,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
|
|||
result.unit(unit(result.unit()));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
result.order(RandomSortDataGenerator.order(result.order()));
|
||||
result.order(RandomSortDataGenerator.order(null));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
result.sortMode(mode(result.sortMode()));
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.index.query.IdsQueryBuilder;
|
||||
import org.elasticsearch.index.query.MatchAllQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
|
@ -79,15 +79,10 @@ public class RandomSortDataGenerator {
|
|||
|
||||
public static Object missing(Object original) {
|
||||
Object missing = null;
|
||||
Object otherMissing = null;
|
||||
if (original instanceof BytesRef) {
|
||||
otherMissing = ((BytesRef) original).utf8ToString();
|
||||
} else {
|
||||
otherMissing = original;
|
||||
}
|
||||
Object otherMissing = original;
|
||||
|
||||
while (missing == null || missing.equals(otherMissing)) {
|
||||
int missingId = ESTestCase.randomIntBetween(0, 3);
|
||||
int missingId = ESTestCase.randomIntBetween(0, 4);
|
||||
switch (missingId) {
|
||||
case 0:
|
||||
missing = ("_last");
|
||||
|
@ -99,6 +94,9 @@ public class RandomSortDataGenerator {
|
|||
missing = ESTestCase.randomAsciiOfLength(10);
|
||||
break;
|
||||
case 3:
|
||||
missing = ESTestCase.randomUnicodeOfCodepointLengthBetween(5, 15);
|
||||
break;
|
||||
case 4:
|
||||
missing = ESTestCase.randomInt();
|
||||
break;
|
||||
default:
|
||||
|
@ -109,9 +107,14 @@ public class RandomSortDataGenerator {
|
|||
return missing;
|
||||
}
|
||||
|
||||
public static SortOrder order(SortOrder original) {
|
||||
SortOrder order = SortOrder.ASC;
|
||||
if (order.equals(original)) {
|
||||
/**
|
||||
* return a random {@link SortOrder} settings, except the one provided by parameter if set
|
||||
*/
|
||||
public static SortOrder order(@Nullable SortOrder original) {
|
||||
if (original == null) {
|
||||
return ESTestCase.randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
|
||||
}
|
||||
if (original.equals(SortOrder.ASC)) {
|
||||
return SortOrder.DESC;
|
||||
} else {
|
||||
return SortOrder.ASC;
|
||||
|
|
|
@ -34,17 +34,17 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder
|
|||
|
||||
@Override
|
||||
protected ScoreSortBuilder createTestItem() {
|
||||
return randomScoreSortBuilder();
|
||||
}
|
||||
|
||||
public static ScoreSortBuilder randomScoreSortBuilder() {
|
||||
return new ScoreSortBuilder().order(randomBoolean() ? SortOrder.ASC : SortOrder.DESC);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScoreSortBuilder mutate(ScoreSortBuilder original) throws IOException {
|
||||
ScoreSortBuilder result = new ScoreSortBuilder();
|
||||
if (original.order() == SortOrder.ASC) {
|
||||
result.order(SortOrder.DESC);
|
||||
} else {
|
||||
result.order(SortOrder.ASC);
|
||||
}
|
||||
result.order(RandomSortDataGenerator.order(original.order()));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -40,11 +40,15 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
|||
|
||||
@Override
|
||||
protected ScriptSortBuilder createTestItem() {
|
||||
return randomScriptSortBuilder();
|
||||
}
|
||||
|
||||
public static ScriptSortBuilder randomScriptSortBuilder() {
|
||||
ScriptSortType type = randomBoolean() ? ScriptSortType.NUMBER : ScriptSortType.STRING;
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(new Script(randomAsciiOfLengthBetween(5, 10)),
|
||||
type);
|
||||
if (randomBoolean()) {
|
||||
builder.order(RandomSortDataGenerator.order(builder.order()));
|
||||
builder.order(RandomSortDataGenerator.order(null));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
if (type == ScriptSortType.NUMBER) {
|
||||
|
|
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
x * Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.geo.GeoPoint;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.search.SearchModule;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class SortBuilderTests extends ESTestCase {
|
||||
|
||||
private static final int NUMBER_OF_RUNS = 20;
|
||||
|
||||
protected static NamedWriteableRegistry namedWriteableRegistry;
|
||||
|
||||
static IndicesQueriesRegistry indicesQueriesRegistry;
|
||||
|
||||
SortParseElement parseElement = new SortParseElement();
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
namedWriteableRegistry = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* test two syntax variations:
|
||||
* - "sort" : "fieldname"
|
||||
* - "sort" : { "fieldname" : "asc" }
|
||||
*/
|
||||
public void testSingleFieldSort() throws IOException {
|
||||
SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
|
||||
String json = "{ \"sort\" : { \"field1\" : \"" + order + "\" }}";
|
||||
List<SortBuilder<?>> result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
SortBuilder<?> sortBuilder = result.get(0);
|
||||
assertEquals(new FieldSortBuilder("field1").order(order), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : \"field1\" }";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new FieldSortBuilder("field1"), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : { \"_doc\" : \"" + order + "\" }}";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new FieldSortBuilder("_doc").order(order), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : \"_doc\" }";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new FieldSortBuilder("_doc"), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : { \"_score\" : \"" + order +"\" }}";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new ScoreSortBuilder().order(order), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : \"_score\" }";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new ScoreSortBuilder(), sortBuilder);
|
||||
|
||||
// test two spellings for _geo_disctance
|
||||
json = "{ \"sort\" : ["
|
||||
+ "{\"_geoDistance\" : {"
|
||||
+ "\"pin.location\" : \"40,-70\" } }"
|
||||
+ "] }";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new GeoDistanceSortBuilder("pin.location", 40, -70), sortBuilder);
|
||||
|
||||
json = "{ \"sort\" : ["
|
||||
+ "{\"_geo_distance\" : {"
|
||||
+ "\"pin.location\" : \"40,-70\" } }"
|
||||
+ "] }";
|
||||
result = parseSort(json);
|
||||
assertEquals(1, result.size());
|
||||
sortBuilder = result.get(0);
|
||||
assertEquals(new GeoDistanceSortBuilder("pin.location", 40, -70), sortBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* test random syntax variations
|
||||
*/
|
||||
public void testRandomSortBuilders() throws IOException {
|
||||
for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) {
|
||||
List<SortBuilder<?>> testBuilders = randomSortBuilderList();
|
||||
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
|
||||
xContentBuilder.startObject();
|
||||
if (testBuilders.size() > 1) {
|
||||
xContentBuilder.startArray("sort");
|
||||
} else {
|
||||
xContentBuilder.field("sort");
|
||||
}
|
||||
for (SortBuilder<?> builder : testBuilders) {
|
||||
if (builder instanceof ScoreSortBuilder || builder instanceof FieldSortBuilder) {
|
||||
switch (randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
if (builder instanceof ScoreSortBuilder) {
|
||||
xContentBuilder.value("_score");
|
||||
} else {
|
||||
xContentBuilder.value(((FieldSortBuilder) builder).getFieldName());
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
xContentBuilder.startObject();
|
||||
if (builder instanceof ScoreSortBuilder) {
|
||||
xContentBuilder.field("_score");
|
||||
} else {
|
||||
xContentBuilder.field(((FieldSortBuilder) builder).getFieldName());
|
||||
}
|
||||
xContentBuilder.value(builder.order());
|
||||
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) {
|
||||
xContentBuilder.endArray();
|
||||
}
|
||||
xContentBuilder.endObject();
|
||||
List<SortBuilder<?>> parsedSort = parseSort(xContentBuilder.string());
|
||||
assertEquals(testBuilders.size(), parsedSort.size());
|
||||
Iterator<SortBuilder<?>> iterator = testBuilders.iterator();
|
||||
for (SortBuilder<?> parsedBuilder : parsedSort) {
|
||||
assertEquals(iterator.next(), parsedBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<SortBuilder<?>> randomSortBuilderList() {
|
||||
int size = randomIntBetween(1, 5);
|
||||
List<SortBuilder<?>> list = new ArrayList<>(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
switch (randomIntBetween(0, 3)) {
|
||||
case 0:
|
||||
list.add(new ScoreSortBuilder());
|
||||
break;
|
||||
case 1:
|
||||
String fieldName = rarely() ? SortParseElement.DOC_FIELD_NAME : randomAsciiOfLengthBetween(1, 10);
|
||||
list.add(new FieldSortBuilder(fieldName));
|
||||
break;
|
||||
case 2:
|
||||
list.add(GeoDistanceSortBuilderTests.randomGeoDistanceSortBuilder());
|
||||
break;
|
||||
case 3:
|
||||
list.add(ScriptSortBuilderTests.randomScriptSortBuilder());
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("unexpected randomization in tests");
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* test array syntax variations:
|
||||
* - "sort" : [ "fieldname", { "fieldname2" : "asc" }, ...]
|
||||
*/
|
||||
public void testMultiFieldSort() throws IOException {
|
||||
String json = "{ \"sort\" : ["
|
||||
+ "{ \"post_date\" : {\"order\" : \"asc\"}},"
|
||||
+ "\"user\","
|
||||
+ "{ \"name\" : \"desc\" },"
|
||||
+ "{ \"age\" : \"desc\" },"
|
||||
+ "{"
|
||||
+ "\"_geo_distance\" : {"
|
||||
+ "\"pin.location\" : \"40,-70\" } },"
|
||||
+ "\"_score\""
|
||||
+ "] }";
|
||||
List<SortBuilder<?>> result = parseSort(json);
|
||||
assertEquals(6, result.size());
|
||||
assertEquals(new FieldSortBuilder("post_date").order(SortOrder.ASC), result.get(0));
|
||||
assertEquals(new FieldSortBuilder("user").order(SortOrder.ASC), result.get(1));
|
||||
assertEquals(new FieldSortBuilder("name").order(SortOrder.DESC), result.get(2));
|
||||
assertEquals(new FieldSortBuilder("age").order(SortOrder.DESC), result.get(3));
|
||||
assertEquals(new GeoDistanceSortBuilder("pin.location", new GeoPoint(40, -70)), result.get(4));
|
||||
assertEquals(new ScoreSortBuilder(), result.get(5));
|
||||
}
|
||||
|
||||
private static List<SortBuilder<?>> parseSort(String jsonString) throws IOException {
|
||||
XContentParser itemParser = XContentHelper.createParser(new BytesArray(jsonString));
|
||||
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
|
||||
context.reset(itemParser);
|
||||
|
||||
assertEquals(XContentParser.Token.START_OBJECT, itemParser.nextToken());
|
||||
assertEquals(XContentParser.Token.FIELD_NAME, itemParser.nextToken());
|
||||
assertEquals("sort", itemParser.currentName());
|
||||
itemParser.nextToken();
|
||||
return SortBuilder.fromXContent(context);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue