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:
dbastrikin
2026-03-21 15:28:29 +02:00
parent 81185bb7ff
commit 78f1c8bf76
41 changed files with 1688 additions and 28 deletions

View File

@@ -184,12 +184,14 @@ go run ./cmd/importoff \
| `GET/POST/DELETE` | `/user-products`, `/user-products/{id}` | Продукты пользователя (холодильник) |
| `POST` | `/user-products/batch` | Массовое добавление продуктов |
| `GET` | `/diary?date=YYYY-MM-DD` | Записи дневника питания за дату |
| `GET` | `/diary/recent?limit=<n>` | Последние записи дневника (блюда и продукты) |
| `POST/DELETE` | `/diary`, `/diary/{id}` | Добавить / удалить запись дневника |
| `GET` | `/home/summary` | Сводка для главного экрана |
| `GET` | `/recommendations` | AI-рекомендации блюд |
| `GET/POST/DELETE` | `/saved-recipes`, `/saved-recipes/{id}` | Сохранённые рецепты |
| `GET/PUT/DELETE` | `/menu/items/{id}` | Меню на неделю |
| `GET/POST/PATCH` | `/shopping-list`, `/shopping-list/{i}/check` | Список покупок |
| `GET` | `/dishes/search?q=<query>&limit=<n>` | Поиск блюд (FTS + trgm) |
| `GET` | `/dishes/{id}` | Блюдо по ID |
| `GET` | `/recipes/{id}` | Рецепт по ID |
| `POST` | `/ai/recognize-receipt` | Распознать чек (синхронно) |

View File

