diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 1bd8171ffa8..94dc17b6117 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.builder.XContentBuilder; import org.elasticsearch.index.query.xcontent.XContentQueryBuilder; import org.elasticsearch.search.facets.AbstractFacetBuilder; import org.elasticsearch.search.highlight.HighlightBuilder; +import org.elasticsearch.search.query.SortParseElement; import java.io.IOException; import java.util.ArrayList; @@ -147,7 +148,7 @@ public class SearchSourceBuilder implements ToXContent { */ public SearchSourceBuilder sort(String name, Order order) { boolean reverse = false; - if (name.equals("score")) { + if (name.equals(SortParseElement.SCORE_FIELD_NAME)) { if (order == Order.ASC) { reverse = true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/SortParseElement.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/SortParseElement.java index e8328c37a75..ea0ceec2bc1 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/SortParseElement.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/SortParseElement.java @@ -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_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() { } @@ -74,9 +77,9 @@ public class SortParseElement implements SearchParseElement { if (token == XContentParser.Token.VALUE_STRING) { String direction = parser.text(); if (direction.equals("asc")) { - reverse = "score".equals(fieldName); + reverse = SCORE_FIELD_NAME.equals(fieldName); } else if (direction.equals("desc")) { - reverse = !"score".equals(fieldName); + reverse = !SCORE_FIELD_NAME.equals(fieldName); } } else { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { @@ -95,13 +98,13 @@ public class SortParseElement implements SearchParseElement { } private void addSortField(SearchContext context, List sortFields, String fieldName, boolean reverse) { - if ("score".equals(fieldName)) { + if (SCORE_FIELD_NAME.equals(fieldName)) { if (reverse) { sortFields.add(SORT_SCORE_REVERSE); } else { sortFields.add(SORT_SCORE); } - } else if ("doc".equals(fieldName)) { + } else if (DOC_FIELD_NAME.equals(fieldName)) { if (reverse) { sortFields.add(SORT_DOC_REVERSE); } else {