From c6fba2067c84f57142fd8cc6f41e13b37beb1c95 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Fri, 21 Jun 2024 13:03:23 -0600 Subject: [PATCH] Moving to websearch_to_tsquery because it makes more sense and also due to annoying abusers --- admin/routes.go | 2 +- api/graph/schema.resolvers.go | 12 ++++++------ api/loaders/loaders.go | 2 +- core/routes.go | 2 +- helpers.go | 11 ++++++++--- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/admin/routes.go b/admin/routes.go index e472b53..2bb4cca 100644 --- a/admin/routes.go +++ b/admin/routes.go @@ -67,7 +67,7 @@ func (s *Service) Autocomplete(c echo.Context) error { s := links.ParseSearch(org) opts := &database.FilterOptions{ Filter: sq.And{ - sq.Expr(`to_tsvector('simple', o.name || ' ' || o.slug) @@ to_tsquery('simple', ?)`, s), + sq.Expr(`to_tsvector('simple', o.name || ' ' || o.slug) @@ websearch_to_tsquery('simple', ?)`, s), sq.Eq{"o.is_active": true}, }, } diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index f849c38..cc98fea 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -4228,7 +4228,7 @@ func (r *queryResolver) GetUsers(ctx context.Context, input *model.GetUserInput) opts.Filter = sq.And{ opts.Filter, sq.Expr(`to_tsvector('simple', u.full_name || ' ' || u.email || ' ' || o.slug) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } @@ -4373,7 +4373,7 @@ func (r *queryResolver) GetOrganizations(ctx context.Context, input *model.GetOr opts.Filter = sq.And{ opts.Filter, sq.Expr(`to_tsvector('simple', o.name || ' ' || o.slug ) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } @@ -4822,7 +4822,7 @@ func (r *queryResolver) GetOrgLinks(ctx context.Context, input *model.GetLinkInp linkOpts.Filter = sq.And{ linkOpts.Filter, sq.Expr(`to_tsvector('simple', ol.title || ' ' || ol.description || ' ' || ol.url) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } @@ -6103,7 +6103,7 @@ func (r *queryResolver) GetFeed(ctx context.Context, input *model.GetFeedInput) linkOpts.Filter = sq.And{ linkOpts.Filter, sq.Expr(`to_tsvector('simple', ol.title || ' ' || ol.description || ' ' || ol.url) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } @@ -6245,7 +6245,7 @@ func (r *queryResolver) GetAdminOrganizations(ctx context.Context, input *model. opts.Filter = sq.And{ opts.Filter, sq.Expr(`to_tsvector('simple', o.name || ' ' || o.slug ) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } @@ -6533,7 +6533,7 @@ func (r *queryResolver) GetAdminDomains(ctx context.Context, input *model.GetAdm opts.Filter = sq.And{ opts.Filter, sq.Or{ - sq.Expr(`to_tsvector('simple', d.name) @@ to_tsquery('simple', ?)`, s), + sq.Expr(`to_tsvector('simple', d.name) @@ websearch_to_tsquery('simple', ?)`, s), sq.ILike{"lookup_name": fmt.Sprintf("%%%s%%", *input.Search)}, }, } diff --git a/api/loaders/loaders.go b/api/loaders/loaders.go index a3d99dd..1c31334 100644 --- a/api/loaders/loaders.go +++ b/api/loaders/loaders.go @@ -76,7 +76,7 @@ func getPopularLinks(ctx context.Context) func(key []string) ([][]*models.BaseUR opts.Filter = sq.And{ opts.Filter, sq.Expr(`to_tsvector('simple', b.title || ' ' || b.url) - @@ to_tsquery('simple', ?)`, s), + @@ websearch_to_tsquery('simple', ?)`, s), } } diff --git a/core/routes.go b/core/routes.go index 6fcd40f..4a6635c 100644 --- a/core/routes.go +++ b/core/routes.go @@ -3217,7 +3217,7 @@ func (s *Service) TagAutocomplete(c echo.Context) error { if q != "" { s := links.ParseSearch(q) opts := &database.FilterOptions{ - Filter: sq.Expr(`to_tsvector('simple', t.name) @@ to_tsquery('simple', ?)`, s), + Filter: sq.Expr(`to_tsvector('simple', t.name) @@ websearch_to_tsquery('simple', ?)`, s), } if orgID != "" { opts.Filter = sq.And{ diff --git a/helpers.go b/helpers.go index 6f77a84..db1fc69 100644 --- a/helpers.go +++ b/helpers.go @@ -800,10 +800,15 @@ func (t TagQuery) GetSubQuery(inputTag, inputExcludeTag *string) (string, []inte // We need to do some parsing to avoid systax error // for some string chars +// +// Moved search to use websearch_to_tsquery so we shouldn't add any formatting to the search +// This should probably be removed in the future. func ParseSearch(s string) string { - s = strings.TrimSpace(s) - s = strings.Replace(s, ":", "\\:", -1) - return strings.Replace(s, " ", ":* & ", -1) + ":*" + //s = strings.TrimSpace(s) + //s = strings.Replace(s, ":", "\\:", -1) + //return strings.Replace(s, " ", ":* & ", -1) + ":*" + + return s } func AddQueryElement(q template.URL, param, val string) template.URL { -- 2.45.2