@@ -21,6 +21,17 @@ type offRecord struct {
Code string `json:"code"`
ProductName string `json:"product_name"`
ProductNameEN string `json:"product_name_en"`
ProductNameRu string `json:"product_name_ru"`
ProductNameDe string `json:"product_name_de"`
ProductNameFr string `json:"product_name_fr"`
ProductNameEs string `json:"product_name_es"`
ProductNameIt string `json:"product_name_it"`
ProductNamePt string `json:"product_name_pt"`
ProductNameZh string `json:"product_name_zh"`
ProductNameJa string `json:"product_name_ja"`
ProductNameKo string `json:"product_name_ko"`
ProductNameAr string `json:"product_name_ar"`
ProductNameHi string `json:"product_name_hi"`
CategoriesTags []string `json:"categories_tags"`
Nutriments offNutriments `json:"nutriments"`
UniqueScansN int `json:"unique_scans_n"`
@@ -34,6 +45,12 @@ type offNutriments struct {
Fiber100g *float64 `json:"fiber_100g"`
}
// aliasRow holds one multilingual alias for a product.
type aliasRow struct {
lang string
alias string
}
type productImportRow struct {
canonicalName string
barcode string
@@ -43,6 +60,7 @@ type productImportRow struct {
fat *float64
carbs *float64
fiber *float64
aliases []aliasRow
}
// categoryPrefixes maps OpenFoodFacts category tag prefixes to our product_categories slugs.
@@ -217,6 +235,28 @@ func run() error {
}
seenInBatch[canonicalName] = true
// Collect non-English localised names as aliases so that searches in
// other languages (e.g. "сникерс" → "Snickers") can find the product.
langNames := map[string]string{
"ru": strings.TrimSpace(record.ProductNameRu),
"de": strings.TrimSpace(record.ProductNameDe),
"fr": strings.TrimSpace(record.ProductNameFr),
"es": strings.TrimSpace(record.ProductNameEs),
"it": strings.TrimSpace(record.ProductNameIt),
"pt": strings.TrimSpace(record.ProductNamePt),
"zh": strings.TrimSpace(record.ProductNameZh),
"ja": strings.TrimSpace(record.ProductNameJa),
"ko": strings.TrimSpace(record.ProductNameKo),
"ar": strings.TrimSpace(record.ProductNameAr),
"hi": strings.TrimSpace(record.ProductNameHi),
}
var productAliases []aliasRow
for lang, name := range langNames {
if name != "" && name != canonicalName {
productAliases = append(productAliases, aliasRow{lang: lang, alias: name})
}
}
totalAccepted++
productBatch = append(productBatch, productImportRow{
canonicalName: canonicalName,
@@ -227,6 +267,7 @@ func run() error {
fat: record.Nutriments.Fat100g,
carbs: record.Nutriments.Carbohydrates100g,
fiber: record.Nutriments.Fiber100g,
aliases: productAliases,
})
if len(productBatch) >= *batchSizeFlag {
@@ -336,18 +377,21 @@ func flushBatch(
carbs_per_100g = COALESCE(EXCLUDED.carbs_per_100g, products.carbs_per_100g),
fiber_per_100g = COALESCE(EXCLUDED.fiber_per_100g, products.fiber_per_100g),
updated_at = now()
RETURNING id`)
RETURNING id, canonical_name`)
if upsertError != nil {
return 0, fmt.Errorf("upsert products: %w", upsertError)
}
// Build a canonical_name → product_id map so we can insert aliases below.
productIDByName := make(map[string]string, len(productBatch))
var insertedCount int64
for upsertRows.Next() {
var productID string
if scanError := upsertRows.Scan(&productID); scanError != nil {
var productID, canonicalName string
if scanError := upsertRows.Scan(&productID, &canonicalName); scanError != nil {
upsertRows.Close()
return 0, fmt.Errorf("scan upserted product id: %w", scanError)
}
productIDByName[canonicalName] = productID
insertedCount++
}
upsertRows.Close()
@@ -355,5 +399,35 @@ func flushBatch(
return 0, fmt.Errorf("iterate upsert results: %w", rowsError)
}
// Insert multilingual aliases collected during parsing.
// ON CONFLICT DO NOTHING so re-imports are safe.
aliasBatch := &pgx.Batch{}
for _, row := range productBatch {
productID, exists := productIDByName[row.canonicalName]
if !exists || len(row.aliases) == 0 {
continue
}
for _, aliasEntry := range row.aliases {
aliasBatch.Queue(
`INSERT INTO product_aliases (product_id, lang, alias)
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING`,
productID, aliasEntry.lang, aliasEntry.alias,
)
}
}
if aliasBatch.Len() > 0 {
batchResults := pgxConn.SendBatch(requestContext, aliasBatch)
for range aliasBatch.Len() {
if _, execError := batchResults.Exec(); execError != nil {
batchResults.Close()
return 0, fmt.Errorf("insert product alias: %w", execError)
}
}
if closeError := batchResults.Close(); closeError != nil {
return 0, fmt.Errorf("close alias batch: %w", closeError)
}
}
return insertedCount, nil
}

View File

@@ -22,6 +22,18 @@ type Entry struct {
CreatedAt time.Time `json:"created_at"`
}
// RecentDiaryItem is a lightweight summary of a recently logged product or dish.
type RecentDiaryItem struct {
ItemType string `json:"item_type"` // "dish" | "product"
DishID *string `json:"dish_id,omitempty"`
ProductID *string `json:"product_id,omitempty"`
Name string `json:"name"`
ImageURL *string `json:"image_url,omitempty"`
CategoryName *string `json:"category_name,omitempty"`
CaloriesPer100g *float64 `json:"calories_per_100g,omitempty"`
CaloriesPerServing *float64 `json:"calories_per_serving,omitempty"`
}
// CreateRequest is the body for POST /diary.
type CreateRequest struct {
Date string `json:"date"`

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"log/slog"
"net/http"
"strconv"
"github.com/food-ai/backend/internal/infra/middleware"
"github.com/go-chi/chi/v5"
@@ -15,6 +16,7 @@ type DiaryRepository interface {
ListByDate(ctx context.Context, userID, date string) ([]*Entry, error)
Create(ctx context.Context, userID string, req CreateRequest) (*Entry, error)
Delete(ctx context.Context, id, userID string) error
GetRecent(ctx context.Context, userID string, limit int) ([]*RecentDiaryItem, error)
}
// DishRepository is the subset of dish.Repository used by Handler to resolve dish IDs.
@@ -119,6 +121,31 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusCreated, entry)
}
// GetRecent handles GET /diary/recent?limit=<n>
func (h *Handler) GetRecent(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())
if userID == "" {
writeError(w, http.StatusUnauthorized, "unauthorized")
return
}
limit := 10
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
if parsed, parseError := strconv.Atoi(limitStr); parseError == nil && parsed > 0 && parsed <= 20 {
limit = parsed
}
}
items, queryError := h.repo.GetRecent(r.Context(), userID, limit)
if queryError != nil {
slog.Error("get recent diary items", "err", queryError)
writeError(w, http.StatusInternalServerError, "failed to get recent items")
return
}
if items == nil {
items = []*RecentDiaryItem{}
}
writeJSON(w, http.StatusOK, items)
}
// Delete handles DELETE /diary/{id}
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())

