mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-27 18:38:41 +00:00
Allow for the sort search element to speciy the sort fields in an array, closes #30.
This commit is contained in:
parent
3f045dee1f
commit
1aa8e01184
modules/elasticsearch/src/main/java/org/elasticsearch
@ -206,10 +206,7 @@ public class RestSearchAction extends BaseRestHandler {
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(queryBuilder);
|
||||
|
||||
searchSourceBuilder.queryParserName(request.param("queryParserName"));
|
||||
String explain = request.param("explain");
|
||||
if (explain != null) {
|
||||
searchSourceBuilder.explain(Boolean.parseBoolean(explain));
|
||||
}
|
||||
searchSourceBuilder.explain(request.paramAsBoolean("explain", false));
|
||||
|
||||
List<String> fields = request.params("field");
|
||||
if (fields != null && !fields.isEmpty()) {
|
||||
|
@ -31,6 +31,7 @@ import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.util.gnu.trove.TObjectIntHashMap;
|
||||
import org.elasticsearch.util.trove.ExtTObjectIntHasMap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -57,8 +58,26 @@ public class SortParseElement implements SearchParseElement {
|
||||
}
|
||||
|
||||
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
|
||||
JsonToken token;
|
||||
JsonToken token = jp.getCurrentToken();
|
||||
List<SortField> sortFields = Lists.newArrayListWithCapacity(2);
|
||||
if (token == JsonToken.START_ARRAY) {
|
||||
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
|
||||
if (token == JsonToken.START_OBJECT) {
|
||||
addCompoundSortField(jp, context, sortFields);
|
||||
} else if (token == JsonToken.VALUE_STRING) {
|
||||
addSortField(context, sortFields, jp.getText(), false, -1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
addCompoundSortField(jp, context, sortFields);
|
||||
}
|
||||
if (!sortFields.isEmpty()) {
|
||||
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
|
||||
}
|
||||
}
|
||||
|
||||
private void addCompoundSortField(JsonParser jp, SearchContext context, List<SortField> sortFields) throws IOException {
|
||||
JsonToken token;
|
||||
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
|
||||
if (token == JsonToken.FIELD_NAME) {
|
||||
String fieldName = jp.getCurrentName();
|
||||
@ -85,6 +104,12 @@ public class SortParseElement implements SearchParseElement {
|
||||
}
|
||||
}
|
||||
}
|
||||
addSortField(context, sortFields, fieldName, reverse, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addSortField(SearchContext context, List<SortField> sortFields, String fieldName, boolean reverse, int type) {
|
||||
if ("score".equals(fieldName)) {
|
||||
if (reverse) {
|
||||
sortFields.add(SORT_SCORE_REVERSE);
|
||||
@ -113,8 +138,3 @@ public class SortParseElement implements SearchParseElement {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!sortFields.isEmpty()) {
|
||||
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user