feat(search+docs): trigram fuzzy search, brand/country filters, developer docs
Search: - migration 0009: trigram GIN index on brand.name + btree on country_of_origin - SearchProducts: typo-tolerant word_similarity matching (>=0.42) on top of ILIKE substring + barcode; new brand/country filters; rank by similarity * (0.5 + quality_score). Response gains country_of_origin, quality_score and per-result relevance score. - public search UI: brand/country filter inputs; show country in results Docs: - serve embedded OpenAPI 3 spec at GET /api/v1/openapi.json (not rate limited) - ApiDocs page: auth + rate-limit section, updated search params/response - docs/api.md developer guide Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
+67
-22
@@ -82,11 +82,22 @@ func (s *Store) ProductBarcodes(ctx context.Context, productID string) ([]Barcod
|
||||
|
||||
// ProductSummary is a lightweight row used in search/listing responses.
|
||||
type ProductSummary struct {
|
||||
ID string `json:"id"`
|
||||
GTIN *string `json:"gtin"`
|
||||
Name string `json:"name"`
|
||||
Brand *string `json:"brand"`
|
||||
CategoryPath *string `json:"category_path"`
|
||||
ID string `json:"id"`
|
||||
GTIN *string `json:"gtin"`
|
||||
Name string `json:"name"`
|
||||
Brand *string `json:"brand"`
|
||||
CategoryPath *string `json:"category_path"`
|
||||
Country *string `json:"country_of_origin"`
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
Score *float64 `json:"score,omitempty"`
|
||||
}
|
||||
|
||||
// SearchFilters bundles the optional filters accepted by SearchProducts.
|
||||
type SearchFilters struct {
|
||||
Query string // fuzzy name / barcode query
|
||||
Category string // ltree path; matches the subtree
|
||||
Brand string // fuzzy brand name
|
||||
Country string // country_of_origin prefix (case-insensitive)
|
||||
}
|
||||
|
||||
const productSelect = `
|
||||
@@ -148,34 +159,67 @@ func (s *Store) ProductByID(ctx context.Context, id string) (*Product, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// SearchProducts performs a fuzzy name search with optional category subtree filter.
|
||||
func (s *Store) SearchProducts(ctx context.Context, q, category string, limit, offset int) ([]ProductSummary, int, error) {
|
||||
// fuzzyThreshold is the minimum word_similarity for a name to be considered a
|
||||
// fuzzy match. ~0.42 tolerates common typos (e.g. "choclate"→"Chocolate")
|
||||
// without returning unrelated products.
|
||||
const fuzzyThreshold = "0.42"
|
||||
|
||||
// SearchProducts runs a trigram-fuzzy name search with optional category /
|
||||
// brand / country filters. When a query is present, matching is inclusive
|
||||
// (substring OR trigram-similar OR barcode), and results are ranked by name
|
||||
// similarity blended with quality_score so the best, most-complete records
|
||||
// surface first. Without a query, results are ordered by quality_score.
|
||||
func (s *Store) SearchProducts(ctx context.Context, f SearchFilters, limit, offset int) ([]ProductSummary, int, error) {
|
||||
args := []any{}
|
||||
where := "WHERE p.status = 'active'"
|
||||
if q != "" {
|
||||
args = append(args, q)
|
||||
where += ` AND (p.name ILIKE '%' || $1 || '%'
|
||||
|
||||
qIdx := 0
|
||||
if f.Query != "" {
|
||||
args = append(args, f.Query)
|
||||
qIdx = len(args)
|
||||
q := "$" + strconv.Itoa(qIdx)
|
||||
where += ` AND (p.name ILIKE '%' || ` + q + ` || '%'
|
||||
OR word_similarity(` + q + `, p.name) >= ` + fuzzyThreshold + `
|
||||
OR EXISTS (SELECT 1 FROM product_barcode pb
|
||||
WHERE pb.product_id = p.id AND pb.gtin ILIKE '%' || $1 || '%'))`
|
||||
WHERE pb.product_id = p.id AND pb.gtin ILIKE '%' || ` + q + ` || '%'))`
|
||||
}
|
||||
if category != "" {
|
||||
args = append(args, category)
|
||||
if f.Category != "" {
|
||||
args = append(args, f.Category)
|
||||
where += " AND c.path <@ $" + strconv.Itoa(len(args)) + "::ltree"
|
||||
}
|
||||
if f.Brand != "" {
|
||||
args = append(args, f.Brand)
|
||||
where += " AND b.name ILIKE '%' || $" + strconv.Itoa(len(args)) + " || '%'"
|
||||
}
|
||||
if f.Country != "" {
|
||||
args = append(args, f.Country)
|
||||
where += " AND p.country_of_origin ILIKE $" + strconv.Itoa(len(args)) + " || '%'"
|
||||
}
|
||||
|
||||
from := `FROM product p
|
||||
LEFT JOIN brand b ON b.id = p.brand_id
|
||||
LEFT JOIN category c ON c.id = p.category_id `
|
||||
|
||||
countSQL := "SELECT count(*) FROM product p LEFT JOIN category c ON c.id = p.category_id " + where
|
||||
var total int
|
||||
if err := s.pool.QueryRow(ctx, countSQL, args...).Scan(&total); err != nil {
|
||||
if err := s.pool.QueryRow(ctx, "SELECT count(*) "+from+where, args...).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Ranking: when querying, similarity drives order, multiplied by a
|
||||
// quality factor floored at 0.5 so low-quality records aren't zeroed out.
|
||||
scoreExpr := "NULL::real"
|
||||
orderBy := "p.quality_score DESC, p.name"
|
||||
if f.Query != "" {
|
||||
q := "$" + strconv.Itoa(qIdx)
|
||||
scoreExpr = "word_similarity(" + q + ", p.name)"
|
||||
orderBy = scoreExpr + " * (0.5 + p.quality_score) DESC, p.quality_score DESC, p.name"
|
||||
}
|
||||
|
||||
args = append(args, limit, offset)
|
||||
listSQL := `
|
||||
SELECT p.id, p.gtin, p.name, b.name, c.path::text
|
||||
FROM product p
|
||||
LEFT JOIN brand b ON b.id = p.brand_id
|
||||
LEFT JOIN category c ON c.id = p.category_id ` + where +
|
||||
" ORDER BY p.name LIMIT $" + strconv.Itoa(len(args)-1) + " OFFSET $" + strconv.Itoa(len(args))
|
||||
listSQL := "SELECT p.id, p.gtin, p.name, b.name, c.path::text, p.country_of_origin, p.quality_score, " +
|
||||
scoreExpr + " AS score " + from + where +
|
||||
" ORDER BY " + orderBy +
|
||||
" LIMIT $" + strconv.Itoa(len(args)-1) + " OFFSET $" + strconv.Itoa(len(args))
|
||||
|
||||
rows, err := s.pool.Query(ctx, listSQL, args...)
|
||||
if err != nil {
|
||||
@@ -186,7 +230,8 @@ LEFT JOIN category c ON c.id = p.category_id ` + where +
|
||||
out := []ProductSummary{}
|
||||
for rows.Next() {
|
||||
var ps ProductSummary
|
||||
if err := rows.Scan(&ps.ID, &ps.GTIN, &ps.Name, &ps.Brand, &ps.CategoryPath); err != nil {
|
||||
if err := rows.Scan(&ps.ID, &ps.GTIN, &ps.Name, &ps.Brand, &ps.CategoryPath,
|
||||
&ps.Country, &ps.QualityScore, &ps.Score); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, ps)
|
||||
|
||||
Reference in New Issue
Block a user