Skip to content

Commit

Permalink
Allow for the sort search element to speciy the sort fields in an arr…
Browse files Browse the repository at this point in the history
…ay, closes #30.
  • Loading branch information
kimchy committed Feb 20, 2010
1 parent 3f045de commit 1aa8e01
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ private String parseSearchSource(RestRequest request) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.util.gnu.trove.TObjectIntHashMap;
import org.elasticsearch.util.trove.ExtTObjectIntHasMap;

import java.io.IOException;
import java.util.List;

/**
Expand All @@ -57,8 +58,26 @@ public SortParseElement() {
}

@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();
Expand All @@ -85,36 +104,37 @@ public SortParseElement() {
}
}
}
if ("score".equals(fieldName)) {
if (reverse) {
sortFields.add(SORT_SCORE_REVERSE);
} else {
sortFields.add(SORT_SCORE);
}
} else if ("doc".equals(fieldName)) {
if (reverse) {
sortFields.add(SORT_DOC_REVERSE);
} else {
sortFields.add(SORT_DOC);
}
} else {
FieldMappers fieldMappers = context.mapperService().smartNameFieldMappers(fieldName);
if (fieldMappers == null || fieldMappers.mappers().isEmpty()) {
if (type == -1) {
throw new SearchParseException("No built in mapping found for [" + fieldName + "], and no explicit type defined");
}
} else {
fieldName = fieldMappers.mappers().get(0).indexName();
if (type == -1) {
type = fieldMappers.mappers().get(0).sortType();
}
}
sortFields.add(new SortField(fieldName, type, reverse));
}
addSortField(context, sortFields, fieldName, reverse, type);
}
}
if (!sortFields.isEmpty()) {
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
}

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);
} else {
sortFields.add(SORT_SCORE);
}
} else if ("doc".equals(fieldName)) {
if (reverse) {
sortFields.add(SORT_DOC_REVERSE);
} else {
sortFields.add(SORT_DOC);
}
} else {
FieldMappers fieldMappers = context.mapperService().smartNameFieldMappers(fieldName);
if (fieldMappers == null || fieldMappers.mappers().isEmpty()) {
if (type == -1) {
throw new SearchParseException("No built in mapping found for [" + fieldName + "], and no explicit type defined");
}
} else {
fieldName = fieldMappers.mappers().get(0).indexName();
if (type == -1) {
type = fieldMappers.mappers().get(0).sortType();
}
}
sortFields.add(new SortField(fieldName, type, reverse));
}
}
}

0 comments on commit 1aa8e01

Please sign in to comment.