Framework
A user searching a company name might type an exact code, a fragment of a name or a descriptive phrase. The search module can combine results from several strategies. Identify which index owns the missing record before rebuilding anything.
The @open-mercato/search module provides a pluggable search architecture with multiple strategy support. It orchestrates parallel execution across backends, merges results using Reciprocal Rank Fusion, and handles graceful degradation when providers are unavailable.
Tip · Read this first – the mental model that prevents bugs
Search is often described as "three interchangeable query-time strategies." That is only the read side. Underneath there are three physically independent stores, each with its own owner, writer, and reindex pipeline – and no single command rebuilds all of them. In particular, the CLI command yarn mercato search reindex does not populate the Meilisearch fulltext index. See Three stores, three pipelines before touching indexing.
@open-mercato/searchTokenSearchStrategy (id tokens), VectorSearchStrategy (id vector), FullTextSearchStrategy (id fulltext)searchService, searchStrategies, searchIndexer registered at bootsearch.index_record and search.delete_record eventsThe three built-in strategy ids are declared in packages/shared/src/modules/search.ts:
export type SearchStrategyId = 'tokens' | 'vector' | 'fulltext' | (string & Record<string, never>)
FullTextSearchStrategy is backed by a pluggable fulltext driver; today the only implemented driver is Meilisearch (createFulltextDriver() returns it when MEILISEARCH_HOST is set). The DI wiring registers each strategy that is not skipped:
export function registerSearchModule(container: SearchContainer, options?: SearchModuleOptions): void {
const strategies: SearchStrategy[] = []
// Token strategy (always available) — Postgres search_tokens via Kysely
if (!options?.skipTokens) {
const db = container.resolve<EntityManager>('em').getKysely()
strategies.push(new TokenSearchStrategy(db))
}
// Vector strategy (registered even if not yet available; checked lazily at search time)
if (!options?.skipVector) {
const embeddingService = container.resolve<EmbeddingService>('vectorEmbeddingService')
const drivers = container.resolve<VectorDriver[]>('vectorDrivers')
strategies.push(new VectorSearchStrategy(embeddingService, drivers[0]))
}
// Fulltext strategy — wraps a pluggable driver (Meilisearch today)
if (!options?.skipFulltext) {
const fulltextDriver = createFulltextDriver({ host: process.env.MEILISEARCH_HOST, /* ... */ })
if (fulltextDriver) strategies.push(new FullTextSearchStrategy(fulltextDriver))
}
const searchService = new SearchService({
strategies,
defaultStrategies: determineDefaultStrategies(strategies), // prefers fulltext > vector > tokens
fallbackStrategy: 'tokens',
mergeConfig: {
duplicateHandling: 'highest_score',
strategyWeights: { fulltext: 1.2, vector: 1.0, tokens: 0.8 },
},
})
container.register({
searchService: asValue(searchService),
searchStrategies: asValue(strategies),
searchIndexer: asValue(new SearchIndexer(searchService, options?.moduleConfigs ?? [], {
// without it, every custom field is left out of buildSource / formatResult / resolveUrl / resolveLinks
encryptedCustomFieldKeysResolver: createEncryptedCustomFieldKeysResolver(container.resolve<EntityManager>('em').getKysely()),
})),
})
}
The strategy layer is the read interface. Writes land in three independent physical stores:
| Store | Backend | Owned by | Written by | Read by |
|---|---|---|---|---|
search_tokens | Postgres table | query_index module | The tokenizer during projection (buildSearchTokenRows / replaceSearchTokensForRecord in query_index/lib/search-tokens.ts) | TokenSearchStrategy and directly by list-API routes |
| Fulltext | Meilisearch | @open-mercato/search | The fulltext driver, driven by the fulltext-indexing queue/worker | FullTextSearchStrategy |
| Vector | pgvector / qdrant / chromadb | @open-mercato/search + query_index | VectorSearchStrategy.index via the vector-indexing queue; embeddings from an EmbeddingService | VectorSearchStrategy |
Key invariant: rebuilding one store does not rebuild the others. They have separate population pipelines, described next.
Emitted from query_index/subscribers/upsert_one.ts (event query_index.upsert_one):
entity_indexes projection row (read-your-writes for list endpoints).reindexSearchTokensForRecord → rebuilds search_tokens for the record.query_index.vectorize_one → vector store.search.index_record → fulltext_upsert subscriber → enqueues onto fulltext-indexing → worker → searchService.index → fulltext driver → Meilisearch.// query_index/subscribers/upsert_one.ts (deferred block)
await reindexSearchTokensForRecord(em, { ...doc }) // search_tokens
await bus.emitEvent('query_index.vectorize_one', { ... }) // vector store
await bus.emitEvent('search.index_record', { // -> fulltext-indexing -> Meilisearch
entityId: 'customers:customer_person_profile',
recordId: '123',
tenantId: 'tenant-abc',
organizationId: 'org-xyz',
})
Deletes mirror this via search.delete_record.
There are three separate reindex entry points, each covering a different subset of stores:
| Entry point | Rebuilds | Populates Meilisearch? |
|---|---|---|
CLI yarn mercato search reindex | entity_indexes projection + search_tokens + emits vector events | No |
Core API POST /api/query_index/reindex (feature query_index.reindex) | Same reindexEntity path – projection + tokens + vector | No |
Search API POST /api/search/reindex (feature search.reindex) | Clears and repopulates the reindexed scope in the Meilisearch index (the whole tenant index only for an unscoped full reindex) | Yes – the only fulltext bulk path |
Vector API POST /api/search/embeddings/reindex (feature search.manage) | Vector store only | No |
Warning · The search reindex trap
yarn mercato search reindex is a tokens + projection + vector reindex. It does not touch Meilisearch, despite the historical help string. To rebuild the fulltext index after a bulk data change, call POST /api/search/reindex. For the fulltext path to actually drain you need QUEUE_STRATEGY=async and a running worker: yarn mercato search worker fulltext-indexing.
Which store answers which query:
GET /api/search/global) – uses the tenant's saved strategy set (default ['fulltext','vector','tokens']). Fulltext (Meilisearch) is the intended fuzzy/typo-tolerant backend; tokens participate as an always-available fallback. GET /api/search additionally accepts a strategies override.search.hybrid_search) – resolves the same SearchService.SearchService. Each list route (customers, auth users, customer_accounts, messages) queries search_tokens directly, because searchable PII columns are encrypted at rest and ILIKE on ciphertext cannot match plaintext. This is why the token index must exist even when Meilisearch is configured.fieldPolicy.hashOnly: email, phone, tax_id) – served by token-hash presence, plus a dedicated emailHash column for email.Modules opt in by exporting searchConfig from src/modules/<module>/search.ts.
import type { SearchModuleConfig } from '@open-mercato/shared/modules/search'
export const searchConfig: SearchModuleConfig = {
defaultStrategies: ['fulltext', 'tokens'],
entities: [
{
entityId: 'customers:customer_person_profile',
enabled: true,
priority: 10,
buildSource: async (ctx) => {
const { record, customFields } = ctx
return {
text: [record.preferred_name, record.first_name, record.last_name, record.job_title],
presenter: {
title: record.preferred_name ?? `${record.first_name} ${record.last_name}`,
subtitle: record.job_title,
icon: 'user',
},
}
},
formatResult: async (ctx) => ({
title: ctx.record.preferred_name ?? `${ctx.record.first_name} ${ctx.record.last_name}`,
subtitle: ctx.record.job_title,
icon: 'user',
}),
resolveUrl: async ({ record }) => `/backend/customers/${record.entity_id}`,
fieldPolicy: {
searchable: ['preferred_name', 'first_name', 'last_name', 'job_title'],
hashOnly: ['email', 'phone'],
excluded: ['date_of_birth', 'government_id'],
},
},
],
}
export default searchConfig
| Callback | Purpose |
|---|---|
buildSource | Returns text to index and optional presenter metadata |
formatResult | Shapes the result payload for UI consumers |
resolveUrl | Primary URL when a result is clicked |
resolveLinks | Additional action links (edit, view) |
fieldPolicy | Controls which fields go to external providers |
All strategies implement the SearchStrategy interface:
interface SearchStrategy {
readonly id: SearchStrategyId
readonly name: string
readonly priority: number
isAvailable(): Promise<boolean>
ensureReady(): Promise<void>
search(query: string, options: SearchOptions): Promise<SearchResult[]>
index(record: IndexableRecord): Promise<void>
delete(entityId: string, recordId: string, tenantId: string): Promise<void>
bulkIndex?(records: IndexableRecord[]): Promise<void>
purge?(entityId: string, tenantId: string): Promise<void>
}
tokens, priority 10)Reads the Postgres search_tokens table (hashed tokens) via Kysely. Always available, works with encrypted data.
const strategy = new TokenSearchStrategy(db)
await strategy.search('john doe', { tenantId: 'tenant-123' })
vector, priority 20)Wraps an EmbeddingService and a pluggable VectorDriver (pgvector / qdrant / chromadb). Requires a configured embedding provider.
const strategy = new VectorSearchStrategy(embeddingService, vectorDriver)
await strategy.search('customer support issues', { tenantId: 'tenant-123' })
fulltext, priority 30)Wraps a pluggable FullTextSearchDriver for fast full-text fuzzy search with typo tolerance. The only implemented driver today is Meilisearch (chosen automatically when MEILISEARCH_HOST is set).
const driver = createFulltextDriver({
host: process.env.MEILISEARCH_HOST,
apiKey: process.env.MEILISEARCH_API_KEY,
})
const strategy = new FullTextSearchStrategy(driver)
await strategy.search('jhon doe', { tenantId: 'tenant-123' }) // handles typos
SearchService.search() runs the selected strategies in parallel (Promise.allSettled), then mergeAndRankResults combines them with weighted Reciprocal Rank Fusion (RRF):
// RRF score: weight / (k + rank + 1), k = 60 (constant), rank = position in strategy results
const merged = mergeAndRankResults(results, {
duplicateHandling: 'highest_score',
strategyWeights: {
fulltext: 1.2, // Boost fulltext (Meilisearch) results
vector: 1.0,
tokens: 0.8, // Lower weight for token results
},
minScore: 0.01,
})
A strategy that fails or is unavailable is skipped, not fatal; if none are available the service falls back to tokens.
Per-entity fieldPolicy controls which fields reach which store:
searchable – fuzzy-indexed (Meilisearch / vector text).hashOnly – hashed into search_tokens for exact/prefix match without exposing plaintext (email, phone, tax_id).excluded – never indexed anywhere (passwords, secrets, government ids).In addition, the tokenizer applies a global blocklist: the built-in password,token,secret,hash plus anything in OM_SEARCH_FIELD_BLOCKLIST.
The search module subscribes to events emitted by the query_index module. The full incremental chain is:
record write
→ query_index.upsert_one (subscriber)
→ entity_indexes projection (sync)
→ reindexSearchTokensForRecord → search_tokens
→ query_index.vectorize_one → vector store
→ search.index_record → fulltext_upsert subscriber
→ fulltext-indexing queue
→ worker → searchService.index → Meilisearch
// Emitted from query_index/subscribers/upsert_one.ts
await bus.emitEvent('search.index_record', {
entityId: 'customers:customer_person_profile',
recordId: '123',
tenantId: 'tenant-abc',
organizationId: 'org-xyz',
})
// Emitted from query_index/subscribers/delete_one.ts
await bus.emitEvent('search.delete_record', {
entityId: 'customers:customer_person_profile',
recordId: '123',
tenantId: 'tenant-abc',
})
Orchestrates indexing by resolving entity configurations and building IndexableRecord objects:
const indexer = container.resolve<SearchIndexer>('searchIndexer')
await indexer.indexRecord({
entityId: 'customers:customer_person_profile',
recordId: '123',
tenantId: 'tenant-abc',
record: { first_name: 'John', last_name: 'Doe' },
customFields: { title: 'CEO' },
})
While it indexes a record, the indexer keeps custom fields whose definition is encrypted out of the context it passes to buildSource, formatResult, resolveUrl and resolveLinks: it removes them from ctx.customFields, from the record's cf: / cf_ keys and from every record a callback loads through ctx.queryEngine, so their plaintext stays out of the source text, presenter and links that strategies store or embed. IndexableRecord.fields keeps the original values for hashed search_tokens indexing, while plaintextExcludedCustomFields keeps the encrypted keys out of fulltext document fields and vector fallback text (see #7894). When the definitions cannot be read, the indexer leaves out every custom field of that entity and reports search.encrypted_custom_fields_unresolved. An indexer constructed without encryptedCustomFieldKeysResolver leaves out every custom field and reports search.encrypted_custom_fields_resolver_missing: resolve searchIndexer from the container, or pass createEncryptedCustomFieldKeysResolver(em.getKysely()) from @open-mercato/search/di when you construct SearchIndexer yourself. Search-time presenter enrichment still calls the same callbacks with decrypted values to render results; its output is not stored.
The exported opt-in VectorIndexService applies the same callback and query-engine filtering contract. If an application constructs it directly, pass the same encryptedCustomFieldKeysResolver; omission or lookup failure excludes every custom field for that indexing operation.
# Fulltext — Meilisearch (enables FullTextSearchStrategy)
MEILISEARCH_HOST=http://localhost:7700
MEILISEARCH_API_KEY="<meilisearch-api-key>"
MEILISEARCH_INDEX_PREFIX=om
SEARCH_EXCLUDE_ENCRYPTED_FIELDS=false # keep encrypted fields out of the fulltext index
# Vector (enables VectorSearchStrategy via embedding providers)
OPENAI_API_KEY="<openai-api-key>"
# See vector-search.mdx for all embedding provider options
# Async indexing (required for the fulltext + vector queues to drain)
QUEUE_STRATEGY=async
REDIS_URL=redis://localhost:6379
The OM_SEARCH_* flags below are token/Postgres-only – they tune the search_tokens index and have no effect on Meilisearch or the vector store:
| Variable | Default | Controls |
|---|---|---|
OM_SEARCH_ENABLED | true | Master kill-switch for token building and TokenSearchStrategy. |
OM_SEARCH_MIN_LEN | 3 | Minimum token length; also the floor of prefix expansion. |
OM_SEARCH_ENABLE_PARTIAL | true | Prefix/partial expansion – indexing "john" stores hashes for joh,john. Enables prefix matching at the cost of ~5–6× more search_tokens rows. |
OM_SEARCH_HASH_ALGO | sha256 | Token hash algorithm (sha1/md5 also accepted). |
OM_SEARCH_STORE_RAW_TOKENS | false | Store plaintext token alongside the hash – security-sensitive; avoid in production. |
OM_SEARCH_FIELD_BLOCKLIST | – | Comma-separated extra field names excluded from tokenization (merged with built-in password,token,secret,hash). See below for entity-scoped entries. |
OM_SEARCH_MAX_FIELD_CHARS | 20000 | Maximum input characters considered per field value before splitting or prefix expansion; 0 disables the limit. |
OM_SEARCH_MAX_TOKENS_PER_FIELD | 5000 | Maximum distinct token rows across all values of one field; 0 disables the limit. |
OM_SEARCH_MAX_TOKENS_PER_RECORD | 20000 | Maximum token rows across all fields in one indexed record; 0 disables the limit. Spent in the document's key order – see the note below. |
OM_SEARCH_DEBUG | false | Verbose token/indexing debug logging (redacted – never logs raw tokens or PII). |
The per-record budget is spent field by field in the order the indexed document's keys iterate in,
and the record stops tokenizing at the first field that exhausts it. On a record that fits under the
limit this is invisible; on one that exceeds it, which fields stay searchable follows that key
order, starving from the end of it first – and the two write paths order it differently. The batch
reindex path (buildIndexDocument) writes base columns, then cf:* keys, then the aggregate
search_text field last, so search_text starves first. The incremental single-record write path
(buildIndexDoc) additionally inserts l10n:{locale}:{field} translation keys between the cf:*
keys and search_text, so those translation keys consume budget the batch path would have spent on
search_text: on a translated record over budget, search_text starves even sooner, and the
translation fields starve next, before any cf:* key or base column. Each path is self-consistent because it always tokenizes the
in-memory document it is about to write. Code that recomputes a record's expected tokens must not
read the document back out of the entity_indexes.doc jsonb column first – Postgres stores jsonb
keys in a canonical order (by key length, then byte order) rather than insertion order, so the same
record can truncate at a different field on the way back. Rebuild the document with the builder that
matches the write path being checked – buildIndexDoc for incrementally-written records,
buildIndexDocument for batch-reindexed ones.
OM_SEARCH_FIELD_BLOCKLIST gates both the per-field tokens and the aggregate search_text
field that every entity's search actually queries. Because search_text concatenates the
record's string fields, a field must be blocklisted for it to stay out of the index – you
cannot blocklist search_text itself without disabling search for that entity.
Each comma-separated entry matches field names by substring (case-insensitive), and may carry
an optional entityType@ prefix to scope it to a single entity type:
# `body` is excluded everywhere; `notes` only on customer interactions
OM_SEARCH_FIELD_BLOCKLIST=body,customers:customer_interaction@notes
This matters for volume as much as for privacy: with prefix expansion enabled, a large
free-text column such as an imported e-mail body can dominate search_tokens.
Changes take effect for documents built after the change – reindex the affected entities
(yarn mercato query_index reindex --entity <entityType>) to drop tokens that are already
stored.
search_text and encrypted fieldsThe document persisted in entity_indexes stays encrypted at rest, so the aggregate stored
alongside it concatenates ciphertext for every field an encryption map covers. Search tokens
are therefore built from a decrypted copy of the document, with search_text recomposed
from those plaintext values – otherwise the aggregate would be tokenized from ciphertext and
could never match what a user types. Indexes written before this behavior shipped keep their
ciphertext-derived search_text tokens until they are rewritten: a record heals on its next
ordinary write, which rebuilds its tokens, so a reindex
(yarn mercato query_index reindex --entity <entityType>) is only needed to heal records
nothing writes to again.
Third-party packages can register custom strategies via DI:
import { addSearchStrategy } from '@open-mercato/search'
import { AlgoliaStrategy } from './algolia-strategy'
// In your module's di.ts
export function register(container: AppContainer) {
if (process.env.ALGOLIA_APP_ID) {
addSearchStrategy(container, new AlgoliaStrategy({
appId: process.env.ALGOLIA_APP_ID,
apiKey: process.env.ALGOLIA_API_KEY,
}))
}
}
Adding a new fulltext backend (Typesense, Elasticsearch, …) is better done as a FullTextSearchDriver behind FullTextSearchStrategy – shipped in a dedicated provider package – so it reuses the existing indexing pipeline. See the search architecture spec (.ai/specs/2026-07-24-search-architecture-clarification-and-evolution.md) for the roadmap.