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 aa8f1fdd97f..15c6369c1f8 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 @@ -33,7 +33,7 @@ import static com.google.common.collect.Lists.*; import static org.elasticsearch.util.json.JsonBuilder.*; /** - * A search source builder allowing to easily build search source. Simple consruction + * A search source builder allowing to easily build search source. Simple construction * using {@link org.elasticsearch.search.builder.SearchSourceBuilder#searchSource()}. * * @author kimchy (shay.banon) @@ -41,6 +41,11 @@ import static org.elasticsearch.util.json.JsonBuilder.*; */ public class SearchSourceBuilder { + public static enum Order { + ASC, + DESC + } + /** * A static factory method to construct a new search source. */ @@ -132,6 +137,26 @@ public class SearchSourceBuilder { return this; } + /** + * Adds a sort against the given field name and the sort ordering. + * + * @param name The name of the field + * @param order The sort ordering + */ + public SearchSourceBuilder sort(String name, Order order) { + boolean reverse = false; + if (name.equals("score")) { + if (order == Order.ASC) { + reverse = true; + } + } else { + if (order == Order.DESC) { + reverse = true; + } + } + return sort(name, null, reverse); + } + /** * Add a sort against the given field name and if it should be revered or not. *