From e56b2232bb0b8dc1d1320f3f0a4c0a936dfc4a20 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 5 Jun 2024 10:24:28 +0200 Subject: [PATCH] fix(typesense): do not try to sort by position when searching in a saved filter This change fixes a bug where Typesense would try to sort by the project view of a saved filter. The view position is not indexed in Typesense, hence filtering fails. Because sorting by position is not a feature in saved filters, I've removed the logic for sorting saved filters with Typesense. --- pkg/models/task_collection.go | 1 + pkg/models/task_search.go | 12 ++++++++++-- pkg/models/tasks.go | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index f992195f1..676510164 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -262,6 +262,7 @@ func (tf *TaskCollection) ReadAll(s *xorm.Session, a web.Auth, search string, pa opts.page = page opts.perPage = perPage opts.expand = tf.Expand + opts.isSavedFilter = tf.isSavedFilter if view != nil { var hasOrderByPosition bool diff --git a/pkg/models/task_search.go b/pkg/models/task_search.go index f9fbfc6b2..b7b87b6fd 100644 --- a/pkg/models/task_search.go +++ b/pkg/models/task_search.go @@ -517,7 +517,13 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, } var sortbyFields []string - for i, param := range opts.sortby { + var usedParams int + for _, param := range opts.sortby { + + if opts.isSavedFilter && param.sortBy == taskPropertyPosition { + continue + } + // Validate the params if err := param.validate(); err != nil { return nil, totalCount, err @@ -536,11 +542,13 @@ func (t *typesenseTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, sortbyFields = append(sortbyFields, sortBy+"(missing_values:last):"+param.orderBy.String()) - if i == 2 { + if usedParams == 2 { // Typesense supports up to 3 sorting parameters // https://typesense.org/docs/0.25.0/api/search.html#ranking-and-sorting-parameters break } + + usedParams++ } sortby := strings.Join(sortbyFields, ",") diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 386ed376a..ae21ff855 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -175,6 +175,7 @@ type taskSearchOptions struct { filterIncludeNulls bool filter string filterTimezone string + isSavedFilter bool projectIDs []int64 expand TaskCollectionExpandable }