Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 43 ยท Practice and Review

Elasticsearch Interview Questions

Prepare for Elasticsearch interviews: short answers on concepts, relevance, shards, mappings, performance and trade-offs against SQL and other engines.

Interviews about Elasticsearch rarely ask you to recite API syntax. They test whether you understand why the engine behaves as it does: why search is fast, why a field cannot change type, why deep pagination hurts, and when you should choose something else. This lesson collects the questions that come up most, grouped by topic, each with a short answer you can say out loud in under a minute.

Two questions come with runnable stand-ins (small JavaScript models) so you can see the answer rather than memorize it. They are teaching models, not the real engine, and the lesson says where they simplify. Each answer links to the lesson with the full story.

What interviewers look for

Why, not how Explain the mechanism behind the feature.
Trade-offs Every choice costs something.
Experience Mention a real problem you hit.

Short, accurate answers beat long vague ones.

1 / 2

Concepts

What is Elasticsearch? A distributed search and analytics engine built on Apache Lucene. You store JSON documents, it indexes every field, and you query through a REST API. It is excellent at full-text search, filtering and aggregations over large data. (What Is Elasticsearch?)

Why is search fast? Because of the inverted index: a sorted dictionary of terms, each pointing to a postings list of documents. A query reads the postings of its terms instead of scanning documents. (The Inverted Index)

Is Elasticsearch a database? It can store and return data, but it is a search engine first. It has no multi-document ACID transactions and no joins like SQL, and it is near-real-time, not instantly consistent. Most teams keep their source of truth in a database and index a copy in Elasticsearch.

What is _source? The original JSON you sent, stored as is. Search results return it, and updates and reindexing read it. Fields are separately indexed (inverted index, doc values) for searching, sorting and aggregating.

What are an index, a document and a field? A document is a JSON object, a field is a key in it, and an index is a named collection of documents with a mapping and settings. Roughly (and only roughly) these resemble a row, a column and a table.

Search and relevance

What does an analyzer do? It turns text into terms in three stages: character filters, one tokenizer, then token filters (lowercasing, stop words, stemming). The same analysis runs at index time and, by default, at query time, which is why a match query for "FOX" finds "fox". (Analysis, Tokenizers and Analyzers)

match vs term? match analyzes the query text, then looks up the resulting terms, which suits text fields. term does no analysis and looks up the exact value, which suits keyword, numbers and dates. The classic bug is a term query on a text field with capital letters: the index holds lowercased tokens, so it finds nothing.

The stand-in below models the difference with a tiny "text" index (analyzed) and "keyword" index (exact).

JS match on text vs term on keyword

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The match finds two documents, even though the capitalization differs, because both sides are analyzed. The keyword lookup is exact: right capitalization matches, any other spelling returns nothing. Note that foxes is not found by fox, because this stand-in has no stemmer.

How is relevance computed? With BM25 by default. A term's score grows with its frequency in the document (with saturation), grows with its rarity across the index (IDF) and is lowered for long fields. You can see the breakdown with explain: true or the _explain API. (Relevance Scoring with BM25)

Query context vs filter context? In query context, clauses answer "how well does it match?" and affect the score. In filter context, they answer "does it match?" with a yes or no, skip scoring, and their results can be cached. Put exact conditions (status, ranges, tenant) in filter. (Bool Queries and Compound Logic)

How do you improve search quality? Choose the right analyzers (stemming, synonyms, language analyzers), boost important fields, use multi_match with the right type, add fuzziness for typos, and tune with real queries. Measure with a judged test set rather than guessing. (Custom Analyzers, Synonyms and Stemming)

Mappings

Dynamic vs explicit mapping? With dynamic mapping Elasticsearch guesses types from the first value it sees. That is quick, but guesses go wrong (a numeric string, a date-like value). Explicit mappings are safer in production. dynamic: strict rejects unknown fields. (Dynamic Mapping and Index Templates)

Why can't you change a field's type? Existing segments already hold data in the old shape (an inverted index for a text field, doc values for a number). Mixing types in one field would make those files inconsistent. The fix is a new index with the right mapping and a reindex, usually behind an alias so clients never notice. (Using Elasticsearch from Code)

text vs keyword? text is analyzed for full-text search; keyword is stored as one exact value for filtering, sorting and aggregations. Use a multi-field to have both.

How do you model relations without joins? Prefer denormalization: store the data you need together in one document, and accept that updates touch several documents. Use nested when the objects in an array must stay together, because plain objects are flattened and lose their pairing. Use join (parent-child) only when the child data changes far more often than the parent. (Data Modeling and Denormalization, Objects, Arrays and Nested Fields)

Shards and clusters

