Core concepts
Index
An index is a named collection of documents that share a similar structure — analogous to a table in MySQL. Index names must be lowercase. You can have one index per entity type (e.g.,products, orders) or combine related entities into a single index with distinct field sets.
- green — all primary and replica shards are allocated and active.
- yellow — all primary shards are active, but at least one replica shard is unallocated. Reads and writes work; the cluster has no redundancy for affected shards.
- red — at least one primary shard is unallocated. Some data is unavailable.
Document
A document is a single JSON object stored inside an index. It is the smallest unit Elasticsearch indexes and returns. Every document has a_id field (auto-generated or user-specified) and an _index field indicating which index it belongs to.
Shard
Elasticsearch horizontally partitions each index into shards. Each shard is an independent Lucene index that can be hosted on any node in the cluster. Sharding lets you store more data than fits on a single machine and parallelize search queries across multiple nodes. You setnumber_of_shards at index creation time and cannot change it afterward without reindexing. Choose a shard count that fits your expected data volume and leaves room to grow.
Replica
A replica is an exact copy of a primary shard hosted on a different node. Replicas serve two purposes:- Fault tolerance — if the node holding a primary shard fails, a replica is promoted to primary automatically.
- Read throughput — search queries can be routed to any replica, distributing read load.
number_of_replicas on a live index without reindexing.
Mapping and field types
Mapping defines how Elasticsearch stores and indexes each field — the equivalent of a table schema. Elasticsearch can infer mapping from the first document you index (dynamic mapping), but for production use you should define explicit mappings to control field types and prevent unintended behavior.Key field types
The critical distinction is between
keyword and text:
keywordfields store the raw string and support only equality and prefix queries.textfields are tokenized — split into individual terms by an analyzer — and support full-text queries. The trade-off is thattextfields cannot be sorted or aggregated efficiently.
Query DSL
Elasticsearch’s Query DSL lets you express searches as JSON objects sent in the request body of aGET /_search request. Every query returns a hits array with matching documents and a _score representing relevance.
match_all
Returns every document in the index.term
Exact-match query forkeyword, numeric, date, or boolean fields. Does not analyze the query value.
match
Full-text query fortext fields. Analyzes the query string using the same analyzer as the field.
range
Returns documents where a field value falls within a specified range.bool
Combines multiple queries with boolean logic. Usemust (AND), should (OR), and must_not (NOT).
multi_match
Runs the same query string against multiple fields simultaneously.Highlighting
You can ask Elasticsearch to return the matched fragment with the matching terms wrapped in HTML tags.How the inverted index works
Elasticsearch builds an inverted index for everytext field. A normal (forward) index maps documents to words; an inverted index maps words to documents. This is what makes full-text search fast.
Build time (indexing):
- The analyzer splits the field value into terms (tokenization, lowercasing, stop-word removal, stemming depending on configuration).
- For each term, Elasticsearch records the document ID, the position of the term within the document, and frequency of occurrence.
- The resulting mapping from term → document list is stored in the Lucene segment files on disk.
- The query string is analyzed using the same analyzer.
- Elasticsearch looks up each resulting term in the inverted index to get a list of document IDs.
- For multi-term queries, Elasticsearch intersects (AND) or unions (OR) the document lists.
- Documents are scored using TF-IDF or BM25 and sorted by score.
Index management
Create an index with settings
Index a document
Update a document (partial)
Delete a document
Bulk operations
The_bulk API processes multiple create, update, and delete operations in a single request. Operations in a bulk request are not atomic — individual operations can fail without rolling back the others.
Check index health
When to use Elasticsearch vs. MySQL vs. Redis
Elasticsearch is eventually consistent by design. After you index a document, it becomes searchable only after the next refresh (default every 1 second). Do not use Elasticsearch as your primary database for transactional data that must be immediately consistent.