且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Go Olivere/Elastic基于多个字段进行排序

更新时间:2023-09-04 19:30:58

从示例中可以看到,您在示例中使用的 SortBy 函数是可变参数,如下所示: SortBy(sorter ... Sorter)* SearchService .

因此,您只需使用两个过滤条件就可以调用它一次:

So you only need to call it once with both your filter conditions:

    sortQuery1 := elastic.NewFieldSort("name")
    sortQuery2 := elastic.NewFieldSort("age").Desc()

    searchService := client.Search().
        Index("students").
        SortBy(sortQuery1, sortQuery2)

此请求正文一旦编组为JSON,将如下所示:

Once this request body is marshalled to JSON, it will look like the following:

{
    "sort": [
        { "name": { "order": "asc" } },
        { "age": { "order": "desc" } }
    ]
}