Repository navigation
fix(main): optimize order search with indexed candidate retrieval - #1204
Merged
Merged
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Oct 17, 2026
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix issue #1185: Order search scans large histories through JavaScript UDFs (ARCH-009b)
What Changed
orders_search_ftsandcustomers_search_fts) with sync triggers, automated index rebuild on data-only restore, and shadow table filtering ingetTables.GET /api/orderssearch handler to pre-filter candidate order and customer IDs using FTS5 trigram matching (and indexed customer lookups for phone searches) for queries of at least 3 codepoints, falling back to JavaScript UDF evaluation for short queries.Risk Assessment
Testing
Exercised 5 live end-to-end product scenarios in Playwright Chromium against the running FloCafe POS application along with 100k and 300k order scaling benchmarks, verifying indexed candidate retrieval via FTS5 trigrams, exact Unicode and phone normalization parity, short-query UDF fallbacks, zero-callback candidate pruning on non-matches, and captured visual screenshots for every scenario.
Evidence: Live Order Search Validation Report
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
main/routes/orders.ts:678- In main/routes/orders.ts:678, isEligible checks search.length >= 3 using JavaScript UTF-16 code units rather than Unicode codepoint count ([...search].length >= 3). When an input contains astral plane characters outside the BMP (such as two emojis like '🍕🍔' or '☕🥐'), search.length evaluates to 3 or 4, but contains only 2 Unicode codepoints. When executed against SQLite FTS5 trigram index at main/routes/orders.ts:704 (orders_search_fts MATCH ?) and main/routes/orders.ts:707 (customers_search_fts MATCH ?), SQLite throws 'SqliteError: fts5: query must be at least 3 characters', resulting in an unhandled HTTP 500 error instead of falling back to the UDF scan. Sibling sites: main/routes/orders.ts:704 (orders_search_fts MATCH ?) and main/routes/orders.ts:707 (customers_search_fts MATCH ?). Use codepoint length ([...search].length >= 3) to determine trigram eligibility.main/db.ts:7414- Simplification: In main/db.ts:7414-7421 (migration 115), customers_search_fts defines and indexes phone and phone_digits alongside name, maintaining them across triggers in main/db.ts:7423 (trg_customers_search_ai), main/db.ts:7426 (trg_customers_search_ad), and main/db.ts:7428-7431 (trg_customers_search_au), as well as the backfill in main/db.ts:7436. However, User Intent Order search scans large histories through JavaScript UDFs #1185 does not require indexing customer phone fields in FTS5: phone searches in main/routes/orders.ts:685-700 deliberately bypass customers_search_fts to query the customers table directly with digit UDFs to preserve formatting parity across phone formats, while non-phone searches in main/routes/orders.ts:664 strictly filter customer matches by name via orders_search_contains(search_customer.name, ?). Sibling sites: main/db.ts:7423, main/db.ts:7426, main/db.ts:7428, and main/db.ts:7436. The remedy requires modifying the migration schema to remove phone and phone_digits from customers_search_fts, indexing only name.🔧 Fix applied.
2 warnings still open:
main/db.ts:7414- Simplification: In main/db.ts:7414-7421 (migration 115), customers_search_fts defines and indexes phone and phone_digits alongside name, maintaining them across triggers in main/db.ts:7423 (trg_customers_search_ai), main/db.ts:7426 (trg_customers_search_ad), and main/db.ts:7428-7431 (trg_customers_search_au), as well as the backfill in main/db.ts:7436. However, User Intent Order search scans large histories through JavaScript UDFs #1185 does not require indexing customer phone fields in FTS5: phone searches in main/routes/orders.ts:685-700 deliberately bypass customers_search_fts to query the customers table directly with digit UDFs to preserve formatting parity across phone formats, while non-phone searches in main/routes/orders.ts:664 strictly filter customer matches by name via orders_search_contains(search_customer.name, ?). Sibling sites: main/db.ts:7423, main/db.ts:7426, main/db.ts:7428, and main/db.ts:7436. The remedy requires modifying the migration schema to remove phone and phone_digits from customers_search_fts, indexing only name.main/db.ts:7414- Simplification: In main/db.ts:7414-7421 (migration 115), customers_search_fts defines and indexes phone and phone_digits alongside name, maintaining them across triggers in main/db.ts:7423 (trg_customers_search_ai), main/db.ts:7426 (trg_customers_search_ad), and main/db.ts:7428-7431 (trg_customers_search_au), as well as the backfill in main/db.ts:7436. However, User Intent Order search scans large histories through JavaScript UDFs #1185 does not require indexing customer phone fields in FTS5: phone searches in main/routes/orders.ts:685-700 deliberately bypass customers_search_fts to query the customers table directly with digit UDFs to preserve formatting parity across phone formats, while non-phone searches in main/routes/orders.ts:664 strictly filter customer matches by name via orders_search_contains(search_customer.name, ?). Sibling sites: main/db.ts:7423, main/db.ts:7426, main/db.ts:7428, and main/db.ts:7436. The remedy requires authorization because modifying the migration schema to remove phone and phone_digits from customers_search_fts alters database definition and triggers.✅ **Test** - passed
✅ No issues found.
npm run test:orders-searchnode tests/run-electron-node-test.cjs tests/orders-search-performance.test.tsnode tests/run-electron-node-test.cjs tests/orders-search-api.test.tsPlaywright Chromium UI test against real running FloCafe server at http://127.0.0.1:3210/orders✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.