View File

@@ -35,6 +35,7 @@ type MockDiaryRepository struct {
ListByDateFn func(ctx context.Context, userID, date string) ([]*diary.Entry, error)
CreateFn func(ctx context.Context, userID string, req diary.CreateRequest) (*diary.Entry, error)
DeleteFn func(ctx context.Context, id, userID string) error
GetRecentFn func(ctx context.Context, userID string, limit int) ([]*diary.RecentDiaryItem, error)
}
func (m *MockDiaryRepository) ListByDate(ctx context.Context, userID, date string) ([]*diary.Entry, error) {
@@ -48,3 +49,10 @@ func (m *MockDiaryRepository) Create(ctx context.Context, userID string, req dia
func (m *MockDiaryRepository) Delete(ctx context.Context, id, userID string) error {
return m.DeleteFn(ctx, id, userID)
}
func (m *MockDiaryRepository) GetRecent(ctx context.Context, userID string, limit int) ([]*diary.RecentDiaryItem, error) {
if m.GetRecentFn != nil {
return m.GetRecentFn(ctx, userID, limit)
}
return []*diary.RecentDiaryItem{}, nil
}

View File

@@ -124,6 +124,78 @@ func (r *Repository) Create(ctx context.Context, userID string, req CreateReques
return scanEntry(row)
}
// GetRecent returns the most recently logged distinct dishes and products for a user.
func (r *Repository) GetRecent(ctx context.Context, userID string, limit int) ([]*RecentDiaryItem, error) {
if limit <= 0 || limit > 20 {
limit = 10
}
lang := locale.FromContext(ctx)
const q = `
WITH recent AS (
SELECT
dish_id, product_id,
MAX(created_at) AS last_used
FROM meal_diary
WHERE user_id = $1
GROUP BY dish_id, product_id
ORDER BY last_used DESC
LIMIT $2
)
SELECT
r.dish_id::text,
r.product_id::text,
COALESCE(dt.name, d.name, p.canonical_name) AS name,
d.image_url,
COALESCE(pct.name, pc.name) AS category_name,
p.calories_per_100g,
(SELECT MIN(rec.calories_per_serving)
FROM recipes rec WHERE rec.dish_id = r.dish_id) AS calories_per_serving
FROM recent r
LEFT JOIN dishes d
ON d.id = r.dish_id
LEFT JOIN dish_translations dt
ON dt.dish_id = d.id AND dt.lang = $3
LEFT JOIN products p
ON p.id = r.product_id
LEFT JOIN product_categories pc
ON pc.slug = p.category
LEFT JOIN product_category_translations pct
ON pct.product_category_slug = p.category AND pct.lang = $3
ORDER BY r.last_used DESC`
rows, queryError := r.pool.Query(ctx, q, userID, limit, lang)
if queryError != nil {
return nil, fmt.Errorf("get recent diary items: %w", queryError)
}
defer rows.Close()
var results []*RecentDiaryItem
for rows.Next() {
var item RecentDiaryItem
var dishID, productID *string
if scanError := rows.Scan(
&dishID, &productID,
&item.Name, &item.ImageURL, &item.CategoryName,
&item.CaloriesPer100g, &item.CaloriesPerServing,
); scanError != nil {
return nil, fmt.Errorf("scan recent diary item: %w", scanError)
}
item.DishID = dishID
item.ProductID = productID
if dishID != nil {
item.ItemType = "dish"
} else {
item.ItemType = "product"
}
results = append(results, &item)
}
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
}
// Delete removes a diary entry for the given user.
func (r *Repository) Delete(ctx context.Context, id, userID string) error {
tag, deleteError := r.pool.Exec(ctx,

View File

@@ -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 {

View File

@@ -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())

View File

@@ -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) {

View File

@@ -202,15 +202,33 @@ func (r *Repository) Search(requestContext context.Context, query string, limit
FROM product_aliases pa
WHERE pa.product_id = p.id AND pa.lang = $3
) al ON true
WHERE EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND pa.alias ILIKE '%' || $1 || '%'
WHERE (
to_tsvector('english', p.canonical_name) @@ plainto_tsquery('english', $1)
OR EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND to_tsvector('simple', pa.alias) @@ plainto_tsquery('simple', $1)
)
OR p.canonical_name ILIKE '%' || $1 || '%'
OR EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND pa.alias ILIKE '%' || $1 || '%'
)
OR similarity(p.canonical_name, $1) > 0.3
)
OR p.canonical_name ILIKE '%' || $1 || '%'
OR similarity(p.canonical_name, $1) > 0.3
ORDER BY similarity(p.canonical_name, $1) DESC
ORDER BY
GREATEST(
ts_rank(to_tsvector('english', p.canonical_name), plainto_tsquery('english', $1)),
COALESCE((
SELECT MAX(ts_rank(to_tsvector('simple', pa.alias), plainto_tsquery('simple', $1)))
FROM product_aliases pa
WHERE pa.product_id = p.id AND (pa.lang = $3 OR pa.lang = 'en')
), 0),
similarity(p.canonical_name, $1)
) DESC
LIMIT $2`
rows, queryError := r.pool.Query(requestContext, searchQuery, query, limit, lang)

View File

@@ -92,6 +92,7 @@ func NewRouter(
r.Route("/dishes", func(r chi.Router) {
r.Get("/", dishHandler.List)
r.Get("/search", dishHandler.Search)
r.Get("/{id}", dishHandler.GetByID)
})
@@ -111,6 +112,7 @@ func NewRouter(
r.Route("/diary", func(r chi.Router) {
r.Get("/", diaryHandler.GetByDate)
r.Get("/recent", diaryHandler.GetRecent)
r.Post("/", diaryHandler.Create)
r.Delete("/{id}", diaryHandler.Delete)
})

View File

@@ -141,6 +141,7 @@ CREATE TABLE products (
CREATE INDEX idx_products_canonical_name ON products (canonical_name);
CREATE INDEX idx_products_category ON products (category);
CREATE INDEX idx_products_barcode ON products (barcode) WHERE barcode IS NOT NULL;
CREATE INDEX idx_products_fts ON products USING GIN (to_tsvector('english', canonical_name));
-- ---------------------------------------------------------------------------
-- product_aliases
@@ -153,6 +154,7 @@ CREATE TABLE product_aliases (
);
CREATE INDEX idx_product_aliases_lookup ON product_aliases (product_id, lang);
CREATE INDEX idx_product_aliases_trgm ON product_aliases USING GIN (alias gin_trgm_ops);
CREATE INDEX idx_product_aliases_fts ON product_aliases USING GIN (to_tsvector('simple', alias));
-- ---------------------------------------------------------------------------
-- cuisines + cuisine_translations
@@ -217,9 +219,11 @@ CREATE TABLE dishes (
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_dishes_cuisine ON dishes (cuisine_slug);
CREATE INDEX idx_dishes_category ON dishes (category_slug);
CREATE INDEX idx_dishes_rating ON dishes (avg_rating DESC);
CREATE INDEX idx_dishes_cuisine ON dishes (cuisine_slug);
CREATE INDEX idx_dishes_category ON dishes (category_slug);
CREATE INDEX idx_dishes_rating ON dishes (avg_rating DESC);
CREATE INDEX idx_dishes_name_fts ON dishes USING GIN (to_tsvector('english', name));
CREATE INDEX idx_dishes_name_trgm ON dishes USING GIN (name gin_trgm_ops);
CREATE TABLE dish_translations (
dish_id UUID NOT NULL REFERENCES dishes(id) ON DELETE CASCADE,
@@ -228,6 +232,8 @@ CREATE TABLE dish_translations (
description TEXT,
PRIMARY KEY (dish_id, lang)
);
CREATE INDEX idx_dish_translations_name_fts ON dish_translations USING GIN (to_tsvector('simple', name));
CREATE INDEX idx_dish_translations_name_trgm ON dish_translations USING GIN (name gin_trgm_ops);
CREATE TABLE dish_tags (
dish_id UUID NOT NULL REFERENCES dishes(id) ON DELETE CASCADE,