REST Search API: Change `score` to `_score` to denote sorting by hit score, closes #271.

This commit is contained in:
kimchy 2010-07-22 23:39:55 +03:00
parent b1c0a78c8e
commit 2a3130c649
2 changed files with 9 additions and 5 deletions

View File

@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder; import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
import org.elasticsearch.search.facets.AbstractFacetBuilder; import org.elasticsearch.search.facets.AbstractFacetBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder; import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.query.SortParseElement;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -147,7 +148,7 @@ public class SearchSourceBuilder implements ToXContent {
*/ */
public SearchSourceBuilder sort(String name, Order order) { public SearchSourceBuilder sort(String name, Order order) {
boolean reverse = false; boolean reverse = false;
if (name.equals("score")) { if (name.equals(SortParseElement.SCORE_FIELD_NAME)) {
if (order == Order.ASC) { if (order == Order.ASC) {
reverse = true; reverse = true;
} }

View File

@ -41,6 +41,9 @@ public class SortParseElement implements SearchParseElement {
private static final SortField SORT_DOC = new SortField(null, SortField.DOC); private static final SortField SORT_DOC = new SortField(null, SortField.DOC);
private static final SortField SORT_DOC_REVERSE = new SortField(null, SortField.DOC, true); private static final SortField SORT_DOC_REVERSE = new SortField(null, SortField.DOC, true);
public static final String SCORE_FIELD_NAME = "_score";
public static final String DOC_FIELD_NAME = "_doc";
public SortParseElement() { public SortParseElement() {
} }
@ -74,9 +77,9 @@ public class SortParseElement implements SearchParseElement {
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
String direction = parser.text(); String direction = parser.text();
if (direction.equals("asc")) { if (direction.equals("asc")) {
reverse = "score".equals(fieldName); reverse = SCORE_FIELD_NAME.equals(fieldName);
} else if (direction.equals("desc")) { } else if (direction.equals("desc")) {
reverse = !"score".equals(fieldName); reverse = !SCORE_FIELD_NAME.equals(fieldName);
} }
} else { } else {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@ -95,13 +98,13 @@ public class SortParseElement implements SearchParseElement {
} }
private void addSortField(SearchContext context, List<SortField> sortFields, String fieldName, boolean reverse) { private void addSortField(SearchContext context, List<SortField> sortFields, String fieldName, boolean reverse) {
if ("score".equals(fieldName)) { if (SCORE_FIELD_NAME.equals(fieldName)) {
if (reverse) { if (reverse) {
sortFields.add(SORT_SCORE_REVERSE); sortFields.add(SORT_SCORE_REVERSE);
} else { } else {
sortFields.add(SORT_SCORE); sortFields.add(SORT_SCORE);
} }
} else if ("doc".equals(fieldName)) { } else if (DOC_FIELD_NAME.equals(fieldName)) {
if (reverse) { if (reverse) {
sortFields.add(SORT_DOC_REVERSE); sortFields.add(SORT_DOC_REVERSE);
} else { } else {