← selected work

RecipeSense: A Learned Ranker for What's in Your Kitchen

Recipe search over 238,157 recipes: a learned ranker puts the right recipe on page one for 97% of typed queries (TF-IDF: 54%), in about 20 ms.

Jul 2024ML Engineer & Backend Developerwith Nauman Pathan, Sara Pathan
RecipeSense: A Learned Ranker for What's in Your Kitchen

Overview

RecipeSense answers one question: what can I cook with what is in my kitchen? You type ingredients the way you would say them, or describe what you feel like eating, and it ranks 238,157 recipes in about 20 milliseconds. There are no ratings or accounts behind it, so it works from the first query.

What comes out

Four ingredients go in, some of them by their Hindi names. The page shows the ranked recipes and the server's measured time for the ranking.

RecipeSense results for turmeric powder, green chillies, paneer and spinach leaves: 60 results from the learned ranker in 29.3 ms, led by Methi Chaman, Palak Paneer Kathi Roll and Palak Paneer
Paneer, spinach, turmeric and green chillies. The time is the ranker's, measured on the server.

"haldi", "turmeric powders" and "Turmeric powder (Haldi)" are one ingredient. Typed names are resolved to canonical ones through an alias table, singular folding, then a closest-spelling match and a character n-gram match for misspellings, so "tomatoe", "chiken" and "green chilly" still land.

Free text takes a different path. "something creamy and spicy with spinach" is embedded with MiniLM and compared against every recipe's text. On a recipe page, More like this finds neighbours in a joint space of ingredient vectors and text vectors.

How it works

Three cheap rankers each nominate 50 recipes: TF-IDF on the text as typed, IDF-weighted ingredient overlap on the cleaned names, and skip-gram ingredient embeddings. A LightGBM lambdarank model scores the union on 18 features. Cuisine, course, vegetarian and likes are applied last.

Block diagram of one RecipeSense request. Five typed ingredients are resolved to canonical ids and scored by TF-IDF, ingredient overlap and ingredient embeddings; the top 50 of each become 114 candidates with 18 features that a 93-tree LightGBM ranker scores; filters and boosts give Palak Paneer first in 23 ms. Side panels show free-text search with MiniLM, more-like-this over a joined 256-dim matrix and the files built offline
One request, end to end. Everything on the right is built offline and committed.

The strongest feature is each candidate's gap to the best overlap score for that query. The ranker's main job is on long pantry lists, where it learns to ignore ingredients a recipe does not use.

How good is it

The detailed comparison is on the first dataset, 6,521 Indian recipes, where every ranker can be run on every query. The full catalogue comes after it.

There are no ratings or click logs, so the evaluation is known-item search. A query is built from a held-out recipe's own ingredients, and a ranker scores well if that recipe comes back near the top. Recipes are split by id: half for tuning and training, half for reporting, with 3,120 test queries per style.

Share of queries where the source recipe is in the top 6, the app's first page:

Finding the source recipe in the first six results
RankerTypedExactPantry
Substring countTyped52.4%Exact88.9%Pantry67.5%
TF-IDFTyped71.9%Exact86.6%Pantry83.8%
Ingredient overlap on cleaned namesTyped93.1%Exact97.5%Pantry95.7%
Ingredient embeddings aloneTyped77.0%Exact83.5%Pantry37.5%
6,521 Indian recipes. 3,120 held-out queries per style. Top-6 hit rate; higher is better. Bright bars mark the best result in each column, including ties.

Typed queries use the Hindi alias half the time and flip singular and plural. Exact queries spell ingredients as the dataset does. Pantry queries are three quarters of the recipe plus ten unrelated common ingredients.

Most of the gain is data cleaning: resolving aliases and plurals takes typed queries from 72% to 93% before any model is trained. The learned ranker adds the rest, mostly on pantry queries.

Growing it to 238,157 recipes

The first version had 6,521 recipes. Adding the 231,636 recipes of the Food.com dataset made the catalogue 36 times larger, and the serverless function failed twice on the way: the bundle was 539 MB against a 500 MB limit, and then it was killed for memory at 1.1 GB.

