fix: improve search to prioritize path-based matching
When searching for option paths like "services.nginx", use name-based LIKE matching instead of full-text search. This ensures the results are options that start with the query, not random options that mention the term somewhere in their description. - Path queries (containing dots): use LIKE for name prefix matching - Text queries (no dots): use FTS for full-text search on name+description Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
@@ -297,15 +298,29 @@ func (s *PostgresStore) GetChildren(ctx context.Context, revisionID int64, paren
|
||||
|
||||
// SearchOptions searches for options matching a query.
|
||||
func (s *PostgresStore) SearchOptions(ctx context.Context, revisionID int64, query string, filters SearchFilters) ([]*Option, error) {
|
||||
// Use PostgreSQL full-text search
|
||||
baseQuery := `
|
||||
SELECT id, revision_id, name, parent_path, type, default_value, example, description, read_only
|
||||
FROM options
|
||||
WHERE revision_id = $1
|
||||
AND to_tsvector('english', name || ' ' || COALESCE(description, '')) @@ plainto_tsquery('english', $2)`
|
||||
var baseQuery string
|
||||
var args []interface{}
|
||||
argNum := 1
|
||||
|
||||
args := []interface{}{revisionID, query}
|
||||
argNum := 3
|
||||
// If the query looks like an option path (contains dots), prioritize name-based matching.
|
||||
if strings.Contains(query, ".") {
|
||||
baseQuery = `
|
||||
SELECT id, revision_id, name, parent_path, type, default_value, example, description, read_only
|
||||
FROM options
|
||||
WHERE revision_id = $1
|
||||
AND (name = $2 OR name LIKE $3)`
|
||||
args = []interface{}{revisionID, query, query + ".%"}
|
||||
argNum = 4
|
||||
} else {
|
||||
// For non-path queries, use PostgreSQL full-text search
|
||||
baseQuery = `
|
||||
SELECT id, revision_id, name, parent_path, type, default_value, example, description, read_only
|
||||
FROM options
|
||||
WHERE revision_id = $1
|
||||
AND to_tsvector('english', name || ' ' || COALESCE(description, '')) @@ plainto_tsquery('english', $2)`
|
||||
args = []interface{}{revisionID, query}
|
||||
argNum = 3
|
||||
}
|
||||
|
||||
if filters.Type != "" {
|
||||
baseQuery += fmt.Sprintf(" AND type = $%d", argNum)
|
||||
|
||||
Reference in New Issue
Block a user