Data Management Examples
These recipes use a products collection. See the five-minute example
for a schema and starter data.
Inspect and page through data
curl "http://localhost:9200/collections?offset=0&limit=20&sort_by=name:asc"
curl "http://localhost:9200/collections/products/documents?offset=0&limit=25"
curl "http://localhost:9200/collections/products/documents/product-42?include_created_at=true"
Create a document with a known ID
Stable IDs make retries safe and simplify updates:
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents \
-H "Content-Type: application/json" \
-d '{
"id": "product-42",
"title": "USB-C Dock",
"description": "Dock with Ethernet and two display outputs",
"category": "accessories",
"price": 89.00,
"in_stock": true
}'
Omit id when a generated ID is acceptable.
Apply a partial update
Only supplied fields change:
curl --fail-with-body -X PUT \
http://localhost:9200/collections/products/documents/product-42 \
-H "Content-Type: application/json" \
-d '{"price":79.00,"in_stock":false}'
The update route has upsert behavior. Check existence first if accidentally creating a missing ID would be undesirable.
Import JSON in bulk
For small batches, send a JSON array:
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents/import \
-H "Content-Type: application/json" \
-d '[
{"id":"cable-1","title":"USB-C Cable","price":12.50},
{"id":"cable-2","title":"Thunderbolt Cable","price":35.00}
]'
For large files, stream newline-delimited JSON without loading it into shell memory:
{"id":"stand-1","title":"Laptop Stand","price":45.00}
{"id":"sleeve-1","title":"Laptop Sleeve","price":24.00}
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents/import \
-H "Content-Type: application/x-ndjson" \
--data-binary @products.ndjson
Inspect the import response rather than assuming every row succeeded.
Update documents matching a query
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents/_update_by_query \
-H "Content-Type: application/json" \
-d '{
"filter_by": "category:accessories && price:<20",
"set": {"in_stock": false}
}'
Use the exact request shape documented in Update Documents by Query for your server version.
Export selected data
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents/export \
-H "Content-Type: application/json" \
-d '{
"filter_by": "in_stock:true",
"export_fields": "id,title,price,category"
}' \
--output products-export.json
Delete narrowly, then broadly
Delete one document:
curl --fail-with-body -X DELETE \
http://localhost:9200/collections/products/documents/product-42
Before deleting by filter, run the same filter as a search and inspect found:
curl --fail-with-body -X POST \
http://localhost:9200/collections/products/documents/search \
-H "Content-Type: application/json" \
-d '{"q":"*","filter_by":"category:discontinued","limit":1}'
curl --fail-with-body -X DELETE \
"http://localhost:9200/collections/products/documents?filter_by=category:discontinued"
Replace an index without changing application code
Create and populate products_v2, then point the stable alias at it:
curl --fail-with-body -X PUT \
http://localhost:9200/aliases/products_live \
-H "Content-Type: application/json" \
-d '{"collection_name":"products_v2"}'
curl --fail-with-body http://localhost:9200/aliases/products_live
Applications can query products_live while new index versions use new physical
collection names. See Aliases for the full reindexing pattern.