Easy-Es Easy-Es
💋Home
  • v2.0.0(current version)
  • What's New

    • What' s New In Easy-Es v2.0.0?
  • history version

    • v1.x.x
  • Upgrade guide

    • Upgrade to 2.x.x instructions
💖Support
  • OS Community
  • Recommend Goods
  • Project PPT (opens new window)
  • Project introduction
  • Project members
  • PR
Join
Customer
  • Doc-Apis (opens new window)
  • Fitness plan automatic generation system (opens new window)
  • Vuepress-theme-vdoing (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
  • 简体中文 (opens new window)
  • English (opens new window)

adv display by random ❤️become sponsor
💋Home
  • v2.0.0(current version)
  • What's New

    • What' s New In Easy-Es v2.0.0?
  • history version

    • v1.x.x
  • Upgrade guide

    • Upgrade to 2.x.x instructions
💖Support
  • OS Community
  • Recommend Goods
  • Project PPT (opens new window)
  • Project introduction
  • Project members
  • PR
Join
Customer
  • Doc-Apis (opens new window)
  • Fitness plan automatic generation system (opens new window)
  • Vuepress-theme-vdoing (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
  • 简体中文 (opens new window)
  • English (opens new window)
  • Quick start

    • Introduction
    • Applicable scene
    • Worry free
    • Avoid pit
    • Quick start
    • Springboot demo
    • Config
    • Annotation
  • Core functions

    • Auto process index
    • CRUD
    • Condition constructor
  • Extended function

    • Mixed query
    • Origin query
    • Page
    • Nested
    • Join Parent child
    • Get DSL
  • High-level syntax

    • Field filtering
    • Sort
      • EE built-in sorting
      • custom sorting
    • Aggregation
    • Match
    • Weight
    • Highlight
    • Geo
  • Plugin

    • Plugin
  • Other

    • Faq
    • Difference from MP
    • MySQL and EE syntax comparison
    • Update log
    • Update plan
    • Copyright
    • Thanks
  • v1.xDoc
  • High-level syntax
LaoHan
2023-03-18
目录

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);

1
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);
    }
1
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);
    }
1
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);
    }
1
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);
    }
1
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);
    }
1
2
3
4
5
6
7
8
9
10
11

Effect: image.png

# 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)
1
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);
    }
1
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

Help us improve this document (opens new window)
Last update: 2024/03/29
Field filtering
Aggregation

← Field filtering Aggregation→

Theme by Vdoing | Copyright © 2021-2025 LaoHan | Zhejiang ICP No. 2022020479 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式