An Elasticsearch cheat sheet is a single-page reference for the commands and Query DSL you reach for daily: index and document operations, searches and aggregations, vector / kNN search, ESQL, and cluster management, against Elasticsearch 9.x. It also carries the full runbook for a zero-downtime reindex with aliases, in both curl and Kibana Dev Tools form.
Quick reference
Elasticsearch 9.x Quick Reference
REST endpoints, Query DSL fragments, aggregations, and admin commands for Elasticsearch 9.x.
I run Elasticsearch behind a few production search experiences and an analytics pipeline. The sections below are organized in the order you actually hit them: first connect to the cluster, then create an index and load documents, then search, then aggregate, then tune.
What this cheat sheet covers
Use this page as a reference card. Each section is a self-contained chunk: skip to Search DSL if you already have a populated index, skip to Aggregations if you need facet counts, skip to Vector / kNN search if you are wiring up semantic search, or to Reindex with zero downtime when a mapping change forces a new index. The end-of-page Common mistakes and FAQ sections answer the questions I get asked most.
What this sheet does NOT cover:
- Elastic's commercial features (Watcher, machine learning UI, cross-cluster replication). Those need an enterprise license. The free / open AGPL+ELv2 stack covers everything below.
- Kibana dashboards. Kibana is its own product with its own docs.
- The legacy Transport client. It was removed in Elasticsearch 8.0. Use the REST clients (Java, Python
elasticsearch, Node@elastic/elasticsearch, or raw HTTP).
Connecting to Elasticsearch
Every command in this sheet is an HTTP request. I show them in curl form because that is the universal denominator. The same calls work verbatim in Kibana Dev Tools (paste without the curl -X prefix), in httpie, or through any official client.
A local single-node 9.x cluster started fresh requires basic auth (the elastic user) and HTTPS by default. The first-time setup prints the password to the console; reset it any time with:
bin/elasticsearch-reset-password -u elasticA typical authenticated request:
curl -k -u elastic:CHANGEME https://localhost:9200/_cluster/healthThe -k skips self-signed cert verification. For production, use a real certificate and pass --cacert instead. Most of the URLs in the rest of this sheet are shown without the host prefix; assume https://localhost:9200 (or wherever your cluster lives).
Search DSL: a worked example
The Query DSL is more verbose than the URI query string but composable. The idiom I reach for first is a bool query with must for text relevance and filter for binary conditions:
curl -k -u elastic:CHANGEME -X POST "https://localhost:9200/products/_search" \
-H 'Content-Type: application/json' -d '{
"size": 20,
"query": {
"bool": {
"must": [
{ "match": { "name": "wireless headphones" } }
],
"filter": [
{ "term": { "in_stock": true } },
{ "range": { "price": { "lte": 200 } } }
]
}
},
"sort": [
"_score",
{ "rating": "desc" }
],
"_source": ["name", "price", "rating", "image_url"]
}'What each part does:
mustruns the user query through thenamefield's analyzer and contributes to_score.filterenforces "in stock and under $200" without touching the score; these clauses are cached.sortranks by relevance first, then by rating as a tiebreaker._sourcerestricts the response to the four fields the UI actually renders, saving bandwidth.
Aggregations: facets and analytics
Aggregations turn search results into facet counts, time series, and summary statistics. A typical product-search facet response:
curl -k -u elastic:CHANGEME -X POST "https://localhost:9200/products/_search" \
-H 'Content-Type: application/json' -d '{
"size": 0,
"query": { "match": { "name": "headphones" } },
"aggs": {
"by_brand": { "terms": { "field": "brand.keyword", "size": 10 } },
"price_bucket": { "histogram": { "field": "price", "interval": 50 } },
"stats_price": { "stats": { "field": "price" } }
}
}'"size": 0 skips the hits and just returns the aggregations. That is the right pattern when you only need counts.
Aggregations can nest. To get the average price by brand:
"aggs": {
"by_brand": {
"terms": { "field": "brand.keyword", "size": 10 },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}Vector / kNN search
Elasticsearch 8.x added native vector search; 9.x stabilized it as the recommended path for semantic retrieval. Define a dense_vector field in the mapping, then run a knn query with a query vector you compute client-side (or use semantic_text to skip the client step).
Mapping:
{
"mappings": {
"properties": {
"title": { "type": "text" },
"embedding": { "type": "dense_vector", "dims": 768, "index": true, "similarity": "cosine" }
}
}
}Query:
{
"knn": {
"field": "embedding",
"query_vector": [0.012, -0.041, ...],
"k": 10,
"num_candidates": 100
}
}num_candidates controls recall vs latency. A common ratio is 10× k. For hybrid search that combines lexical relevance with vector similarity, put both knn and a bool/match query into the same request:
{
"query": { "match": { "title": "noise cancelling" } },
"knn": { "field": "embedding", "query_vector": [...], "k": 10, "num_candidates": 100, "boost": 0.7 }
}For the broader context on how vector search fits with embeddings, document chunking, and a working app, see how to build RAG with embeddings and vector search and how to add semantic search to a MySQL app.
ESQL: the SQL-like query language
Introduced in 8.11 and refined through the 8.x and 9.x lines, ESQL gives Elasticsearch a piped query language that reads like SQL with a Unix-pipeline twist. For analytics, dashboards, and log exploration it is often easier than building the equivalent Query DSL by hand.
FROM logs-2026.05.*
| WHERE status >= 400 AND status < 500
| STATS count = count() BY host, status
| SORT count DESC
| LIMIT 20
When to use which:
| Use case | DSL | ESQL |
|---|---|---|
| Search-relevance tuning, scoring | Yes | No (no relevance scoring) |
| Time-series analytics, log queries | Possible but verbose | Yes |
| Joins across multiple indices | No | Yes (LOOKUP JOIN, tech preview in 8.18/9.0, GA in 8.19/9.1) |
| Aggregations | Yes | Yes (cleaner syntax) |
| Vector / kNN search | Yes | Not yet |
Reindex with zero downtime using an alias
To reindex Elasticsearch without search downtime, create a replacement index, copy documents with the _reindex API, validate it, then move the application's alias in one _aliases request. Searches keep using the same alias throughout. This is how you apply a mapping change you cannot make in place (see the PUT /:index/_mapping and POST /_reindex rows in the quick reference).
An alias swap does not synchronize writes. The copy can miss later inserts, updates, and deletes. This walkthrough keeps search available while pausing Elasticsearch writes from before the copy until after cutover. That is a write maintenance window, not uninterrupted read/write availability. If your application cannot pause or durably queue writes, use the change-capture approach before following the cutover steps.
Each request has a curl version and a matching Kibana Dev Tools Console version. Run one version of each request, inspect the result, and only then continue. The REST APIs used here are documented for Elasticsearch 8.x and 9.x; this is a documentation-reviewed walkthrough, not a claim that every release and deployment combination has been execution-tested, so rehearse the exact requests against your version and a representative dataset first. Managed or Serverless deployments can restrict index settings and privileges, and OpenSearch has a separate version and security model.
The pattern: applications query a stable alias
The application uses products, an alias pointing to products_v3. Build products_v4 separately, then move products once the replacement is ready. The application keeps the same endpoint. You can explore both states in the graphic; its labels follow the settings below.
An alias must have a different name from any existing index or data stream. If your application currently addresses a physical index called products, you cannot create an alias with that name alongside it. Introduce a distinct alias, deploy the application change, and verify every reader and writer uses it before migrating. Do not delete the existing index to free the name.
When a mapping change needs a reindex
Changing an existing field's type or its index-time analyzer generally means rebuilding documents in a new index. Adding a new field is different: the mapping API supports it. A new keyword multi-field is also allowed, but old documents need rewriting with _update_by_query or reindexing to populate it. Some mapping parameters, including search_analyzer, can be updated without rebuilding the index. See Elastic's update mapping API and search analyzer documentation.
Adding a dense_vector mapping does not generate embeddings. You need an embedding process or an appropriate ingest pipeline to supply vectors. The example here uses ordinary product fields so it can focus on the migration itself.
Setup: use your own values in curl and Kibana
Set the host, current index, new index, and application alias. Both command versions and the diagram update with these values. Values are saved in this browser; do not enter credentials.
In Kibana: open Dev Tools → Console on the deployment you intend to change. Paste one request, place the cursor inside it, and click its play button or press Ctrl+Enter / Cmd+Enter. Execute each request individually. A request is the GET, PUT, POST, or DELETE line plus its entire JSON body, when present; do not execute JSON lines separately. Do not select and run the whole walkthrough. Console can submit multiple requests sequentially, but an asynchronous reindex returns before the copy finishes. Console request controls.
Kibana uses its connected Elasticsearch cluster and your Kibana permissions. The host field affects curl only; changing it here cannot switch Kibana to another cluster. Compare GET / in Console with the curl response, including cluster_uuid, before continuing. These are REST requests in Console syntax, not KQL or ES|QL queries.
In a terminal: the curl examples use Bash/zsh syntax and curl 7.76+ (--fail-with-body). Set the username once; curl will prompt for its password when each request runs. Use a migration account with the required read, write, create-index, alias-management, settings, and monitoring privileges.
export ES_USER='elastic'For a self-managed cluster with a private certificate authority, configure its trusted CA certificate before running curl:
export CURL_CA_BUNDLE='/absolute/path/to/http_ca.crt'Skip that environment variable when the endpoint already has a certificate trusted by your system. Keep certificate verification enabled. For API-key authentication, replace the --user option with your locally configured authorization header. Do not enter credentials into this page.
Check the cluster identity
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/'GET /Before changing anything, confirm all of these:
- You have a recoverable snapshot or source-of-truth rebuild path, enough disk for both indices and replicas, and a tested mapping.
- This is a regular index migration within one cluster. Data streams, remote reindexing, filtered aliases, custom routing, and lifecycle-managed indices need additional planning.
- The source has
_sourceenabled. Reindexing reads it; it does not copy the original mapping, settings, or templates for you. Reindex prerequisites. - Your replacement starts empty, receives no application writes, and does not inherit the application alias from an index template. One operator controls the migration.
- All producers can pause or durably queue writes, including background jobs, bulk importers, deletes, and direct-to-index clients. Blocking an index alone does not queue writes.
Step 1: Check the alias and source configuration
1a. Inspect the application alias
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/_alias/products'GET /_alias/productsExpect exactly the current index with this alias under aliases. Stop if it points somewhere else, spans multiple indices, has is_write_index: false, or carries a filter or routing options you have not accounted for. Preserve any required alias options in both forward and rollback changes; the example below assumes a plain, single-index alias.
Only if the response explicitly reports alias not found (404), create the alias. Authentication failures and connectivity errors are not missing aliases.
1b. Create the alias only if it is missing
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{
"actions": [
{
"add": {
"index": "products_v3",
"alias": "products",
"is_write_index": true
}
}
]
}'POST /_aliases
{
"actions": [
{
"add": {
"index": "products_v3",
"alias": "products",
"is_write_index": true
}
}
]
}Wait for acknowledged: true, then repeat 1a. If you are introducing this alias, deploy and verify the application's switch to it before continuing.
1c. Inspect the source mapping
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v3/_mapping'GET /products_v3/_mapping1d. Record the source settings
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v3/_settings?include_defaults=true'GET /products_v3/_settings?include_defaults=trueRecord analyzers, shard count, replica policy, refresh interval, routing, and ingest-pipeline settings. Build the replacement from an explicit, reviewed definition. Do not blindly copy generated settings such as the UUID or creation date.
Step 2: Create the new index with the required mapping
This is a small example product mapping. Replace it with the schema your actual documents require, including custom analyzers and dynamic templates. Here name.keyword supports sorting and aggregations. The shard and replica choices are examples, not sizing advice.
2a. Create the empty replacement index
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request PUT 'https://localhost:9200/products_v4' \
--header 'Content-Type: application/json' \
--data '{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "60s"
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"description": {
"type": "text"
},
"price": {
"type": "double"
},
"in_stock": {
"type": "boolean"
}
}
}
}'PUT /products_v4
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "60s"
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"description": {
"type": "text"
},
"price": {
"type": "double"
},
"in_stock": {
"type": "boolean"
}
}
}
}Wait for creation to be acknowledged. If the index already exists, inspect it and choose a fresh destination name; do not delete it to make the example work. A 60-second refresh interval can reduce refresh overhead while copying. It controls search visibility, not a disk commit. Keeping a replica avoids deliberately dropping redundancy; zero replicas is an optional performance tradeoff only when the source is recoverable and you accept rebuilding the destination after a failure. Indexing-speed guidance.
2b. Inspect the destination, including inherited aliases and settings
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v4'GET /products_v42c. Refresh the destination before checking that it is empty
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/products_v4/_refresh'POST /products_v4/_refresh2d. Confirm zero destination documents
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v4/_count'GET /products_v4/_countRequire count: 0, no failed shards, no application alias on the destination, and no unexpected ingest pipeline. A template can add settings or aliases even when the create request does not include them. If the new index unexpectedly received the live alias, stop and repair that routing before proceeding.
Step 3: Pause writes, refresh the source, and start reindexing
Pause producers first and drain their in-flight work. Keep new operations in a durable queue or stop accepting writes for the maintenance window. Reads may continue. Then add a write block as a guard against a forgotten producer:
3a. Block writes to the old index after pausing producers
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request PUT 'https://localhost:9200/products_v3/_block/write'PUT /products_v3/_block/writeWait for success for every index/shard in the response. The dedicated index-block API accounts for in-flight writes before acknowledging the block. An unexpected writer now gets an error; your application must retain or retry that operation rather than lose it.
3b. Make the last source writes visible to reindex
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/products_v3/_refresh'POST /products_v3/_refreshCheck _shards.failed: 0. A refresh makes recent operations searchable, which matters because reindex reads through search. Leave the source write-blocked through validation and cutover.
3c. Start one asynchronous reindex task
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/_reindex?wait_for_completion=false&slices=auto&requests_per_second=500' \
--header 'Content-Type: application/json' \
--data '{
"source": {
"index": "products_v3",
"size": 1000
},
"dest": {
"index": "products_v4",
"op_type": "create"
}
}'POST /_reindex?wait_for_completion=false&slices=auto&requests_per_second=500
{
"source": {
"index": "products_v3",
"size": 1000
},
"dest": {
"index": "products_v4",
"op_type": "create"
}
}The response should contain a task such as node_id:12345. This means the task started, not that the copy finished. Save the complete value, including the node ID and colon, in the task field below. Start the task once; repeat the status request instead of resubmitting _reindex.
op_type: create refuses to overwrite existing destination documents. A conflict is a reason to stop in this empty-destination workflow. Do not add conflicts: proceed just to get past it. slices: auto can parallelize work; the parent status can advance in jumps as slices finish. The throttle is an example starting point, not a guarantee of acceptable load. Monitor query latency, heap, disk, and rejections. Reindex execution and throttling.
If the start request times out without returning an ID, inspect running reindex tasks before deciding whether to retry:
3d. Find running reindex tasks only if the start response was lost
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/_tasks?actions=*reindex&detailed=true'GET /_tasks?actions=*reindex&detailed=trueStep 4: Check reindex progress and wait for successful completion
4a. Poll this task until it finishes
Paste the complete task ID returned by the reindex request (node ID:number) before copying this request.
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/_tasks/<task_id>'GET /_tasks/<task_id>Repeat 4a every 5–10 seconds, or less often for a large copy. It may take minutes or hours. Do not advance to the swap while completed is false.
When it becomes true, inspect the whole response. For this unfiltered copy into an empty, private destination, require:
- No top-level
error. - A
responseobject withtimed_out: falseand an emptyfailuresarray. version_conflicts: 0.createdequal tototal;updated,deleted, andnoopsequal to zero.
completed: true means the task stopped; it does not prove success. A missing task (404), lost node, cancellation, malformed response, or document failure needs investigation. Preserve the old alias and inspect the partial destination before planning a retry. Do not assume that rerunning the request resumes safely. Task status.
Step 5: Validate documents, queries, and replica readiness
5a. Refresh the destination after the copy
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/products_v4/_refresh'POST /products_v4/_refresh5b. Count the frozen source
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v3/_count'GET /products_v3/_count5c. Count the replacement
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v4/_count'GET /products_v4/_countRequire no failed shards and equal counts for this full, write-paused migration. A lower count is not an acceptable expected gap. Equal counts are necessary here but do not prove equal content: an update can change a document without changing the total.
Compare representative document IDs and source values, then run your application's actual queries directly against the new index. Check relevance, sorting, filters, aggregations, routing, and latency. For the example mapping, this is a basic search-and-sort smoke test:
5d. Smoke-test the replacement mapping
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v4/_search' \
--header 'Content-Type: application/json' \
--data '{
"size": 5,
"query": {
"match_all": {}
},
"sort": [
{
"name.keyword": "asc"
}
]
}'GET /products_v4/_search
{
"size": 5,
"query": {
"match_all": {}
},
"sort": [
{
"name.keyword": "asc"
}
]
}Check timed_out: false and _shards.failed: 0, then inspect the returned documents. Replace this query with your real application queries as well; one generic search cannot validate the migration.
Restore your intended production settings. The example uses one replica and a one-second refresh interval. Substitute the values you chose for this deployment rather than assuming those are universal defaults.
5e. Apply the intended production settings
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request PUT 'https://localhost:9200/products_v4/_settings' \
--header 'Content-Type: application/json' \
--data '{
"index": {
"number_of_replicas": 1,
"refresh_interval": "1s"
}
}'PUT /products_v4/_settings
{
"index": {
"number_of_replicas": 1,
"refresh_interval": "1s"
}
}5f. Wait for the destination replica to be ready
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/_cluster/health/products_v4?wait_for_status=green&timeout=60s'GET /_cluster/health/products_v4?wait_for_status=green&timeout=60sRequire timed_out: false and the expected health state. This example needs enough eligible data nodes to allocate its replica. A single-node lab with one replica stays yellow; choose an explicit lab replica policy instead of ignoring a production allocation failure. A health wait timing out means inspect allocation and repeat the check, not proceed. Cluster health.
Step 6: Swap the alias atomically, verify, then resume writes
Repeat 1a immediately before cutover. It must still resolve only to the old index. Coordinate with other operators and automation so nobody changes it during the migration.
Run both actions together as one request. Never issue separate remove and add requests.
6a. Move the alias in one request
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{
"actions": [
{
"remove": {
"index": "products_v3",
"alias": "products",
"must_exist": true
}
},
{
"add": {
"index": "products_v4",
"alias": "products",
"is_write_index": true
}
}
]
}'POST /_aliases
{
"actions": [
{
"remove": {
"index": "products_v3",
"alias": "products",
"must_exist": true
}
},
{
"add": {
"index": "products_v4",
"alias": "products",
"is_write_index": true
}
}
]
}must_exist: true makes a missing removal fail instead of allowing a partial action list. Check acknowledged: true, no reported action errors, and the resulting alias. This avoids a deliberate gap in alias routing; it does not guarantee every application request succeeds regardless of cluster health or client behavior. Alias action semantics.
6b. Verify the alias points only to the new index
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/_alias/products'GET /_alias/productsIf cutover times out or returns an ambiguous result, inspect 6b before retrying. The change may already have applied.
6c. Test search through the application alias
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products/_search' \
--header 'Content-Type: application/json' \
--data '{
"size": 5,
"query": {
"match_all": {}
}
}'GET /products/_search
{
"size": 5,
"query": {
"match_all": {}
}
}Verify hits come from the new _index, check for shard failures, and test the real application. Only then resume producers and replay queued operations through the alias in order. Wait for that queue to drain before declaring the search index current. Keep the old index write-blocked so a missed direct writer fails visibly. Monitor failed writes and search latency.
When writes must continue during reindex
A background reindex is a copy, not an ongoing replication subscription. If uninterrupted writes and up-to-date searches are required, the migration needs a separate consistency protocol:
- Capture a durable change-log position before the baseline copy starts. Retain inserts, updates, and delete events, including document IDs and routing.
- Build the replacement, then replay changes into it in order. If replay overlaps the baseline, use a tested version-ordering strategy so older copied documents cannot overwrite newer changes.
- Establish a cutover barrier: drain in-flight writers and apply changes through a known position before moving reads. A brief ingestion pause or a coordinated writer handoff is usually necessary. Prove there is no untracked gap.
- Route subsequent writes to the replacement, drain remaining events, and monitor lag. Maintain the old index too if you need a rollback that preserves post-cutover writes.
Idempotency alone does not establish ordering. A second _reindex pass or an updated_at query does not reliably catch deletes and can race with new writes. Replaying only after switching reads exposes an incomplete index until replay finishes. These are application-level design decisions; the commands above do not implement change capture.
Rollback: check for new writes before moving back
The retained old index is a fallback for search, but it stops receiving writes after cutover. Once the replacement has accepted writes, an immediate reverse swap exposes stale data. Pause producers, account for in-flight operations, reconcile post-cutover inserts, updates, and deletes into the old index, and validate that its mapping can represent them. If it cannot, fix forward or rebuild from the source of truth.
Only when the old index is current and validated, or while all writes have remained paused since before the original copy, reverse the routing:
Rollback A. Swap back only after validating the old index
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request POST 'https://localhost:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{
"actions": [
{
"remove": {
"index": "products_v4",
"alias": "products",
"must_exist": true
}
},
{
"add": {
"index": "products_v3",
"alias": "products",
"is_write_index": true
}
}
]
}'POST /_aliases
{
"actions": [
{
"remove": {
"index": "products_v4",
"alias": "products",
"must_exist": true
}
},
{
"add": {
"index": "products_v3",
"alias": "products",
"is_write_index": true
}
}
]
}Repeat the alias and application checks. The old index is still write-blocked. Before resuming producers after a successful rollback, remove that block:
Rollback B. Unblock the old index before resuming writes
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request PUT 'https://localhost:9200/products_v3/_settings' \
--header 'Content-Type: application/json' \
--data '{
"index.blocks.write": false
}'PUT /products_v3/_settings
{
"index.blocks.write": false
}The same unblock request applies if you abandon the migration before cutover: confirm the alias still points only to the old index, stop any unwanted copy, inspect the failure, then unblock it and resume producers. Do not leave the application unintentionally stuck in its write maintenance window.
Keep the old index until the rollback window closes
Retain it for your agreed observation period, with a snapshot or reproducible rebuild path. Check all aliases, direct clients, dashboards, background jobs, and recovery requirements before deletion. Keeping an index for a day does not by itself make rollback safe.
Cleanup A. Inspect every alias on the old index
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request GET 'https://localhost:9200/products_v3/_alias'GET /products_v3/_aliasThe next request is optional and destructive. Run it separately, after the observation period and only when the old index is no longer needed. It deletes the entire named index, not just the application's alias.
Cleanup B. Delete the retired index only after sign-off
curl --fail-with-body --silent --show-error --user "$ES_USER" \
--request DELETE 'https://localhost:9200/products_v3'DELETE /products_v3Optional bash helper: copy and validate, then stop for review
The helper replaces the copy-and-count portion of steps 3–5. Do not run it after already starting 3c. It requires curl 7.76+, jq, an existing empty destination with your reviewed mapping, paused producers, and a write-blocked source. Set ES_USER and your CA trust as above. It prompts once for the password and does not print it.
bash es-zero-downtime-reindex.sh --host 'https://localhost:9200' --old 'products_v3' --new 'products_v4' --alias 'products'It stops on HTTP errors, missing task IDs, task errors, timeouts, document failures, conflicts, unexpected task counts, or mismatched document counts. It leaves settings, write blocks, aliases, and index deletion to the operator. It cannot prove document equality, query correctness, or application readiness. Continue the manual validation in 5d–5f, then review cutover in step 6. The older --yes and --rollback automation options are no longer supported.
Review the helper source
#!/usr/bin/env bash
# Copy and validate only. Alias cutover is intentionally manual.
# https://techearl.com/elasticsearch-zero-downtime-reindex
# Requires Bash, curl 7.76+, jq; regular indices in one Elasticsearch cluster.
set -euo pipefail
HOST="${ES_HOST:-https://localhost:9200}"
OLD_INDEX=""
NEW_INDEX=""
ALIAS=""
TASK=""
usage() {
cat <<'HELP'
Usage: bash es-zero-downtime-reindex.sh --old NAME --new NAME --alias NAME [--host URL]
Prepare the destination mapping, pause all producers, drain in-flight writes,
and add PUT /OLD/_block/write first. The destination must be empty and private.
This helper refreshes the source, starts one reindex, polls it, and checks counts.
It NEVER swaps aliases, changes settings, unblocks writes, or deletes indices.
Auth: ES_USER (default elastic), then a password prompt; or ES_AUTH=user:password.
TLS: set CURL_CA_BUNDLE to your private CA file if needed. Verification stays on.
--yes and --rollback are no longer supported. Review the article's manual steps.
HELP
}
fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
while [[ $# -gt 0 ]]; do
case "$1" in
--old|--new|--alias|--host)
[[ $# -ge 2 && -n "$2" ]] || fail "Missing value for $1"
case "$1" in
--old) OLD_INDEX="$2" ;;
--new) NEW_INDEX="$2" ;;
--alias) ALIAS="$2" ;;
--host) HOST="$2" ;;
esac
shift 2 ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; fail "Unsupported argument: $1" ;;
esac
done
for name in "$OLD_INDEX" "$NEW_INDEX" "$ALIAS"; do
[[ "$name" =~ ^[a-z0-9][a-z0-9._-]*$ && ${#name} -le 255 ]] || fail "Use individual lowercase names, starting with a letter or number (no wildcards)."
done
[[ "$OLD_INDEX" != "$NEW_INDEX" && "$OLD_INDEX" != "$ALIAS" && "$NEW_INDEX" != "$ALIAS" ]] || fail "Index and alias names must all differ."
[[ "$HOST" =~ ^https?://[^/?#@[:space:]\"\']+(/[^?#@[:space:]\"\']*)?$ ]] || fail "Invalid host; do not put credentials, query strings, or fragments in the URL."
HOST="${HOST%/}"
command -v curl >/dev/null || fail "curl is required"
command -v jq >/dev/null || fail "jq is required"
AUTH="${ES_AUTH:-}"
if [[ -z "$AUTH" ]]; then
[[ -t 0 ]] || fail "Set ES_AUTH for non-interactive use, or run in a terminal for the password prompt."
read -r -s -p "Password for ${ES_USER:-elastic}: " password
printf '\n'
AUTH="${ES_USER:-elastic}:$password"
unset password
fi
[[ "$AUTH" != *$'\n'* && "$AUTH" != *$'\r'* ]] || fail "Credentials cannot contain newlines."
# Keep credentials out of command-line arguments and output.
AUTH_CONFIG="${AUTH//\\/\\\\}"
AUTH_CONFIG="${AUTH_CONFIG//\"/\\\"}"
unset AUTH ES_AUTH
es() {
curl --config <(printf 'user = "%s"\n' "$AUTH_CONFIG") \
--fail-with-body --silent --show-error --connect-timeout 15 --max-time 90 \
--header 'Content-Type: application/json' "$@"
}
json_request() {
local response
if ! response=$(es "$@"); then
printf '%s\n' "$response" >&2
fail "HTTP/transport failure. Do not retry a mutation until its cluster state is known."
fi
printf '%s' "$response" | jq -e 'type == "object" and (has("error") | not)' >/dev/null || fail "Invalid or error API response."
printf '%s' "$response"
}
check_shards() {
jq -e '._shards.failed == 0 and ._shards.successful > 0' >/dev/null || fail "Missing shard status or shard failures."
}
check_alias() {
json_request "$HOST/_alias/$ALIAS" | jq -e --arg old "$OLD_INDEX" --arg alias "$ALIAS" '
(keys == [$old]) and (.[$old].aliases[$alias] | type == "object" and
(has("filter") | not) and (has("routing") | not) and
(has("index_routing") | not) and (has("search_routing") | not) and
(.is_write_index != false))' >/dev/null || fail "Alias must point only to the source with no filter/routing and permit writes."
}
check_source_block() {
json_request "$HOST/$OLD_INDEX/_settings?flat_settings=true" | jq -e --arg old "$OLD_INDEX" '
(keys == [$old]) and (.[$old].settings["index.blocks.write"] | . == "true" or . == true)' >/dev/null \
|| fail "Source must be an exact index with a write block. Pause producers, then use PUT /OLD/_block/write."
}
count_index() {
local response
response=$(json_request "$HOST/$1/_count")
printf '%s' "$response" | check_shards
printf '%s' "$response" | jq -er '.count | select(type == "number" and . >= 0)'
}
reminder() {
local code=$?
if [[ -n "$TASK" ]]; then
printf '\nTask ID: %s\n' "$TASK" >&2
if [[ $code -ne 0 ]]; then
printf 'The task may still be running. Inspect it before starting another copy.\n' >&2
fi
fi
printf 'No alias/settings/deletion changes were made by this helper. Keep producers paused until cutover, or restore the old write path deliberately.\n' >&2
}
trap reminder EXIT
printf 'Checking alias, source write block, and destination...\n'
check_alias
check_source_block
json_request "$HOST/$NEW_INDEX" | jq -e --arg new "$NEW_INDEX" '
(keys == [$new]) and (.[$new].aliases == {})' >/dev/null || fail "Destination must be an exact index with no aliases."
json_request -X POST "$HOST/$NEW_INDEX/_refresh" | check_shards
[[ "$(count_index "$NEW_INDEX")" == "0" ]] || fail "Destination is not empty. Choose a new index and inspect the partial copy."
json_request -X POST "$HOST/$OLD_INDEX/_refresh" | check_shards
OLD_COUNT=$(count_index "$OLD_INDEX")
BODY=$(jq -nc --arg old "$OLD_INDEX" --arg new "$NEW_INDEX" \
'{source:{index:$old,size:1000},dest:{index:$new,op_type:"create"}}')
printf 'Starting one asynchronous copy. If no task ID returns, inspect GET /_tasks?actions=*reindex&detailed=true before retrying.\n'
START=$(json_request -X POST "$HOST/_reindex?wait_for_completion=false&slices=auto&requests_per_second=500" --data "$BODY")
TASK=$(printf '%s' "$START" | jq -er '.task | select(type == "string" and test("^[A-Za-z0-9_-]+:[0-9]+$"))') || fail "Missing or invalid task ID; inspect running tasks before retrying."
printf 'Task: %s\n' "$TASK"
while true; do
RESULT=$(json_request "$HOST/_tasks/$TASK")
printf '%s' "$RESULT" | jq -e '.completed | type == "boolean"' >/dev/null || fail "Task response has no completed flag."
if [[ "$(printf '%s' "$RESULT" | jq -r '.completed')" == "true" ]]; then break; fi
printf 'Copy is still running. Waiting 5 seconds...\n'
sleep 5
done
printf '%s' "$RESULT" | jq -e '
.response | type == "object" and .timed_out == false and .failures == [] and
.version_conflicts == 0 and (.total | type == "number") and
.created == .total and .updated == 0 and .deleted == 0 and .noops == 0' >/dev/null \
|| { printf '%s\n' "$RESULT" >&2; fail "Task completed without a clean full-copy result. Inspect it; do not cut over."; }
check_source_block
check_alias
json_request -X POST "$HOST/$NEW_INDEX/_refresh" | check_shards
FINAL_OLD_COUNT=$(count_index "$OLD_INDEX")
NEW_COUNT=$(count_index "$NEW_INDEX")
CREATED=$(printf '%s' "$RESULT" | jq -r '.response.created')
[[ "$OLD_COUNT" == "$FINAL_OLD_COUNT" && "$OLD_COUNT" == "$NEW_COUNT" && "$OLD_COUNT" == "$CREATED" ]] \
|| fail "Count mismatch: source before=$OLD_COUNT, source now=$FINAL_OLD_COUNT, destination=$NEW_COUNT, task created=$CREATED."
printf 'Copy checks passed: %s documents. The alias still points to %s.\n' "$NEW_COUNT" "$OLD_INDEX"
printf 'Next: compare document contents and application queries, restore intended settings, wait for replicas, and review the manual alias swap.\n'Version compatibility
| Feature | Available since | Notes |
|---|---|---|
Single type per index (no _type) | 7.0 | Multi-type indices are gone. References to types in older tutorials are obsolete. |
| Composable index templates | 7.8 | Old index_template API replaced by index_templates + component_templates. |
| Searchable snapshots | 7.10 | Mount a snapshot as a read-only index in the frozen tier. |
| Runtime fields | 7.11 | Compute fields at query time from _source or other fields. Cheap mapping additions. |
dense_vector indexed for kNN | 8.0 | Required for the knn query. |
| ESQL | 8.11 | General-availability piped query language. Major addition. |
semantic_text field type | 8.15 | Embedding inference at index and query time, no external embedding step. |
| Elasticsearch 8.16 | 2024 | License returned to open AGPL + ELv2 alongside the existing free Basic. |
For specific upgrade walkthroughs, the official Elasticsearch upgrade documentation is the source of truth. Always snapshot before upgrading.
Common mistakes
The bugs I have shipped or seen in code review.
Searching a keyword field with match, or a text field with term. A match runs the value through the analyzer (lowercase, tokenize); a term does not. If your category field is keyword, { "match": { "category": "Footwear" } } will look for the analyzed form, which a keyword field never produced. Use term. Conversely, term against a text field looks for the exact analyzed token (often lowercased), so { "term": { "title": "Headphones" } } misses Headphones if the analyzer lowercased it to headphones.
Using from/size to paginate past the 10,000-result wall. The default index.max_result_window caps from + size at 10,000 because deep pagination forces every shard to maintain a deep priority queue. Raising the setting is almost always the wrong fix. Use search_after with a unique sort field (usually _id as a tiebreaker) or a Point In Time (PIT) for stable cursor pagination.
Sorting on a text field. Text fields do not support sorting unless you enable fielddata: true (which loads every term into memory). The correct fix is to add a keyword subfield in the mapping (fields: { keyword: { type: "keyword" } }) and sort on name.keyword instead.
Forgetting to refresh after indexing in a test. Elasticsearch refreshes every second by default. If your test indexes a doc and immediately queries it back, the doc is not yet searchable. Append ?refresh=true to the index call, or call POST /:index/_refresh. Do NOT set refresh_interval to a tiny value in production; the cost in segment count is brutal.
Letting unmapped fields run wild. Without an explicit mapping, Elasticsearch auto-detects types from the first document. A field that holds "42" on doc 1 and 42 on doc 2 will end up text and queries with term fail in confusing ways. Define mappings up front for anything that matters.
Treating shards as cheap. Each shard is a Lucene index with overhead. Hundreds of small shards per node will starve heap. The general guidance: 10-50GB per shard for search workloads, fewer larger shards over more smaller ones, total shards per node roughly 20 × heap_in_GB.
Running a single-node cluster in production. A one-node cluster has number_of_replicas: 1 by default and cannot allocate the replica anywhere; cluster status sits at yellow. Either set replicas to 0 (knowing you have no redundancy) or run at least two data nodes. Don't ignore the yellow.
Snapshotting without a registered repository. Snapshots require a repository registered ahead of time; PUT /_snapshot/:repo once, then PUT /_snapshot/:repo/:snapshot. Many teams discover at recovery time that they never set this up. Test the restore path before you need it.
Frequently asked questions
Elasticsearch is a distributed search and analytics engine. It's the right tool when you need full-text search (a product catalog, a knowledge base, a log explorer), faceted browsing (filter by brand, price range, category), or analytics over high-cardinality data (count error rates per host per minute, percentile latencies).
It is the WRONG tool for primary transactional storage. Use a real database (PostgreSQL, MySQL) for the source of truth and index a subset of fields into Elasticsearch for search.
Relational databases optimize for ACID transactions, foreign keys, joins, and precise queries. Elasticsearch optimizes for full-text relevance ranking, faceted aggregations across millions of documents, and horizontal scale via sharding.
Practical differences: Elasticsearch has no real joins (denormalize at index time), no transactions across documents, eventual consistency on writes (one-second default refresh), and a schema that allows mixed types per index if you're not careful. The wins are ranked full-text search, fast aggregations, and easy horizontal scaling.
An index is a logical namespace for documents (conceptually like a SQL table). A document is a single JSON object stored in an index, identified by an `_id`. A shard is the physical unit: each index has 1 or more primary shards, each of which is a Lucene index living on a node.
You read and write at the index level; Elasticsearch routes operations to the right shard automatically. You scale by adding shards (split an index across more nodes) or adding replicas (more read throughput, redundancy).
Two paths. The simplest is Docker:
docker run -p 9200:9200 -e "discovery.type=single-node" -e "ELASTIC_PASSWORD=changeme" docker.elastic.co/elasticsearch/elasticsearch:9.0.0
Then run a one-off Kibana container against the same network, or just use `curl` directly. For a long-running setup, install the package on your OS (apt / yum / brew) so it runs as a service with proper systemd integration. The bundled `elasticsearch-reset-password -u elastic` resets the initial password whenever you lose it.
Most slow searches fall into one of four buckets:
- Wildcard / regex queries on `text` fields: `*foo*` style queries scan every term. Use a properly mapped `keyword` field, or `wildcard` field type for native wildcard support.
- Deep pagination: `from: 9000, size: 100` forces every shard to sort and ship 9100 docs. Switch to `search_after` or PIT.
- Sorting on an analyzed field: Sort on the `.keyword` subfield instead.
- Too many shards: A query fans out to every shard of the queried indices. Consolidate small indices (use rollover or reindex) so a query hits a sane number of shards.
If too MANY docs match, your query is probably underusing `filter` clauses. Move binary conditions out of `must` into `filter` and the result set tightens up.
Both started from the same Lucene-based codebase; OpenSearch forked in 2021 after the Elastic license change, and Amazon maintains it. In 2024 Elastic added AGPL back to Elasticsearch (it landed in the 8.16 release line) alongside the existing open Basic license, so the licensing reason to fork is mostly gone.
Today: Elasticsearch leads on newer search features (vector search, ESQL, semantic_text). OpenSearch leads on AWS-managed integration (if you're on AWS, Amazon OpenSearch Service is cheaper to operate than self-managed Elastic on EC2). The APIs overlap heavily; client code that targets 7.x-compatible endpoints generally runs on both, but new Elastic-only features won't.
match runs your query through the analyzer of the target field (lowercase, tokenize, remove stop words depending on the analyzer). It is the right query for `text` fields and full-text search.
term does NOT run the analyzer. It looks for an exact token in the inverted index. It is the right query for `keyword` fields, IDs, enums, booleans, dates, and IP fields.
The most common bug is querying a `keyword` field with `match` (works by accident sometimes) or a `text` field with `term` (silently misses everything that the analyzer normalized). Check the mapping with `GET /:index/_mapping` before debugging further.
Types were removed in Elasticsearch 7.0. Before that, an index could contain documents of multiple "types" sharing the same mapping. The feature caused more confusion than value and was deprecated in 6.x.
The migration was to give each former type its own index (one index per type), or use a discriminator field within a single index. Any tutorial that uses URLs of the form /:index/:type/_search is from 6.x or older and the code will not run on a modern cluster.
See also
- How to Run Elasticsearch in Docker: a local environment for rehearsing a reindex before production
- MySQL Cheat Sheet: the companion reference for relational queries and the primary store you typically index FROM into Elasticsearch
- How to Build RAG with Embeddings and Vector Search: end-to-end semantic search pipeline that uses the
dense_vectormapping above - How to Add Semantic Search to a MySQL App: the lighter-weight alternative when you don't want to run a full Elasticsearch cluster
- Regex Cheat Sheet: for the regex queries Elasticsearch supports on
keywordandwildcardfields - curl Cheat Sheet: the HTTP client used in every example above, including the flags for self-signed certs and basic auth
- Elasticsearch ransomware: how an open port wiped my database: why the
DELETEand_cat/indicescalls above are a wipe-the-cluster attack chain when the node is exposed without auth, and how to lock it down
External references: Elasticsearch official documentation is the source of truth for endpoint behavior and version-specific changes. The Elastic Search Labs blog covers the newer ESQL, vector search, and semantic_text features in depth.
Sources
Authoritative references this article was fact-checked against.
- ES|QL LOOKUP JOIN, Elastic Search Labselastic.co
- Elasticsearch is open source again, Elastic blogelastic.co
- Elasticsearch Java Transport Client, official docselastic.co
- What's new in Elasticsearch 8.15, Elastic blogelastic.co
- Elasticsearch API: Reindex documents (prerequisites)elastic.co
- Elasticsearch: Reindex indices (execution, slicing, throttling)elastic.co
- Elasticsearch: Aliases (atomic alias actions)elastic.co
- Elasticsearch: Index blocks (write block)elastic.co
- Elasticsearch API: Get task informationelastic.co
- Elasticsearch API: Update field mappingselastic.co
- Kibana: Dev Tools Consoleelastic.co
- Elasticsearch: Aggregationselastic.co
- Elasticsearch: Dense vector field typeelastic.co





