mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
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)
This commit is contained in:
1 parent
a36f74edef
commit
875f82bdcc
2 files changed
+19
-3
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
Reference in new issue
Block a user