omi icon indicating copy to clipboard operation
omi copied to clipboard

App search fetches and enriches the whole catalog on every query, and matches name only

Open mikejsmith1985 opened this issue 3 months ago • 3 comments

Searching the Apps tab is slow no matter what I type — typing s costs the same as typing sports — and some searches return nothing when they clearly shouldn't. Searching adhd gives "No apps found".

Reading /v2/apps/search against d60ab7a1c, both look like the same root cause: the query never reaches the database.

  • backend/database/apps.py:83 — search_apps_db() takes category, capability, my_apps, installed_apps. There is no query parameter.
  • backend/routers/apps.py:672 — so it is called without the text query, and returns the whole catalog for the given filters.
  • backend/routers/apps.py:693-694 — get_apps_installs_count(app_ids) and get_apps_reviews(app_ids) then run over every app in that catalog, followed by an App(**app_dict) construction per record.
  • backend/routers/apps.py:725 — only then is the text applied, in Python: search_query in app.name.lower().

So the per-request cost is independent of the query, which matches the symptom: every keystroke pays for the full catalog plus its install counts and reviews, then discards almost all of it.

The second half is the empty results. That final filter matches the name only, while the parameter is documented as 'Search query for app name or description' (backend/routers/apps.py:650). An app whose description is about ADHD but whose name does not contain the string is unreachable by search. I do not know whether the docstring or the behaviour is the intended one, which is part of why I am asking rather than sending a patch.

Happy to work on this if you want it — I would rather agree the direction first, since pushing the query down to Firestore is a real change to a hot path and the substring match has no obvious index-friendly equivalent.

Investigated with Claude Code; every file:line above was verified against d60ab7a1c.

mikejsmith1985 avatar Aug 09 '26 14:08 mikejsmith1985

Both halves confirmed against d60ab7a1c — thanks for the file:line trace, it was exact.

I've taken the name-only match half in #11294, since that one is unambiguous: the q parameter documents "app name or description", and every client's offline fallback ranks over name/description/category/author (desktop/.../appRanking.ts → matchesQuery). The remote endpoint was strictly narrower than the fallback it replaces — an app findable with the network down vanished once the endpoint answered. Fixed to name OR description, with a relevance tier (exact name → name prefix → description-only) ahead of the installs sort, because results are paginated and widening the predicate alone would let a 10k-install description match push the app you typed the name of off page 1.

Verified: GET /v2/apps/search?q=adhd → ['adhd', 'focus'], was ['adhd'] only; regression test fails on the pre-fix handler.

The pushdown half is yours if you want it — I deliberately didn't touch it. You're right that it's a real change to a hot path and that substring matching has no index-friendly equivalent in Firestore, so it deserves the design conversation you asked for rather than a drive-by patch. #11294 doesn't block it; it only changes which apps pass the in-Python filter.

kodjima33 avatar Aug 09 '26 15:08 kodjima33

Thanks — #11294 is a better fix than the one I had in mind. Ranking the tiers ahead of the installs sort is the part I would have missed; I was thinking about the predicate and not about what pagination does to it.

Taking you up on the pushdown. Two designs below, and I'd rather you picked the direction than got a PR for the one I happened to like. Both are described against 68b6072a.

What is actually expensive

Unchanged by #11294, since it widened the predicate rather than moving it:

  • routers/apps.py:696 — search_apps_db() takes no query, so the filtered-but-untexted catalog comes back whole
  • routers/apps.py:717-718 — get_apps_installs_count() and get_apps_reviews() then run over every one of those ids
  • an App(**app_dict) per record
  • routers/apps.py:749 — only now is the text applied, in Python

The cost is the same whether the user typed s or sports, which is why it is slow on a fast connection. Worth noting /v2/apps/search has no cache, while the grouped endpoint above it uses set_generic_cache(..., ttl=600).

Option A — denormalised search terms on the app doc

Add a search_terms: string[] to plugins_data, maintained in the three write paths (add_app_to_db, upsert_app_to_db, update_app_in_db) plus a backfill. Query it with array_contains, then rank the survivors with the existing _name_match_tier.

Pros

  • No new infrastructure and no second store to operate.
  • The full-catalog fetch disappears; enrichment shrinks to what actually matched.
  • Ships inside the existing deploy lane, and a bug in it degrades to "fewer results", not to a disclosure.
  • Visibility stays where it already is — approved / private / uid remain Firestore fields on the same query, so there is no second copy of the access rules to keep true.

