如何在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
相关产品推荐
相关产品推荐