The fix was to stop doing work at start-up. Everything is packed once, offline: arrays for search, recipe text in compressed blocks that are read only when a recipe page asks for one, and text vectors cut from 384 to 192 dimensions and stored as int8. pandas is gone from serving, and the ingredient and text vectors are no longer copied into one joined matrix. The function now loads the catalogue in about two seconds and peaks at 587 MB.

The models were retrained on the full set and measured the same way, on 1,367 queries per style from held-out recipes:

The full catalogue, measured the same way
RankerTypedExactPantry
TF-IDFTyped53.8%Exact80.4%Pantry86.0%
Ingredient overlap on cleaned namesTyped96.9%Exact97.4%Pantry95.1%
Ingredient embeddings aloneTyped75.9%Exact76.7%Pantry31.2%
238,157 recipes. 1,367 held-out queries per style. Top-6 hit rate; higher is better. Bright bars mark the best result in each column, including ties.

With 36 times more recipes to confuse the right one with, TF-IDF drops on typed queries and the learned ranker holds its place. The search vectors lost little in the cut: they return 92% of the full model's top 24 results.

One thing the bigger vocabulary broke was typo matching. It now holds other people's typos, so "tomatoe" matched a rare "dried tomatoe" instead of tomato. Misspellings are now matched against common ingredients first.

On the first dataset, same split, about 3,100 queries per style. MiniLM finds a dish from its name better than TF-IDF, and is much worse on a list of ingredients. So ingredient search goes to the ranker and free text goes to MiniLM.

Free-text search depends on the query
RankerRecipe titleIngredient sentence
TF-IDF over textRecipe title91.6%Ingredient sentence81.5%
MiniLMRecipe title95.6%Ingredient sentence31.6%
First dataset, same held-out split. Top-6 hit rate; higher is better. Bright bars mark the best result in each column, including ties.

What did not work

Ingredient embeddings do not improve ranking. On their own they collapse on pantry queries (37.5%), because averaging in ten extra ingredients drags the query vector towards generic recipes. An ablation that removed their features from the learned ranker left its held-out score unchanged. They are kept for "more like this", which is what co-occurrence vectors are good at.

Known-item search also has a limit: it checks whether the right recipe can be found, not whether people like the list. There are no human queries or judgements behind these numbers.

Serving it

The whole thing runs on one Vercel project: a static React build and a single Python function. There is no PyTorch, no LightGBM and no pandas at serving time. MiniLM runs as an 8-bit ONNX model, and the ranker's trees are parsed from the LightGBM model file and scored in NumPy, with output identical to LightGBM's.

Recipe photos could not be embedded, so each recipe gets a cover drawn in the browser from its own ingredients: the vessel comes from the course and cuisine, the shapes and colours from the main ingredients.

A RecipeSense recipe page for Methi Chaman: a drawn cover of a green gravy with paneer cubes on a plate, then prep, cook and total times, and the ingredients the cover was drawn from
No photo is requested. The cover is drawn from the recipe's main ingredients.

Use it

The packed catalogue and the trained models are committed, so there is nothing to download.

from recsys.service import Recommender
 
rec = Recommender()
 
# Ingredients, typed the way people type them
for r in rec.recommend(["haldi", "paneer", "palak", "green chilly"], veg=True, limit=3):
    print(f"{r['score']:.2f}  {r['total_time']:>3} min  {r['title']}")
 
# Free text
for r in rec.search("something creamy and spicy with spinach", limit=3):
    print(f"{r['score']:.2f}  {r['title']}")

Registered with the Copyright Office, Government of India, as "Recipesense" (SW-2025020495).

Stack

ml
LightGBMWord2Vec (gensim)MiniLMONNX Runtimescikit-learnNumPy
frontend
React.jsViteTailwind CSS
backend
PythonFlask
tooling
pytestGitHub ActionsDocker
deployment
Vercel