Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 43 ยท Getting Started

What Is Elasticsearch?

Learn what Elasticsearch is, how it differs from a database, how it builds on Lucene, and where it shines (site search, logs, analytics) and where it does not.

Elasticsearch is a distributed search and analytics engine. You give it JSON documents, and it lets you find them in milliseconds: by words in the text, by exact values, by numeric ranges, by distance on a map, or by similarity of meaning. It also summarizes millions of documents on the fly, for example "how many orders per country per day".

Think of the search box on a large shop, the "find in all repositories" feature of a code host, or the dashboard that shows errors per minute across a hundred servers. Behind many of them sits an engine like this. This lesson explains what it is, what it is built from, and when you should (and should not) reach for it.

The real Elasticsearch is a server program, and it cannot run on this site. Everything real in this tutorial (requests, configuration, commands) is shown in read-only blocks. Next to it you get small JavaScript and Python stand-ins that demonstrate how the idea works inside. They are models written for teaching, not Elasticsearch's own code, and each lesson says which is which.

A search engine is not a database

A relational database such as MySQL or PostgreSQL is built to store facts safely and to answer questions precisely: "give me row 42", "sum the payments of customer 7". It supports transactions, joins and constraints.

A search engine is built for a different question: "which documents are the best match for these words?" The key word is best. A database WHERE clause returns rows that match or do not match. A search engine returns a ranked list, each hit with a relevance score.

Relational database Elasticsearch
Main question "Which rows match exactly?" "Which documents match best?"
Data shape Tables, rows, columns JSON documents in indexes
Text matching LIKE '%word%' (slow, no ranking) Analyzed text, ranked by relevance
Transactions Full ACID Per-document only, no multi-document transactions
Joins Core feature Avoided; data is denormalized
Consistency Immediate Near-real-time: new data becomes searchable after about a second
Scaling Usually one big server first Built to spread over many nodes

Because of these trade-offs, Elasticsearch is normally used next to a primary database, not instead of it. The database stays the source of truth. A copy of the data that needs searching is pushed into Elasticsearch.

What Elasticsearch gives you

Full-text search Ranked results for words and phrases.
Aggregations Counts and averages over huge data.
Distributed Data is split over many nodes.
JSON and REST Talk to it over plain HTTP.
1 / 2

Built on Lucene

Elasticsearch did not invent text search. The heavy lifting is done by Apache Lucene, a Java library that has indexed and searched text since 1999. Lucene knows how to break text into words, store them in a structure called an inverted index (the subject of The Inverted Index), and score matches.

Lucene alone is a library for one machine. Elasticsearch wraps it and adds what a real system needs:

  • a REST API over HTTP with JSON, so any language can use it;
  • distribution: an index is split into shards spread over many servers, with copies for safety (see Shards, Replicas and Clusters);
  • near-real-time indexing, aggregations, cluster management, security and monitoring.

Each shard is literally one Lucene index. When you search, Elasticsearch asks every relevant shard, then merges the answers into one ranked list.

The Elastic Stack

You will often hear about the "ELK stack", now called the Elastic Stack. Elasticsearch is the center; a few companions surround it:

  • Kibana: the web interface. It has Dev Tools (a console for sending requests), Discover (browse documents) and dashboards.
  • Beats and Elastic Agent: small shippers that collect logs and metrics from machines.
  • Logstash: a pipeline that reads data, transforms it, and writes it to Elasticsearch.
  • Elasticsearch: stores, searches and aggregates it all.

For this tutorial you mostly need Elasticsearch itself, and Kibana's Dev Tools console as the place where example requests are typed.

Typical use cases

Site and app search. A shop with 200,000 products needs typo tolerance ("ipone" finds iPhone), filters by brand and price, and results sorted by relevance. This is the classic job.

Logs and observability. Servers produce millions of log lines. Elasticsearch ingests them, and you search "all ERROR lines from service checkout in the last 15 minutes" or draw a chart of errors per minute.

Analytics. Because aggregations run on the same data, you can slice events by many dimensions and get answers while the data is still arriving.

Other uses. Geo search ("cafรฉs within 500 m"), security event analysis, document and enterprise search, and, more recently, vector search for semantic matching (see Vector and Semantic Search).

When not to use it

Choose something else, or at least add something else, when:

  • you need transactions across several records (payments, inventory counts);
  • your queries are mostly "fetch by id" or simple lookups: a database or a key-value store is simpler and cheaper;
  • you need joins between large, frequently changing datasets;
  • the data is small (a few thousand rows) and a database LIKE or built-in full-text search is good enough;
  • you cannot tolerate a short delay between writing and finding;
  • you want the one and only copy of important data: Elasticsearch supports snapshots and replicas, but it is not designed to be your only store.

