Elasticsearch: understand indexing and search visibility
Learn how Elasticsearch mappings, shards, and refresh work together, and why a successful write may not appear in search immediately.
Elasticsearch: understand indexing and search visibility
A successful write to Elasticsearch does not always appear in a search immediately. Elasticsearch analyzes and indexes each document according to its mapping, assigns it to a shard, and exposes recent changes to search during a refresh. Understanding this flow helps you choose field types, plan shards, and diagnose read-after-write surprises. This introduction reflects Elastic’s official documentation checked on 23 November 2026; examples are illustrative and were not run on a cluster.
Documents, mappings, and settings
An index is the unit where related JSON documents are stored and searched. Its mapping defines field types and how values are indexed; its settings control behavior such as shard counts and refresh intervals. Choose field types for the question you will ask: text fields are analyzed into terms for full-text search, while keyword fields keep a value suitable for exact filters, sorting, and aggregations. A multi-field can provide both forms of one value. For example, search a product name as text but filter on its exact category as a keyword.
Dynamic mapping infers types from incoming values and is useful while exploring data. In production, an inferred date or numeric-looking identifier may not match your intended meaning, so explicit mappings give more control. Changing a field type later generally requires a new index and reindexing its documents.
Shards distribute work
A shard stores part of an index and can handle indexing and search independently. Every document belongs to one primary shard. Replica shards are copies that add redundancy and can serve read requests. The primary-shard count is fixed when an index is created; the replica count can be adjusted later. Plan primary shards for data volume and workload instead of choosing a large number without a sizing reason. Some deployments, including Elastic Cloud Serverless, manage shard operations for you, so check the deployment’s responsibilities.
A write is not always immediately searchable
Elasticsearch is near-real-time: a successful indexing response means the write was accepted, but a subsequent search may not see it until a refresh makes recent changes searchable. Elastic documents list a default refresh interval of 1 second for Elastic Stack and 5 seconds for Elastic Cloud Serverless. These are not guaranteed per-request delays: settings and search-idle behavior can affect refreshes.
Usually omit the refresh parameter and let the default behavior work. If a specific workflow must search a just-written document before responding, refresh=wait_for waits for a refresh rather than forcing one immediately. refresh=true forces immediate visibility, but frequent forced refreshes create small segments and extra indexing, search, and merge work. If automatic refresh is disabled, wait_for may wait indefinitely until another action refreshes the shard.
Follow one example from mapping to query
The following illustrative example maps a product name for full-text search, its category and SKU for exact values, and price as a number. It is not a reported test:
PUT /products
{
"mappings": {"properties": {
"name": {"type": "text"},
"category": {"type": "keyword"},
"sku": {"type": "keyword"},
"price": {"type": "double"}
}}
}PUT /products/_doc/SKU-104?refresh=wait_for
{"name":"Wireless travel mouse","category":"Accessories","sku":"SKU-104","price":29.5}GET /products/_search
{"query":{"match":{"name":"wireless mouse"}}}The match query analyzes its input for full-text search. Here, wait_for makes the indexing request wait until its change is searchable. Without it, the write is normally more efficient, but an immediate search can miss the new document until refresh.
Choose and troubleshoot deliberately
| Need | Starting point | Check |
|---|---|---|
| Find words in sentences | text and match | Inspect the analyzer’s tokens if results seem surprising. |
| Exact category, state, or SKU | keyword | Define any required case-normalization rules. |
| Search immediately after writing | Default first; wait_for when necessary | The request may take longer while waiting. |
| Plan a growing index | Decide mappings and primary shards before creation | Primary-shard count cannot be changed directly after creation. |
If a result is missing, check the mapping and analyzer first, then consider whether refresh has occurred. If full-text search works but filters or aggregations do not, check whether the field should be keyword. If a field has the wrong type, plan a correctly mapped destination index and reindex rather than assuming a mapping update can change existing indexed values. This sequence links field meaning, distribution, and search visibility to the application’s actual needs.
Sources
[Elasticsearch index fundamentals](https://www.elastic.co/docs/manage-data/data-store/index-basics)
[The refresh parameter](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/refresh-parameter)
[Text field type](https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/text)
[Clusters, nodes, and shards](https://www.elastic.co/docs/deploy-manage/distributed-architecture/clusters-nodes-shards)
[Match query](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-match-query)