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.md
    • Applicable scene
    • Worry free
    • Avoid pit
    • Quick start
    • Springboot demo
    • Spring Integration Guide
    • Solon Integration Guide
    • Config
    • Annotation
  • Core function

    • Condition constructor

      • Introduction of Wrapper
      • Index wrapper
      • Select wrapper
      • Update wrapper
    • Index CRUD

      • Index hosting model
      • Index CRUD
    • Data CRUD

      • Data synchronization solutions
      • Data CRUD
    • Multiple data sources support
    • Dynamic indexing support
    • Four nested query
    • Chain call
  • Extended function

    • Mixed query
    • Origin query
    • Page
    • Nested
    • Join parent child
    • Get DSL
    • ExecuteDSL
    • Execute SQL
    • Custom RequestOptions
    • Custom default method
  • High-level syntax

    • Field filtering
    • Sort
      • EE built-in sorting
      • Custom sorting
    • Aggregation
    • Match&fuzzy match
    • Weight
    • Highlight
    • Geo
    • IP
  • Plugin

    • Plugin
  • Other

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

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

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

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

Effect : image.png

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

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
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式