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:
@@ -59,6 +59,14 @@ type RecipeStep struct {
|
||||
ImageURL *string `json:"image_url"`
|
||||
}
|
||||
|
||||
// DishSearchResult is a lightweight dish returned by the search endpoint.
|
||||
type DishSearchResult struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ImageURL *string `json:"image_url,omitempty"`
|
||||
AvgRating float64 `json:"avg_rating"`
|
||||
}
|
||||
|
||||
// CreateRequest is the body used to create a new dish + recipe at once.
|
||||
// Used when saving a Gemini-generated recommendation.
|
||||
type CreateRequest struct {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
@@ -18,6 +19,31 @@ func NewHandler(repo *Repository) *Handler {
|
||||
return &Handler{repo: repo}
|
||||
}
|
||||
|
||||
// Search handles GET /dishes/search?q=<query>&limit=<n>
|
||||
func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query().Get("q")
|
||||
if query == "" {
|
||||
writeJSON(w, http.StatusOK, []*DishSearchResult{})
|
||||
return
|
||||
}
|
||||
limit := 10
|
||||
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
|
||||
if parsed, parseError := strconv.Atoi(limitStr); parseError == nil && parsed > 0 && parsed <= 50 {
|
||||
limit = parsed
|
||||
}
|
||||
}
|
||||
results, searchError := h.repo.Search(r.Context(), query, limit)
|
||||
if searchError != nil {
|
||||
slog.Error("search dishes", "err", searchError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to search dishes")
|
||||
return
|
||||
}
|
||||
if results == nil {
|
||||
results = []*DishSearchResult{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, results)
|
||||
}
|
||||
|
||||
// List handles GET /dishes — returns all dishes (no recipe variants).
|
||||
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
||||
dishes, err := h.repo.List(r.Context())
|
||||
|
||||
@@ -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