What ElasticSearch is and terminology mapping (Slides 2–13)
ElasticSearch (ES) is a real-time, distributed search and analytics engine built on the Apache Lucene search library (Slides 3, 5). It stores complex JSON documents (dates, geolocations, text, nested objects, arrays), exposes a RESTful JSON/HTTP API usable from any programming language, scales horizontally, and logs every modification in transaction logs replicated across nodes to avoid data loss (Slides 5–6). Real-world examples include GitHub (code search), Wikipedia (full-text search with highlighted snippets), and StackOverflow (full-text plus geolocation) (Slide 4).
SQL → ElasticSearch mapping (Slides 8–10)
SQL (Relational)
ElasticSearch
Notes
database
cluster
Groups indices across machines
table
index
Collection of JSON documents
row
document
Top-level JSON object, uniquely identified by (Index, Id) (ES can auto-generate Id)
column
field
Flexible schema; a field can hold multiple values of the same type (a list)
The three overloaded meanings of index (Slides 11–12, 21)
Noun (index): A collection of documents (like a SQL table).
Verb (to index): Inserting a document into an index (if a document with that ID already exists, it is replaced).
Inverted index: An auxiliary data structure that accelerates full-text retrieval—just like the analytic index at the back of a textbook (Slide 21) listing each unique word alongside the list of documents (pages) where it appears. Every field in a document is indexed by default in ES, and all inverted indices are consulted during search; any non-indexed field is not searchable (Slide 12).
Exact values vs. full text and the Analyzer pipeline (Slides 15–23)
ElasticSearch supports two distinct search modes—and combinations of both (Slide 15):
[Hard] Exact values (Slide 17): Traditional types (integer, float, date, or exact unanalyzed string like username/email). Answers a binary question: “Does this document match the query?”
[Soft] Full text (Slides 18–19): Human-language text (tweets, email bodies). Answers: “How well does this document match the query?” using relevance. Full-text search must account for abbreviations (USA vs. United States of America), plurals/verb conjugations (cat vs. cats, does vs. did), synonyms (game vs. competition), and word order (fox news hunting vs. fox hunting news).
Mapping defines how data in each field is interpreted; ES can dynamically generate a mapping by “guessing” data types (such as recognizing dates) (Slide 16).
Analysis and the 3-stage Analyzer pipeline (Slides 22–23)
To populate an inverted index and prepare queries, an Analyzer processes full text in three sequential stages:
Character filters: Clean the raw string before tokenization (e.g., converting & to and).
Tokenizers: Split the string into individual terms/tokens (e.g., using whitespace or punctuation as separators).
Token filters: Operate on individual terms—changing terms (lowercasing, stemmingcats → cat), removing terms (stopwords), or adding terms (synonyms).
Golden rule (Slide 22): Both the indexed text and the query string must be processed by the same analyzer. (Added example outside the PDF): If "Dogs & Cats" is indexed through an analyzer that converts & → and, lowercases, and stems (["dog", "and", "cat"]), an unanalyzed exact lookup for "Dogs" will fail to match "dog" in the inverted index.
More efficient — typically shrinks candidate documents before scoring
More expensive (scores every matching document)
Rule of thumb (Slide 25): Use query clauses for full-text search or any condition that should affect _score; use filter clauses for everything else.
(Added e-commerce example outside the PDF): To find in-stock products priced <= 100 ranked by how well description matches "running shoes", put price <= 100 and in_stock = true in filter (fast exact pruning) and description: "running shoes" in query (relevance scoring).
Query DSL syntax (Slides 26–33)
Queries are written in Query DSL as JSON bodies sent via HTTP POST (/_search across all indices, index1/_search, or rooms,students/_search across multiple indices), returning the top 10 relevant hits by default (Slides 26, 33; index types inside an index were deprecated in ES 7.0).
match query (Slides 27–29, 32):
POST departments/_search{ "query": { "match": { "name": "John" } }}
On a full-text field, match analyzes the query string with the field’s analyzer and computes _score; on an exact / not_analyzed string field, it checks exact equality and returns _score = 1 (Slide 32).
Compound bool query (Slides 30–31): Combines multiple clauses and sums _score across matching must/should clauses:
must → AND condition (e.g., title: "developer")
should → OR condition (e.g., name: "John" or name: "Mark", controlled with "minimum_should_match": 1)
must_not → NOT condition (e.g., exclude lastname: "Smith")
Data modifications: immutable documents (Slides 34–38)
Insert (PUT /index_name/<id> — Slide 35): Inserts a JSON document; if <id> is omitted, ES generates a unique identifier (e.g., W0tpsmIBdwcYyG50zbta).
Update (PUT index_name/123/_update — Slides 36–37):Documents in ElasticSearch are immutable—you cannot mutate a document in place. Instead, ES reindexes it in four steps:
Retrieve the old document.
Modify the retrieved copy.
Delete the old document (marked inaccessible immediately; physically cleaned up in the background).
Index the new copy.
Delete (DELETE index_name/id — Slide 38): Marks the document removed from the index; physical removal happens asynchronously in the background rather than immediately.
Common confusions
Confusing the three index meanings: In "PUT /departments/123 indexes the document into the departments index and updates its inverted indexes", the verb (indexes), noun (departments index), and data structure (inverted indexes) all appear in one sentence.
Putting numeric/boolean constraints inside query instead of filter: Exact constraints like price <= 100 or status = "active" belong in filter so ES skips relevance scoring and caches/prunes faster.
Expecting in-place updates: Every ES update rewrites and reindexes a new immutable document copy while scheduling the old copy for background cleanup.
Retrieval questions with answers
1. Suppose "Dogs & Cats" is indexed with a character filter (& → and), lowercase filter, and stemmer (dogs → dog, cats → cat), but a query searches for "Dogs" without running the analyzer. Why does it fail to match?
The inverted index stores the normalized, stemmed token "dog", not "Dogs". Because the query was not passed through the same analyzer, searching for "Dogs" fails to match "dog".
2. In a bool query, which clause corresponds to AND, OR, and NOT—and how do filter and query differ in output and cost? must = AND, should = OR, and must_not = NOT. A filter tests exact values with a boolean yes/no answer and is more efficient (pruning documents before scoring), whereas a query evaluates full-text relevance and computes a floating-point _score.
3. Why does updating a single field like "color": "red" in ElasticSearch require four internal steps?
Because documents in ElasticSearch are immutable: ES must retrieve the old document, modify a copy, delete the old document (cleaned later in the background), and index the new copy.