From 445557f37e83a6f69b7fb76e431b4f9fb5036e8b Mon Sep 17 00:00:00 2001 From: Hayden Hargreaves Date: Fri, 19 Sep 2025 13:38:16 -0700 Subject: [PATCH] (FIX): Fixed issues with search. Spaces are now ignored and special characters that are used for the query are not a problem. This can be pushed up for UAT. --- .../database/repository/recipe_repository.go | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/infrastructure/database/repository/recipe_repository.go b/internal/infrastructure/database/repository/recipe_repository.go index 5a4d437..acc793f 100644 --- a/internal/infrastructure/database/repository/recipe_repository.go +++ b/internal/infrastructure/database/repository/recipe_repository.go @@ -369,7 +369,27 @@ func (r *RecipeRepository) SearchRecipes(filters domain.SearchFilters, userId *i // Create search vector query var orderBy string = "" if filters.Search != "" { - vector_query := strings.ReplaceAll(filters.Search, " ", " | ") + spl := strings.Split(filters.Search, " ") + var cleaned []string + + // Use a string replacer, each word in the query will be passed through this + replacer := strings.NewReplacer( + "'", "", + "-", "", + "&", "", + "|", "", + "!", "", + ) + + for i := range len(spl) { + q := strings.TrimSpace(replacer.Replace(spl[i])) + + if q != "" { + cleaned = append(cleaned, q) + } + } + + vector_query := strings.Join(cleaned, " | ") conditions = append( conditions, @@ -377,8 +397,8 @@ func (r *RecipeRepository) SearchRecipes(filters domain.SearchFilters, userId *i ) template := ` - ORDER BY - ts_rank(r.search_vector, to_tsquery('english', '%s')) DESC, + ORDER BY + ts_rank(r.search_vector, to_tsquery('english', '%s')) DESC, ts_rank_cd(r.search_vector, to_tsquery('english', '%s')) DESC ` orderBy = fmt.Sprintf(template, vector_query, vector_query)