What is a shard? An index is split into primary shards; each is a complete Lucene index. Replica shards are copies of primaries on other nodes, for high availability and extra read capacity. A search fans out to one copy of every shard and merges the results. (Shards, Replicas and Clusters)

Why can't you change the number of primary shards of an index? A document's shard is chosen by hashing its routing value (the _id by default) modulo the number of primary shards. A different count would send existing documents to other shards, so they would no longer be found. The _split and _shrink APIs exist, but they follow strict rules and create a new index. The stand-in uses a plain hash and modulo to show the effect (the real hash function is different, but the principle is the same).

JS Why the shard count is fixed

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With 3 shards the ids land on shards 2 0 1 2 0 1 2 0, with 4 shards on 2 3 0 1 2 3 0 1. Seven of eight documents would move, so a lookup by id would go to the wrong shard. That is why changing the primary count means a new index.

What do cluster health colors mean? Green: all primaries and replicas are allocated. Yellow: all primaries are allocated, some replicas are not (common on a single node, where a replica cannot live next to its primary). Red: at least one primary is unassigned, so some data is unavailable.

How many shards should an index have? Few, and big enough to be worth it: a common guideline is shards of tens of gigabytes. Too many small shards waste memory and cluster-state overhead ("oversharding"). Size by data volume and growth, and use data streams with rollover for time-based data.

Performance

Why is deep pagination slow? from + size makes every shard produce its top from + size hits, which the coordinating node merges and throws most of away. Cost grows with depth, and max_result_window (10,000 by default) guards it. Use search_after with a point in time (PIT) for deep or exhaustive paging. (Sorting and Pagination)

How do you make queries faster? Use filters (cacheable), avoid leading wildcards, regex and scripts on large data, query fewer fields, keep a sensible shard size, use _source filtering and avoid fetching huge pages. Check with the profile API before optimizing. (Query Performance and Tuning)

How do you speed up bulk indexing? Use the bulk API in batches of a few megabytes, several parallel workers, a larger refresh_interval (or -1 temporarily), and no replicas during the initial load, then restore them.

What is a refresh? The step that makes new documents visible to search by writing the in-memory buffer to a new segment (every second by default). The translog protects the data before it is flushed to disk. That is why Elasticsearch is near-real-time. (Segments, Refresh and Near-Real-Time Search)

Why are deletes and updates costly? Segments are immutable. A delete only marks the document in a bitmap; an update is a delete plus a new document. The space is reclaimed later by segment merges.

Trade-offs: SQL and other engines

When should you choose Elasticsearch over a relational database? When you need relevance-ranked full-text search, typo tolerance, faceting, analytics over huge append-only data, or horizontal scale for reads. Keep SQL for transactions, strong consistency and complex joins.

When should you not use Elasticsearch? As the only store for critical transactional data, for workloads that are mostly updates by key, or when a simple LIKE or the built-in full-text search of your database is enough for a small dataset.

How does it compare with Solr, OpenSearch and database search? Solr and OpenSearch share the Lucene foundation (OpenSearch is a fork of Elasticsearch 7.10 with its own later development), so the concepts transfer almost entirely. Database full-text search (for example PostgreSQL's) is simpler to operate and transactionally consistent with your data, but offers fewer relevance and scaling features. A dedicated lightweight engine such as Meilisearch can be easier for small, typo-tolerant site search. Pick on needs and operational cost, and say what you measured.

How do you keep Elasticsearch in sync with the database? Publish changes from the application or from the database log, then index through a queue. Make indexing idempotent (use the database id as _id), keep a way to rebuild the whole index, and plan for the delay between the write and its visibility.

How do you prevent query injection? Never build a Query DSL JSON or a query_string from raw user text. Send user text as a value in a match or simple_query_string query, and validate field names against an allowed list. (Security, Backups and Operations)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A term query for "Fox" on an analyzed text field returns nothing. Why?

    The analyzer lowercased the text at index time. term looks for the exact value Fox, which is not among the tokens.
  2. 2. Why can't you change the primary shard count of an existing index?

    Routing uses the hash of the id modulo the number of primaries. Changing the number changes the target shard of most documents.
  3. 3. Which is the best fix for deep pagination over millions of hits?

    search_after continues from the last sort values without making each shard build a huge top list. Raising the window only moves the cost.
  4. 4. A cluster is yellow. What is true?

    Yellow means data is complete and searchable, but redundancy is reduced, for example a replica has no second node to live on.

Summary

  • Answer with the mechanism: inverted index, analysis, BM25, immutable segments, hash routing.
  • Know match vs term, text vs keyword, query vs filter context.
  • Mappings and primary shard counts are fixed per index; change them with a new index and a reindex.
  • Deep pagination, oversharding and expensive queries are the usual performance traps.
  • Elasticsearch complements a database; it rarely replaces one.

Next lesson: Elasticsearch Cheat Sheet.