@sonamu-kit/opensearch is an optional package that indexes a Sonamu application’s PostgreSQL data in OpenSearch and searches it. Each domain transaction records in PostgreSQL which documents changed; a separate worker reads those records and applies them to OpenSearch. Searches read only the index that the PostgreSQL catalog points to.
Read this page to decide whether the package fits your application, then continue with Setup.
What it does and does not do
The package:- Records the indexing intent in the same PostgreSQL transaction as the domain write. A domain write never sends a request to OpenSearch.
- Applies those intents to OpenSearch with a worker. If a document is sent more than once or a request arrives late, the highest revision stays.
- Rebuilds when a definition changes: it fills a new index from the PostgreSQL source, then switches searches to it by moving a read pointer.
- Takes native OpenSearch DSL as the search body and passes only the keys it allows.
- Detect table changes. There is no CDC, no trigger, and no entity decorator; the application calls
markChangedon every write path that affects the index. - Create a schema or an index when the server starts. The application’s migration installs the schema, and an operator creates indexes with administrative commands.
- Ship analyzers, dictionaries, or mappings. A definition carries OpenSearch settings JSON as is.
- Decide authorization. Who may see a search result is the application’s call.
Guarantees and non-guarantees
What the package does not guarantee:
- Exactly-once delivery. When the same revision is sent twice, the second request fails with a version conflict; the worker records the document as done only after confirming that the indexed content equals what it tried to send.
- That commits and search results change together. Code that expects a just-committed change to be searchable at once is wrong.
- An unbounded buffer. While the worker is stopped, intents and ledger rows keep accumulating in PostgreSQL. Monitor the backlog.
Moving parts
- Ledger (
documents): one revision per document, raised by one on eachmarkChangedcall. Deleting a document leaves its ledger row in place. - Intents (
deliveries): one row per target saying “send this document up to this revision”. This is the outbox. - Worker: claims intents under a lease, runs the projection in a read-only snapshot of the primary, and sends the results in bulk requests.
- Target: a versioned physical index
snm-<namespace>-<logical key>-<target ID>plus the alias<physical index>-wthe worker writes through. A definition change means a new target. - Read pointer (
read_heads): which target searches read. It lives in PostgreSQL rather than in an OpenSearch alias, so cutover and rollback happen in a transaction.
sonamu_search by default), not in public, and none of them is a Sonamu entity.
Three handles
Marking, indexing, and searching are separate handles.
An API process can therefore mark and search while the worker runs in another process. Creating a handle opens no connection, starts no timer, and touches no schema; connections open only on an actual operation or on
start(). Installing the package turns nothing on.
What the application owns
- Projections: the function that turns source rows into search documents, and the schema of those documents. See Search definitions.
- Mappings, settings, analyzers: a definition’s
settingsandmappingshold OpenSearch JSON as is. The package only checks their shape. - Authorization: the search filter, and re-checking search results on the primary.
- Marking every write path: creates, updates, deletes, and changes the database makes on its own (foreign-key CASCADE and SET NULL) must all call
markChanged. An unmarked change never reaches the index. See Transactional indexing. - Operations: PostgreSQL roles and OpenSearch credentials, running index creation and cutover, and monitoring the backlog.
Supported versions
Analyzer plugins
The package neither ships nor requires analyzers. If you need a morphological analyzer, install the OpenSearch plugin on the cluster yourself and list its name in the definition’srequiredPlugins. The administrative handle checks that list at two points:
planCreatechecks every node, lists missing plugins inmissingPlugins, and only adds aplugins-missingwarning.- The actual creation (
executeCreate, andbeginRebuild, which takes the same path) refuses withplugins-missingbefore any remote command or PostgreSQL write if a plugin is missing.
analysis-nori and analysis-sudachi 3.6.0 both installed; on OpenSearch 3.9.0 the Sudachi plugin did not install, because its newest release targeted 3.5.0. If you need Japanese analysis, check plugin support yourself before upgrading the engine.
Next steps
Setup
Schema migration, roles, client, recommended values
Search definitions
Projections, mappings, fingerprints
Transactional indexing
Marking changes together with domain writes
Indexing worker
Running the worker, outcomes, monitoring