Merge pull request '(FIX): Fixed issues with search.' (#47) from feature/search-fixes into master
Some checks failed
Deploy application with Docker / build_and_deploy (push) Failing after 1m39s

Reviewed-on: #47
This commit is contained in:
Hayden Hargreaves 2025-09-19 13:39:26 -07:00
commit 6e96c847ec

View File

@ -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)