Strengths and weaknesses at a glance

Strengths

  • Distributed and scalable. An index is split into shards spread over nodes; add nodes to add capacity, and replicas to add safety and read throughput.
  • Fast full-text search and analytics. The inverted index answers "which documents contain this word?" without scanning, and aggregations summarize millions of documents in milliseconds to seconds.
  • Near real time. A new document becomes searchable about a second after it is indexed.
  • REST and JSON. Any language with an HTTP client can use it, and official clients exist for the common ones.
  • Schema-flexible. You can index JSON without declaring fields first (dynamic mapping), then tighten the mapping when you know what you need.
  • Rich ecosystem. Kibana, Logstash, Beats and ready-made integrations.

Weaknesses

  • Resource hungry. Heap, file-system cache and fast disks all matter; a small production cluster costs more than a database holding the same rows.
  • A learning curve. Analyzers, mappings, shard sizing and the Query DSL take time to master, and bad choices (too many shards, mapping explosions) hurt later.
  • Eventual visibility. Writes are not visible until the next refresh, and there are no multi-document transactions.
  • Not a system of record. Keep the authoritative copy in a database or event log, and treat the index as a rebuildable view.
  • Operational care. Upgrades, shard allocation, disk watermarks and security need attention.

How concurrent writes are handled

A relational database usually protects readers with multi-version concurrency control and writers with locks. Elasticsearch has no row locks. Lucene segments are immutable: an update writes a new version of the document and marks the old one deleted. Every write carries a sequence number and primary term, so you can make a write conditional (if_seq_no, if_primary_term) and get a 409 Conflict if somebody else changed the document first. That is optimistic concurrency control, and it is covered in Documents and Indexes.

Elasticsearch versus a traditional database

Question Relational database Elasticsearch
Main strength Consistent transactions, joins Relevance-ranked search, aggregations
Data model Tables with fixed schemas JSON documents, flexible mappings
Query language SQL Query DSL (JSON), plus ES|QL and SQL
Consistency ACID Near real time, per-document atomicity
Scaling out Often vertical; sharding is added work Horizontal by design
Typical role Source of truth Search and analytics copy

A search loop in miniature

Here is the smallest possible "search engine": loop over every document, count how many query words it contains, and sort by that count. It is a stand-in. Real Elasticsearch does not scan every document; it uses an index, which is why it stays fast with millions of documents.

JS A tiny search loop over an array

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

No output.

The query tomato finds three documents, all with the same score. tomato salad ranks document 4 first because it matches two words. sushi returns an empty list, which is a normal result for a search engine: zero hits, not an error.

This loop is slow (it touches every document), it does no stemming (searching "tomatoes" finds nothing), and its scoring is crude. The next lessons fix each of those problems, the same way Elasticsearch does.

What a real request looks like

For comparison, this is the kind of request you will write against a real cluster. It is read-only here, because it needs a server:

JSON
GET /recipes/_search
{
  "query": {
    "match": { "title": "tomato salad" }
  }
}

The response lists hits ordered by _score. You will learn every part of this request in the coming chapters.

A second stand-in shows the shape of a response. It builds an object that looks like a simplified search response from the loop above.

JS A response shaped like Elasticsearch's

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

No output.

Notice the names: hits.total, max_score, _id, _score and _source are the real field names of an Elasticsearch response. The values here come from our toy scorer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between a database WHERE clause and a search query?

    Search engines score each hit so the best matches come first. A WHERE clause only says whether a row matches.
  2. 2. Which library does Elasticsearch use to index and search text inside each shard?

    Lucene is the Java search library. Every Elasticsearch shard is one Lucene index.
  3. 3. Which job is a poor fit for Elasticsearch as the only system?

    Elasticsearch has no multi-document transactions, so money movements belong in a transactional database.
  4. 4. What does Kibana do in the Elastic Stack?

    Kibana provides Dev Tools, Discover and dashboards. Storage and search are done by Elasticsearch.

Summary

  • Elasticsearch is a distributed search and analytics engine with a JSON REST API.
  • It ranks results by relevance, which a normal WHERE clause does not do.
  • Each shard is a Lucene index; Elasticsearch adds distribution, an HTTP API and aggregations.
  • The Elastic Stack adds Kibana (interface), Beats/Agent and Logstash (data collection).
  • Use it for site search, logs and analytics, usually alongside a primary database.
  • Avoid it for transactions, joins and as the only copy of critical data.

Next: Setup and Your First Requests.