Sort
# EE built-in sorting
For field sorting, EE provides some built-in out-of-the-box APIs to support ascending and descending sorting:
// descending sort
wrapper.orderByDesc (sort field, supports multiple fields)
// Ascending
wrapper.orderByAsc (sort field, supports multiple fields)
// Sort according to the score (this function is supported in version 0.9.7+; the default descending order when SortOrder is not specified, the highest score is first, and ascending/descending order is supported)
wrapper.sortByScore(boolean condition, SortOrder sortOrder)
// The sorting input is passed in from the front end, and the string format is similar to that of MySQL before.
wrapper.orderBy(boolean condition, OrderByParam orderByParam);
// Sort input parameters are passed in from the front end, multi-field situation
wrapper.orderBy(boolean condition, List<OrderByParam> orderByParams);
// Sort by distance from near to far
wrapper.orderByDistanceAsc(boolean condition, R column, Geopoint...geoPoints);
// Sort by distance from far to near
wrapper.orderByDistanceDesc(boolean condition, R column, Geopoint...geoPoints);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Example of use:
@Test
public void testSort(){
// Test sorting In order to test sorting, we added a creation time field to the Document object, updated the index, and added two pieces of data
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.likeRight(Document::getContent,"Push");
wrapper.select(Document::getTitle,Document::getGmtCreate);
List<Document> before = documentMapper.selectList(wrapper);
System.out.println("before:"+before);
wrapper.orderByDesc(Document::getGmtCreate);
List<Document> desc = documentMapper.selectList(wrapper);
System.out.println("desc:"+desc);
}
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testSortByScore(){
// Tests are sorted by score in ascending order (low score first)
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent,"Technology");
wrapper.sortByScore(SortOrder.ASC);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
@Test
public void testOrderByParams(){
// Here the simulation parameters are passed in by the front end through the xxQuery class, the sorting is descending according to the title, and ascending according to the content
String jsonParam = "[{\"order\":\"title\",\"sort\":\"DESC\"},{\"order\":\"creator\",\"sort\": \"ASC\"}]";
List<OrderByParam> orderByParams = JSON.parseArray(jsonParam, OrderByParam.class);
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent,"Technology")
.orderBy(orderByParams);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
10
11
@Test
public void testOrderByDistanceAsc() {
// Test the given center point, query the data within 168.8km of the center point, and sort them according to the distance from the center point from near to far
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
GeoPoint centerPoint = new GeoPoint(41.0, 116.0);
wrapper.match(Document::getCreator, "man")
.geoDistance(Document::getLocation, 168.8, centerPoint)
.orderByDistanceAsc(Document::getLocation, centerPoint);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
10
11
@Test
public void testOrderByDistanceAsc() {
// Test the given center point, query the data within 168.8km of the center point, and sort by distance from the center point from far to near
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
GeoPoint centerPoint = new GeoPoint(41.0, 116.0);
wrapper.match(Document::getCreator, "man")
.geoDistance(Document::getLocation, 168.8, centerPoint)
.orderByDistanceDesc(Document::getLocation, centerPoint);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
10
11
Effect:
# custom sorting
background
ES's sorter is very rich and varied, and it is flexible enough that it is difficult for us to simplify all sorting with a fixed solution. Therefore, we provide out-of-the-box support for the above high-frequency sorting. For other low-frequency sorting, it is undoubtedly a better solution to entrust the sorting builder directly to the user in a custom way. It can support 100% of all the query functions provided by ES without using native queries and hybrid queries. And as we continue to iterate and absorb user feedback, we will continue to provide more out-of-the-box API support in the near future, so stay tuned.
// API
wrapper.sort(boolean condition, SortBuilder<?> sortBuilder)
2
Example of use:
@Test
public void testSort(){
// To test complex sorting, there are many subclasses of SortBuilder, only one of them is demonstrated here. For example, some users propose to obtain data randomly
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent,"Technology");
Script script = new Script("Math.random()");
ScriptSortBuilder scriptSortBuilder = new ScriptSortBuilder(script, ScriptSortBuilder.ScriptSortType.NUMBER);
wrapper.sort(scriptSortBuilder);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
10
11
There are many subclasses of the SortBuilder class and are very flexible, so there are enough sorting scenarios that can be supported and covered. For other types of queries, if you encounter them during use, you can refer to the above examples to write