Connect and run your first search
Connect a supported client, create a small index, add one document and search it. A successful result should contain the document you just wrote. Use sample data while checking the setup.
This walkthrough uses the dedicated Elasticsearch-compatible API and the JavaScript 7.x client. It is separate from the main application API connection.
- Connect a supported clientUse the supplied dedicated search endpoint.
- Create an explicit mappingChoose the field types used by this example.
- Index a test documentGive it an ID you can recognize.
- Search and inspect the hitConfirm the result contains that ID and its fields.
Before you start
Obtain the complete Elasticsearch-compatible endpoint and accepted bearer credential for your dedicated deployment. Do not construct this endpoint from the current application Connect URL. If no Elasticsearch connection is supplied, confirm availability first. See connection details.
export OC_ES_URL='https://<your-instance>/v1/tenants/<your-tenant>/es'
export OC_API_KEY='<your-api-key>'The five steps
1. Install a client
This example uses a 7.x Node client. Other clients must be tested against the supported API subset; a matching major version does not establish compatibility for every method.
npm install @elastic/elasticsearch@72. Point it at your instance
Pass the endpoint as the node and your API key as the auth. info() clears the product check and reports the version.
import { Client } from '@elastic/elasticsearch'
const es = new Client({
node: process.env.OC_ES_URL,
auth: { username: 'originchaindb', password: process.env.OC_API_KEY }
})
await es.info() // version 7.14.23. Create an index with a mapping
Declare your fields. text is analyzed for full-text search; keyword is exact and can be grouped and sorted. Indexing explains how to choose, and what happens if you skip this step.
await es.indices.create({
index: 'shop.products',
body: {
mappings: { properties: {
name: { type: 'text' },
brand: { type: 'keyword' },
price: { type: 'long' },
added: { type: 'date' }
} }
}
})4. Add a document
It is searchable the moment this returns. There is no refresh to wait for.
await es.index({
index: 'shop.products',
id: 'sku-8842',
body: {
name: 'Carbon Marathon', brand: 'Aero',
price: 149, added: '2026-09-01T00:00:00Z'
}
})5. Search it
The Query DSL you already write. This one matches the analyzed text and filters on the exact brand.
const { body: res } = await es.search({
index: 'shop.products',
body: {
query: { bool: {
must: [{ match: { name: 'marathon' } }],
filter: [{ term: { brand: 'Aero' } }]
} }
}
})
// res.hits.total.value === 1
// res.hits.hits[0]._source.name === 'Carbon Marathon'Check it from the shell
The same thing without a client, so you can prove connectivity before wiring an app:
curl "$OC_ES_URL/shop.products/_search" \
-H "Authorization: Bearer $OC_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query":{"match":{"name":"marathon"}}}'If the first call fails
A 401 means the credential was missing or rejected. Check its value, scope and required authentication header. For 404, verify the endpoint, index name and whether the document exists. If a client rejects the handshake, compare its version and requested APIs with current support.
Where to go next
-
Choose what each field does — Text, keyword, or both - and how to make one existing column searchable.
-
Load real data — Bulk ingest, and how to guard a write against a concurrent one.
-
Summarise it — Group, count, average, bucket by day, or pull the top documents per bucket.
-
See it modelled — Worked examples for a catalog, a log stream, transactions and tickets.
Elasticsearch is a trademark of Elasticsearch B.V., registered in the U.S. and in other countries. OriginChainDB is not affiliated with, endorsed by, or sponsored by Elasticsearch B.V. OriginChainDB implements a compatible HTTP API so that existing Elasticsearch clients can talk to it; it does not distribute Elasticsearch software.