From 875f82bdcc11e7aebb4a07af0204041bcc1034a0 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Wed, 12 Aug 2026 16:45:06 +0200 Subject: [PATCH] feat(search): native order_by sorting in the opensearch backend Name is analyzed (lowercaseKeyword) and therefore a text field, which OpenSearch refuses to sort on without fielddata; enable it in the index template. The keyword tokenizer emits one term per document, so the fielddata cache stays small. The schema version has not shipped yet, so no reindex is needed. (cherry picked from commit f1d0e7b773b170b119dbd59fa176d656237bed96) --- services/search/pkg/opensearch/backend.go | 21 ++++++++++++++++--- .../pkg/opensearch/internal/osu/request.go | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/services/search/pkg/opensearch/backend.go b/services/search/pkg/opensearch/backend.go index 9eda0976c3..2d55d0548b 100644 --- a/services/search/pkg/opensearch/backend.go +++ b/services/search/pkg/opensearch/backend.go @@ -126,9 +126,7 @@ func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.I searchParams := opensearchgoAPI.SearchParams{ SourceExcludes: []string{"Content"}, // Do not send back the full content in the search response, as it is only needed for highlighting and can be large. The highlighted snippets will be sent back in the response instead. - // ties by id, like the cross-space merge - Sort: []string{"_score:desc", "ID:asc"}, - TrackScores: conversions.ToPointer(true), + TrackScores: conversions.ToPointer(true), // count every match, the default stops at 10000 TrackTotalHits: true, Size: conversions.ToPointer(search.EnginePageSize(sir.PageSize, 1000)), @@ -139,6 +137,22 @@ func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.I searchParams.Preference = "user-" + u.GetId().GetOpaqueId() } + // order_by first, missing values last in both directions, then like the + // cross-space merge: score, ties by id + sortClause := make([]map[string]any, 0, len(sir.OrderBy)+2) + for _, sp := range sir.OrderBy { + field, ok := search.SortIndexField(sp.GetName()) + if !ok { + return nil, errtypes.BadRequest(fmt.Sprintf("field %q is not sortable", sp.GetName())) + } + order := "asc" + if sp.GetIsDescending() { + order = "desc" + } + sortClause = append(sortClause, map[string]any{field: map[string]any{"order": order, "missing": "_last"}}) + } + sortClause = append(sortClause, map[string]any{"_score": map[string]any{"order": "desc"}}, map[string]any{"ID": map[string]any{"order": "asc"}}) + aggregationsBody, err := aggs.Build(sir.Aggregations) if err != nil { return nil, errtypes.BadRequest(err.Error()) @@ -164,6 +178,7 @@ func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.I }, }, Aggs: aggregationsBody, + Sort: sortClause, }, ) if err != nil { diff --git a/services/search/pkg/opensearch/internal/osu/request.go b/services/search/pkg/opensearch/internal/osu/request.go index f6f4e238ee..608773c165 100644 --- a/services/search/pkg/opensearch/internal/osu/request.go +++ b/services/search/pkg/opensearch/internal/osu/request.go @@ -96,6 +96,7 @@ func BuildSearchReq(req *opensearchgoAPI.SearchReq, q Builder, p ...SearchBodyPa type SearchBodyParams struct { Highlight *BodyParamHighlight `json:"highlight,omitempty"` Aggs map[string]any `json:"aggs,omitempty"` + Sort []map[string]any `json:"sort,omitempty"` } //----------------------------------------------------------------------------//