feat: food search sheet with FTS+trgm, dish/recent endpoints, multilingual aliases
Backend: - GET /dishes/search — hybrid FTS (english + simple) + trgm + ILIKE search - GET /diary/recent — recently used dishes and products for the current user - product search upgraded: FTS on canonical_name and product_aliases, ranked by GREATEST(ts_rank, similarity) - importoff: collect product_name_ru/de/fr/... as product_aliases for multilingual search (e.g. "сникерс" → "Snickers") - migrations: FTS + trgm indexes merged into 001_initial_schema.sql (002 removed) Flutter: - FoodSearchSheet: debounced search field, recently-used section, product/dish results, scan-photo and barcode chips - DishPortionSheet: quick ½/1/1½/2 buttons + custom input - + button in meal card now opens FoodSearchSheet instead of going directly to AI scan - 7 new l10n keys across all 12 languages Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -98,6 +98,57 @@ func (r *Repository) List(ctx context.Context) ([]*Dish, error) {
|
||||
return dishes, nil
|
||||
}
|
||||
|
||||
// Search finds dishes matching the query string using FTS + trigram similarity.
|
||||
// Text is resolved for the language in ctx (English fallback).
|
||||
func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*DishSearchResult, error) {
|
||||
if limit <= 0 || limit > 50 {
|
||||
limit = 10
|
||||
}
|
||||
lang := locale.FromContext(ctx)
|
||||
|
||||
const searchQuery = `
|
||||
SELECT d.id,
|
||||
COALESCE(dt.name, d.name) AS name,
|
||||
d.image_url,
|
||||
d.avg_rating,
|
||||
GREATEST(
|
||||
ts_rank(to_tsvector('english', d.name), plainto_tsquery('english', $1)),
|
||||
ts_rank(to_tsvector('simple', COALESCE(dt.name, '')), plainto_tsquery('simple', $1)),
|
||||
similarity(COALESCE(dt.name, d.name), $1)
|
||||
) AS rank
|
||||
FROM dishes d
|
||||
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $3
|
||||
WHERE (
|
||||
to_tsvector('english', d.name) @@ plainto_tsquery('english', $1)
|
||||
OR to_tsvector('simple', COALESCE(dt.name, '')) @@ plainto_tsquery('simple', $1)
|
||||
OR d.name ILIKE '%' || $1 || '%'
|
||||
OR dt.name ILIKE '%' || $1 || '%'
|
||||
OR similarity(COALESCE(dt.name, d.name), $1) > 0.3
|
||||
)
|
||||
ORDER BY rank DESC
|
||||
LIMIT $2`
|
||||
|
||||
rows, queryError := r.pool.Query(ctx, searchQuery, query, limit, lang)
|
||||
if queryError != nil {
|
||||
return nil, fmt.Errorf("search dishes: %w", queryError)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []*DishSearchResult
|
||||
for rows.Next() {
|
||||
var result DishSearchResult
|
||||
var rank float64
|
||||
if scanError := rows.Scan(&result.ID, &result.Name, &result.ImageURL, &result.AvgRating, &rank); scanError != nil {
|
||||
return nil, fmt.Errorf("scan dish search result: %w", scanError)
|
||||
}
|
||||
results = append(results, &result)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// FindOrCreate returns the dish ID and whether it was newly created.
|
||||
// Looks up by case-insensitive name match; creates a minimal dish row if not found.
|
||||
func (r *Repository) FindOrCreate(ctx context.Context, name string) (id string, created bool, err error) {
|
||||
|
||||
Reference in New Issue
Block a user