Cons

  • array_contains matches whole elements. Prefixes work only if you store them, and storing every prefix of every description token is not viable at document size; realistically it is name prefixes plus whole description words.
  • That is a behaviour regression. Today query in app.name.lower() matches mid-word — ssistant finds ADHD Assistant. Token matching does not. Someone will notice.
  • One array_contains per query, so multi-word queries need array_contains_any (OR, and capped at a small number of disjuncts — worth confirming against your tier) or an intersection done in Python.
  • No ranking in the engine, so #11294's tiers stay a Python sort. Fine, because the set is small by then, but it does mean relevance logic lives in two places if you ever add more.

Option B — a Typesense apps collection

backend/utils/memory/atom_keyword_index.py is already the shape of this: ensure_*_collection() validating schema on boot, upsert_*_doc() from the write paths, delete_*_doc(), and a keyword_search_*_ids() that returns ids for the caller to merge. An apps equivalent would follow it, and the endpoint would resolve ids → hydrate only the page from Firestore.

Pros

  • Typo tolerance, prefix and infix matching. Strictly better than today rather than a trade — the ssistant case above keeps working.
  • Ranking moves into the engine. _name_match_tier becomes a sort spec instead of a sort over the whole catalog.
  • Enrichment drops to the 20 rows on the page.
  • Category, capability and rating become facets, so the filter combinations stop multiplying Firestore composite indexes.
  • The infrastructure is already running and there is an in-repo pattern to copy, so this is incremental rather than greenfield.

Cons

  • A second source of truth, with the drift that implies: an app present in Firestore and missing from the index is invisible to search. Needs backfill plus reconciliation.
  • The sharp edge is visibility. approved, private and owner have to be mirrored as filter fields, and a stale one is not a missing result, it is an unapproved or private app shown to someone. Option A cannot fail that way. If you take this, that is the part I would want designed first and tested hardest.
  • Typesense being down becomes apps-search being down, unless the Python path stays as a fallback — which means carrying both.

The cheap one, which may be what you actually want first

Neither of the above is needed to make the symptom go away today: the enriched public catalog is the same for every user, and /v2/apps/search currently caches none of it. A shared snapshot behind the existing set_generic_cache would take most of the cost out for a fraction of the work and change no search semantics at all. It does not scale forever, and it is not what the issue title asks for, but it is honest to say it is the smallest thing that fixes what I reported.

What I would need from you

How large is the catalog now, and how fast is it growing? That is the input that decides this and the one I cannot see. Under a few thousand apps, A plus a cache is probably the right answer for a long time and B is over-building. If it is already large or the trajectory is steep, A's token matching is a regression you would end up undoing, and B is where this lands eventually anyway.

Happy to build whichever you pick, or neither. I have not started on any of them — I would rather have the direction first than send a PR you have to argue with.

Investigated with Claude Code; every file:line above verified against 68b6072a.

mikejsmith1985 avatar Aug 09 '26 19:08 mikejsmith1985

Opened #11526 for the cheap option from my comment above — not the pushdown. The A-vs-B question is still open and still yours; I have not started on either, and the catalog-size answer is still the input I am missing.

Two things worth flagging since both differ from what I proposed here:

I did not add a cache. I had suggested a shared snapshot behind set_generic_cache. Reading the code, get_approved_available_apps() already holds the exact set search_apps_db's default branch selects, behind a 10-minute Redis copy with a singleflight, already invalidated on approve/publish/unpublish/visibility change, and already read by the browse endpoints above search. Reusing it beats adding a second copy — a public list that can go stale on its own is how an unpublished app stays visible after its owner hides it.

It turned up a latent 500 on that shared path. get_approved_available_apps reads app['id'] unguarded, so a legacy document without that field raises KeyError out of a builder that is process-cached and serves the marketplace to everyone. Same failure search_apps was hardened against in the poison guard, on a worse path. Fixed in its own commit ahead of the perf change. I have not seen it fire in production — I found it by reading the path I was about to depend on.

The PR body also declares one behaviour change (search now skips disabled apps, agreeing with browse) and explains why six existing tests moved fixture without changing an assertion.

None of this reduces the read cost to be proportional to the query — it just stops paying it in Firestore reads on every keystroke. Happy to keep waiting on the design call.

mikejsmith1985 avatar Aug 13 '26 22:08 mikejsmith1985