mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 18:35:25 +00:00
Remove SortBuilderParser interface, using abstract methods in SortBuilder
This commit is contained in:
parent
64d362ab9d
commit
2bb57b6f2c
@ -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,7 +60,7 @@ 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;
|
||||
|
@ -36,7 +36,7 @@ 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> {
|
||||
|
||||
public static final String NAME = "_score";
|
||||
static final ScoreSortBuilder PROTOTYPE = new ScoreSortBuilder();
|
||||
|
@ -64,7 +64,7 @@ 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> {
|
||||
|
||||
public static final String NAME = "_script";
|
||||
static final ScriptSortBuilder PROTOTYPE = new ScriptSortBuilder(new Script("_na_"), ScriptSortType.STRING);
|
||||
|
@ -20,15 +20,13 @@
|
||||
package org.elasticsearch.search.sort;
|
||||
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.Sort;
|
||||
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;
|
||||
@ -36,7 +34,6 @@ 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 org.elasticsearch.search.internal.SearchContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -44,14 +41,13 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Collections.unmodifiableMap;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuilderParser<T>, 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");
|
||||
@ -67,17 +63,26 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
PARSERS = unmodifiableMap(parsers);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
@ -106,11 +111,7 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
parseCompoundSortField(parser, context, sortFields);
|
||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||
String fieldName = parser.text();
|
||||
if (fieldName.equals(ScoreSortBuilder.NAME)) {
|
||||
sortFields.add(new ScoreSortBuilder());
|
||||
} else {
|
||||
sortFields.add(new FieldSortBuilder(fieldName));
|
||||
}
|
||||
sortFields.add(fieldOrScoreSort(fieldName));
|
||||
} else {
|
||||
throw new IllegalArgumentException("malformed sort format, "
|
||||
+ "within the sort array, an object, or an actual string are allowed");
|
||||
@ -118,11 +119,7 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
}
|
||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||
String fieldName = parser.text();
|
||||
if (fieldName.equals(ScoreSortBuilder.NAME)) {
|
||||
sortFields.add(new ScoreSortBuilder());
|
||||
} else {
|
||||
sortFields.add(new FieldSortBuilder(fieldName));
|
||||
}
|
||||
sortFields.add(fieldOrScoreSort(fieldName));
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
parseCompoundSortField(parser, context, sortFields);
|
||||
} else {
|
||||
@ -131,6 +128,14 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
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;
|
||||
@ -140,11 +145,7 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
token = parser.nextToken();
|
||||
if (token == XContentParser.Token.VALUE_STRING) {
|
||||
SortOrder order = SortOrder.fromString(parser.text());
|
||||
if (fieldName.equals(ScoreSortBuilder.NAME)) {
|
||||
sortFields.add(new ScoreSortBuilder().order(order));
|
||||
} else {
|
||||
sortFields.add(new FieldSortBuilder(fieldName).order(order));
|
||||
}
|
||||
sortFields.add(fieldOrScoreSort(fieldName).order(order));
|
||||
} else {
|
||||
if (PARSERS.containsKey(fieldName)) {
|
||||
sortFields.add(PARSERS.get(fieldName).fromXContent(context, fieldName));
|
||||
@ -156,40 +157,6 @@ public abstract class SortBuilder<T extends SortBuilder<?>> implements SortBuild
|
||||
}
|
||||
}
|
||||
|
||||
public static void parseSort(XContentParser parser, SearchContext context) throws IOException {
|
||||
QueryParseContext parseContext = context.getQueryShardContext().parseContext();
|
||||
parseContext.reset(parser);
|
||||
Optional<Sort> sortOptional = buildSort(SortBuilder.fromXContent(parseContext), context.getQueryShardContext());
|
||||
if (sortOptional.isPresent()) {
|
||||
context.sort(sortOptional.get());
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<Sort> buildSort(List<SortBuilder<?>> sortBuilders, QueryShardContext context) throws IOException {
|
||||
List<SortField> sortFields = new ArrayList<>(sortBuilders.size());
|
||||
for (SortBuilder<?> builder : sortBuilders) {
|
||||
sortFields.add(builder.build(context));
|
||||
}
|
||||
if (!sortFields.isEmpty()) {
|
||||
// optimize if we just sort on score non reversed, we don't really need sorting
|
||||
boolean sort;
|
||||
if (sortFields.size() > 1) {
|
||||
sort = true;
|
||||
} else {
|
||||
SortField sortField = sortFields.get(0);
|
||||
if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
|
||||
sort = false;
|
||||
} else {
|
||||
sort = true;
|
||||
}
|
||||
}
|
||||
if (sort) {
|
||||
return Optional.of(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder<?> nestedFilter) throws IOException {
|
||||
Nested nested = null;
|
||||
if (nestedPath != null) {
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user