add sort with Order enum
This commit is contained in:
parent
f10a2d428d
commit
442e0e87b0
|
@ -33,7 +33,7 @@ import static com.google.common.collect.Lists.*;
|
||||||
import static org.elasticsearch.util.json.JsonBuilder.*;
|
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()}.
|
* using {@link org.elasticsearch.search.builder.SearchSourceBuilder#searchSource()}.
|
||||||
*
|
*
|
||||||
* @author kimchy (shay.banon)
|
* @author kimchy (shay.banon)
|
||||||
|
@ -41,6 +41,11 @@ import static org.elasticsearch.util.json.JsonBuilder.*;
|
||||||
*/
|
*/
|
||||||
public class SearchSourceBuilder {
|
public class SearchSourceBuilder {
|
||||||
|
|
||||||
|
public static enum Order {
|
||||||
|
ASC,
|
||||||
|
DESC
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A static factory method to construct a new search source.
|
* A static factory method to construct a new search source.
|
||||||
*/
|
*/
|
||||||
|
@ -132,6 +137,26 @@ public class SearchSourceBuilder {
|
||||||
return this;
|
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.
|
* Add a sort against the given field name and if it should be revered or not.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue