Sort
# EE built-in sorting
For sorting fields, EE provides some built-in out-of-the-box APIs to support ascending and descending sorting:
// Sorting in descending order
wrapper.orderByDesc(sort field, supports multiple fields)
// ascending order
wrapper.orderByAsc(sort field, supports multiple fields)
// sort by score (this function is supported in version 0.9.7+; default descending order when SortOrder is not specified, high score is in front, ascending/descending order is supported)
wrapper.sortByScore(boolean condition, SortOrder sortOrder)
// sorting input from the front-end, string format, somewhat similar to the previous MySQL kind
wrapper.orderBy(boolean condition, OrderByParam orderByParam);
// The order input is passed in by the front-end, in a multi-field case
wrapper.orderBy(boolean condition, List<OrderByParam> orderByParams);
// Order by distance from near to far
wrapper.orderByDistanceAsc(boolean condition, R column, Geopoint... .geoPoints);
// Order 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
Usage example:
@Test
public void testSort(){
// testSort To test sorting, we add a new creation time field to the Document object, update the index, and add two new 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(){
// Test sorting by score in ascending order (lower scores come first)
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent, "technical");
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, sorted according to the title descending order, according to the content ascending order
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() {
// given the center point, query the data within 168.8km of the center point, and order them according to the distance from the center point, in descending order
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
GeoPoint centerPoint = new GeoPoint(41.0, 116.0);
wrapper.match(Document::getCreator, "old 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
12
@Test
public void testOrderByDistanceAsc() {
// given the center point, query the data within 168.8km of the center point, and sort by distance from the center point
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
GeoPoint centerPoint = new GeoPoint(41.0, 116.0);
wrapper.match(Document::getCreator, "old 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 sorters are rich and varied and flexible enough that it is hard to simplify all of them with a fixed set of schemes. Therefore, we provide out-of-the-box support for these high-frequency sorts, and for other low-frequency sorts, delegating the sort builder directly to the user through customization is definitely a better solution. As we continue to iterate and incorporate 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
Usage examples:
@Test
public void testSort(){
// Test complex sorting, SortBuilder has many subclasses, here is only a demonstration, for example, a user needs to get data randomly
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent, "technical");
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
SortBuilder class has many subclasses and is very flexible, so it can support and cover enough sorting scenarios, and various other types of queries, if you encounter in the use of the process, you can refer to the above example to write