You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在OpenSearch Go客户端的搜索请求中指定排序顺序

OpenSearch Go客户端设置降序排序的方法

要实现按特定字段降序排序,只需在Sort参数的字符串里追加:desc后缀即可。OpenSearch的Sort参数支持字段名:排序方向的格式,其中desc代表降序,asc代表升序(默认排序方向为升序,可省略:asc)。

修改后的代码示例:

import (
    "github.com/opensearch-project/opensearch-go"
    "github.com/opensearch-project/opensearch-go/opensearchapi"
)

// ... 其他业务代码 ...

req := opensearchapi.SearchRequest{
    Index:  []string{index},
    Body:   dbQuery,
    From:   &from,
    Size:   &size,
    Pretty: true,
    // 在目标字段后拼接:desc指定降序排序
    Sort:   []string{sortField + ":desc"},
}

response, err := req.Do(ctx, client)
if err != nil {
    return nil, err
}

defer response.Body.Close()

内容的提问来源于stack exchange,提问作者AbhiK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:54:52