Elasticsearch
Elasticsearch is a search and analytics engine that stores documents and makes it easy to find text, filter results, and summarize data. Common uses include product search and log analysis.
Also known as: эластик
What Elasticsearch does
A bookstore search box needs to do more than compare strings. It should find titles that contain the words a customer typed, rank the most relevant books first, and let the customer narrow the results by format. Elasticsearch is a distributed engine built on Apache Lucene that supports those operations through one search request. It stores records as JSON documents, grouped into named collections called indices.
What happens when you index a document
An index's mapping defines the field types and how fields are indexed. For a text field, an analyzer breaks the string into tokens and can apply rules such as lowercasing. An inverted index maps the resulting terms to the documents that contain them, so a search doesn't need to scan every title. A keyword field indexes a value as a whole instead. Use it for values such as a product code or format that you want to filter on, sort by, or group by.
Each index has one or more primary shards, and each document is routed to one of them. A shard is a portion of the index that can be searched independently. Replica shards are copies that can serve searches and take over if a primary fails. A coordinating node sends each search to a copy of every relevant shard, then merges the results.
Example: find paperback books
Suppose title is mapped as text and format as keyword:
POST /books/_search
{
"query": {
"bool": {
"must": { "match": { "title": "distributed systems" } },
"filter": { "term": { "format": "paperback" } }
}
}
}
match analyzes the query text and contributes to the relevance score. By default, the search can return a result with only one of the analyzed terms; it isn't a phrase search. The format filter decides whether a book is eligible without changing its score. You could also add an aggregation to count the matching books by publisher.
Where it fits in a system
Elasticsearch is often a searchable copy of data held elsewhere. A shop might keep orders and inventory in PostgreSQL and send product updates to Elasticsearch for search. The application needs to account for lag in that pipeline rather than assume both systems always return the same version. Elasticsearch also has a visibility delay of its own: a successful write doesn't make the document searchable until a refresh. Beyond catalogs, teams use the same mix of filtering and aggregations to investigate logs, for example by counting errors per service over the last hour.
What comes up in interviews
A common debugging question is why an exact filter on a title fails even though the title is present in the document. A term query looks for a term as indexed, without analyzing the query value. Use match for analyzed text, or add a keyword subfield if you also need exact whole-title matching.