Skip to content

Commit

Permalink
more rigid keyword parsing
Browse files Browse the repository at this point in the history
fixes #954
  • Loading branch information
BuckarooBanzay committed Dec 20, 2024
1 parent 256c241 commit 0e92630
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion db/schemasearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"blockexchange/types"
"database/sql"
"fmt"
"regexp"
"strings"

"github.com/lib/pq"
Expand All @@ -13,6 +14,8 @@ type SchemaSearchRepository struct {
DB *sql.DB
}

var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)

func (r *SchemaSearchRepository) buildWhereQuery(query *strings.Builder, search *types.SchemaSearchRequest, with_order bool) []any {
query.WriteString(" from schema s where true=true")
params := []any{}
Expand All @@ -27,7 +30,11 @@ func (r *SchemaSearchRepository) buildWhereQuery(query *strings.Builder, search

if search.Keywords != nil {
query.WriteString(fmt.Sprintf(" and to_tsvector('english', description || ' ' || name) @@ to_tsquery($%d)", i))
params = append(params, *search.Keywords)
// remove non-alpha/non-numbers
keywords := nonAlphanumericRegex.ReplaceAllString(*search.Keywords, "")
// remove double spaces
keywords = strings.ReplaceAll(keywords, " ", " ")
params = append(params, strings.Join(strings.Split(strings.TrimSpace(keywords), " "), "&"))
i++
}

Expand Down

0 comments on commit 0e92630

Please sign in to comment.