Developer notes
Technical reference for developers who customize Search Engine Pack.
Add-on identity
Section titled “Add-on identity”| Item | Value |
|---|---|
| Add-on id | kv_search_engine_pack |
| Function prefix | fn_kv_search_engine_pack_ |
| Language variables | kv_search_engine_pack.* |
| CSS scope | .kv-sep-* (storefront), .kv-sep-admin (admin) |
| PHP namespace | Tygh\Addons\KvSearchEnginePack |
| Admin controller | kv_search_engine_pack (manage, POST actions, console cron) |
| Storefront controller | kv_search_engine_pack.suggest, .track, .cron (JSON endpoints, never cached) |
Architecture
Section titled “Architecture”| Class | Role |
|---|---|
Driver\Driver, Driver\Meilisearch, Driver\Typesense |
One REST driver per engine behind one interface (health, live handle, build, settings, upsert, delete, promote, search, count). Elasticsearch / OpenSearch are meant to be added here. |
Config, State |
Connection (admin key encrypted with fn_encrypt_text, wrapped in JSON because CS-Cart 4.17 pads decrypted text), key/value state in ?:kv_srch_state. Config::isReady() = enabled + license + connection + index built. |
DocMapper |
Catalog rows to index documents, one language per index. Products that are disabled, belong to an inactive vendor or have no name yield no document (so they are deleted). |
Queue |
One row per product, claim by token, version counter, back-off, dead letters. |
Indexer |
Queue worker, full reindex (cursor, resume, swap), cron entry. |
Search, ResultsPage, Suggest |
Query building (visibility filters first), the results page hooks, the JSON endpoints. |
Rules, Analytics, RateLimit, Breaker, Events, Http, ClientIp |
Ranking rules, counters, fixed-window rate limit, circuit breaker, PII-free event log, curl client, visitor address. |
Document
Section titled “Document”id, title, code, brand[] (feature values of type “brand”), categories[] (full path names), short_description, features[] (“Name: Value”), description (text only, 3,000 characters), category_ids[] (with ancestors), price (base), price_g<user group id> (price of each group), in_stock, company_id, vendor, company_ids[] (Ultimate: owner and shared storefronts), usergroup_ids[] (0 = everybody), popularity, created. Images and final prices are not indexed: the core renders them at display time, so they are always current and follow the visitor’s currency and group.
Visibility
Section titled “Visibility”The engine is asked for ids only, with these filters always present: usergroup_ids in (0 + the visitor’s groups), Ultimate company_ids = storefront, vendor scope company_id = vendor. The returned ids then go through fn_get_products (status, storefront, user group, category, availability dates), so a stale index can never show something a visitor may not see.
Sync and locks
Section titled “Sync and locks”- Hooks:
update_product_post,delete_product_post,update_product_amount_post,update_product_prices,update_category_post,update_company,tools_change_status,update_product_feature_post,global_update_products,change_company_status_pre(vendor approved / disabled throughfn_change_company_status),delete_category_preandimport_post(CSV and XML imports write prices, features and variants straight to the database: every product is queued again after an import of the products, features, feature variants or quantity discounts pattern). The two*_prehooks queue before the change and again when the request ends, after the change was written. They only write queue rows (INSERT ... ON DUPLICATE KEY UPDATE version = version + 1). - A job rebuilds the document from the database at the time it runs, so it is idempotent: create, update and delete are the same job (no document = delete).
- A claim is a conditional
UPDATE ... LIMITwith a random token and a 120 s lease. Completing deletes the row only whenversionis unchanged; a save that arrives while the job runs keeps the row queued. An expired lease is claimed again. - Named locks (
GET_LOCK, names include a hash of the database and table prefix):queue(one worker at a time),reindex(one rebuild step at a time),swap(held around every index write and around the promotion, so a swap never happens in the middle of a write). - Full reindex: builds
<name>_b(Meilisearch) or a new timestamped collection (Typesense) with the documented settings, writes batches (1,000 / 250 documents) and saves the cursor after each. Queue writes during the build go to the live and the building index and the product ids are noted; right before the swap, under the swap lock, those products are written again from the database. Promotion: MeilisearchPOST /swap-indexes(rename when no live index exists), Typesense aliasPUT /aliases/<name>; the old index is dropped afterwards. - Failures: a failed step sets state
failedwith the cursor kept (Resume continues). Only transport trouble (no answer, 5xx, timeout) opens the storefront circuit breaker; a rejected document does not.
Fallback
Section titled “Fallback”Search::run() never throws and returns null when the add-on is not ready, the breaker is open, the engine does not answer within the timeout or answers with an error. The caller then leaves the request alone, so the core search (or Instant Live Search) runs. The breaker opens after three failures for 45 seconds.
Full page cache
Section titled “Full page cache”Nothing visitor-specific is printed into cached HTML. The dropdown configuration is identical for everybody; suggestions and beacons are AJAX calls with Cache-Control: no-store; the results page and its facet bar are built from the URL only (signed-in visitors never receive a cached page). The add-on adds the header X-Kv-Sep: engine to results pages ranked by the engine (diagnostics, no visitor data).
Security
Section titled “Security”- The browser never talks to the engine and never receives a key (no search-only key or tenant token is needed).
- Admin key: encrypted at rest, masked in the form, scrubbed from every log line and error text (
Events::scrub), sent only in a request header. - Rate limit:
ClientIp::rateKey()(forged forwarding headers are ignored unless the merchant trusts the proxy); suggest 90 and track 60 requests per minute and client. - Owner mode: the admin controller denies every Multi-Vendor vendor request before any mode runs; the permission schema also denies the controller to vendors.
- Analytics: counters only, see Usage.
Engine API calls
Section titled “Engine API calls”Only the documented endpoints below are used. The same list with notes is kept in the repository (notes/API-NOTES.md).
Meilisearch (tested with 1.58.0)
Section titled “Meilisearch (tested with 1.58.0)”| Call | Documentation |
|---|---|
GET /health, GET /version |
https://www.meilisearch.com/docs/reference/api/health/get-health.md, https://www.meilisearch.com/docs/reference/api/version/get-version.md |
POST /indexes, DELETE /indexes/{uid}, POST /swap-indexes |
https://www.meilisearch.com/docs/reference/api/indexes/create-index.md, https://www.meilisearch.com/docs/reference/api/indexes/delete-index.md, https://www.meilisearch.com/docs/reference/api/indexes/swap-indexes.md |
PATCH /indexes/{uid}/settings |
https://www.meilisearch.com/docs/reference/api/settings |
POST /indexes/{uid}/documents, POST /indexes/{uid}/documents/delete |
https://www.meilisearch.com/docs/reference/api/documents/add-or-replace-documents.md, https://www.meilisearch.com/docs/reference/api/documents/delete-documents-by-filter.md |
GET /tasks/{uid} |
https://www.meilisearch.com/docs/reference/api/async-task-management/get-task.md |
POST /indexes/{uid}/search |
https://www.meilisearch.com/docs/reference/api/search |
Typesense (beta: built to the official Typesense 30 API, not yet verified on a live server; synonyms need Typesense 30+; documentation version 30.0)
Section titled “Typesense (beta: built to the official Typesense 30 API, not yet verified on a live server; synonyms need Typesense 30+; documentation version 30.0)”| Call | Documentation |
|---|---|
GET /health |
https://typesense.org/docs/30.0/api/cluster-operations.html |
POST /collections, GET /collections/{c}, DELETE /collections/{c} |
https://typesense.org/docs/30.0/api/collections.html |
POST /collections/{c}/documents/import?action=upsert, DELETE /collections/{c}/documents?filter_by=... |
https://typesense.org/docs/30.0/api/documents.html |
PUT /aliases/{name}, GET /aliases/{name} |
https://typesense.org/docs/30.0/api/collection-alias.html |
PUT /synonym_sets/{name} (30 and newer) |
https://typesense.org/docs/30.0/api/synonyms.html |
GET /collections/{c}/documents/search |
https://typesense.org/docs/30.0/api/search.html |
Database tables
Section titled “Database tables”?:kv_srch_state (connection, state), ?:kv_srch_queue, ?:kv_srch_events, ?:kv_srch_synonyms, ?:kv_srch_stopwords, ?:kv_srch_rules, ?:kv_srch_stats, ?:kv_srch_seen, ?:kv_srch_rate. Uninstalling drops them.
Hooks used
Section titled “Hooks used”PHP: get_products_pre, get_products, get_products_post, products_sorting, dispatch_before_display, and the twelve sync hooks listed above. Pre-controller products.pre.php (tells Instant Live Search to stay idle while the engine answers) and post-controller products.post.php (facet bar data). Template hooks: index:scripts.post, index:styles.post, products:search_results_mainbox_title.post, and the admin add-on page hooks of the license client.
Hooks provided
Section titled “Hooks provided”None in 1.0.
Template overrides
Section titled “Template overrides”Storefront templates live in design/themes/<theme>/templates/addons/kv_search_engine_pack/. Copy a template to your own theme to customize it; the add-on’s files are replaced on update.
Test status
Section titled “Test status”| Area | Result |
|---|---|
Unit tests (tests/unit.php, PHP 8.1 and 8.3) |
47 checks: text cleaning, ranking rules, filter grammar of both drivers with the visibility filters always present, synonym payloads, privacy guards, HTTP client, the Typesense driver against the documentation-based stand-in |
Meilisearch 1.58.0 (official Windows binary), driver run directly (tests/e2e/live_engine.php) |
28 checks with 10,000 and with 100,000 generated documents: typo, SKU, synonyms, combined filters, facets and price statistics, delete, update, rebuild with atomic swap while queries keep answering, engine unreachable |
Store end-to-end on CS-Cart 4.17.2 Multi-Vendor and Ultimate (tests/e2e/e2e.php) |
83/83 and 80/80: connection and encryption, full reindex (steps, busy lock, resume after a failure, no duplicates, update and delete during the build), queue (collapse, claim, lease expiry, dead letters), the real core paths for vendor status (fn_change_company_status), CSV import (fn_import) and category deletion (fn_delete_category), visibility (status, user group, storefront, vendor, inactive vendor), rules, analytics privacy, rate limit, synonyms, fallback and breaker |
HTTP end-to-end (tests/e2e/http.mjs, ratelimit.mjs) |
Ultimate 28/28; Multi-Vendor 35/35 including a vendor administrator refused on the page and on every POST action, plus engine-down 9/9 and the forged-header rate-limit check |
License (kv qa lic, gate tests/qa-gate.mjs) |
47/47 on both stores, state restored |
| Smarty | rendered admin and storefront pages on Smarty 4 (4.17.2); offline compile of all 11 templates with the Smarty 5 library of CS-Cart 4.21.2 (tests/smarty5_compile.php) |
| Install / uninstall | tables, settings, language variables and the add-on row are gone after uninstall and back after install (both stores) |
| Typesense (beta) | Not run against a real server. Typesense publishes Linux and macOS builds and a Docker image; the development PC had neither Docker nor WSL. The driver is tested against a stand-in whose responses are copied from the official documentation (tests/e2e/mock_typesense.php). One response shape (facet_counts) was not visible in the documentation text that was read and is implemented from the faceting description. Run tests/e2e/live_engine.php typesense <url> <key> against a real server before relying on it. |
| Not run | CS-Cart 4.21 runtime and the full page cache test (kv qa fpc; the 4.21 test store is not available), Multi-Vendor 4.18.3, a store with 100,000 products, an upgrade test (1.0.0 is the first version) |
Performance
Section titled “Performance”All numbers were measured on one Windows development PC (the store, the database, PHP and Meilisearch on the same machine, no network between them). They show the order of magnitude, not a promise for your hardware.
| What | Catalogue | Result |
|---|---|---|
| Meilisearch alone: PHP calls the engine over HTTP (20 ids + 2 facets, 300 queries, typos and SKUs included) | 10,000 generated documents | p50 4.2 to 6.9 ms, p95 16.3 to 27.2 ms (two runs); indexing 6.3 s (about 1,600 documents/s) |
| the same | 100,000 generated documents | p50 3.9 ms, p95 25.0 ms, max 28.6 ms; indexing 84.8 s (about 1,180 documents/s) |
Inside the store: Search::run() (500 ids + facets + price statistics, rules lookup, one PHP process) |
10,251 products (Multi-Vendor 4.17.2) | p50 33.7 ms, p95 51.5 ms, max 60.0 ms |
| Inside the store: dropdown query (10 ids + facets) | 10,251 products | p50 8.7 ms, p95 31.2 ms |
| Inside the store: full reindex, steps of 8 s | 10,254 products | 10.1 s in 3 steps (queue off) |
| Whole HTTP request, results page ranked by the engine | 10,251 products | p50 1,136 ms, p95 1,309 ms |
Whole HTTP request, the same page with the standard search (kv_sep_skip=1) |
10,251 products | p50 1,197 ms, p95 1,260 ms |
| Whole HTTP request, the suggest endpoint (query, visibility check by the core, images and prices of six products) | 10,251 products | p50 797 ms, p95 872 ms |
How to read this honestly:
- Measured: the engine alone answered the test queries in the times above, also with 100,000 documents. That figure is the engine only; no store with 100,000 products was run.
- A whole page request on this development PC costs about one second whatever the search does, because CS-Cart’s own start-up and page rendering dominate (the PC was also busy with other work). With 10,000 products the standard CS-Cart search was not slower than the engine (p50 1,197 ms against 1,136 ms): a database can still scan 10,000 names quickly. What the engine adds at that size is typo tolerance, relevance ranking, synonyms, facet counts and filters; the speed advantage is expected to show on much larger catalogs and busier servers, and it was not measured here.
- Numbers come from
tests/e2e/live_engine.php,tests/e2e/e2e.phpand a timing script over HTTP, one run each, with the engine, database and PHP on the same machine. Do not quote them as guarantees.