BackendBench
Coffee

Unravel › Java concepts › NoSQL and caches

Persistence

Elasticsearch and OpenSearch search

Elasticsearch is a search engine built on inverted indexes.

🔎 Unravel this example

At a glance

Mental model

The index at the back of a book.

What happens, in order
  1. 1Documents are JSON objects stored in an indexEach text field is analysed (lowercased, split into terms, stemmed) at write time, and the terms point back to documents.
  2. 2A search request combines a bool query: must (scored matches), filter (yes/no conditions that are cached), should and must_notResults come back with a relevance score and highlights.
  3. 3Bulk requests index many documents per call and are essential for throughputAggregations compute counts and statistics for faceted navigation.
  4. 4Spring Data Elasticsearch offers repositories and ElasticsearchOperations so queries can be derived from method names or built with a fluent query
Remember
Watch out
Using it as the primary databaseExpecting immediate visibility of writesDynamic mappings that explode with arbitrary keysDeep pagination with from and size on huge result sets
Example (highlighted lines explain it)
SearchSourceBuilder source = new SearchSourceBuilder()    .query(QueryBuilders.boolQuery()        .must(QueryBuilders.matchQuery("title", text))        .filter(QueryBuilders.termQuery("status", "PUBLISHED")))    .size(20);SearchRequest request = new SearchRequest("articles").source(source);SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  1. line 1 uses SearchSourceBuilder, the core of Elasticsearch and OpenSearch search
  2. line 2 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
  3. line 3 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
  4. line 4 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
  5. line 6 uses SearchRequest, the core of Elasticsearch and OpenSearch search
  6. line 7 uses SearchResponse, the core of Elasticsearch and OpenSearch search
Read the full explanation (about 2 min)

Elasticsearch is a search engine built on inverted indexes. You index documents, then ask full-text, filtered and aggregated questions that ordinary databases answer slowly or not at all.

A mental model

The index at the back of a book. Instead of reading every page to find "scheduler", you look up the word and get the page numbers immediately.

How it works

Documents are JSON objects stored in an index. Each text field is analysed (lowercased, split into terms, stemmed) at write time, and the terms point back to documents.

A search request combines a bool query: must (scored matches), filter (yes/no conditions that are cached), should and must_not. Results come back with a relevance score and highlights.

Bulk requests index many documents per call and are essential for throughput. Aggregations compute counts and statistics for faceted navigation.

Spring Data Elasticsearch offers repositories and ElasticsearchOperations so queries can be derived from method names or built with a fluent query.

Going deeper

It is a search index, not your system of record. Keep the source of truth in a database and feed Elasticsearch from it (events, CDC, outbox); otherwise you cannot rebuild the index after a mapping change.

Writes become visible after a refresh (one second by default), so "save then immediately search" can miss the new document. Do not build workflows that need read-your-writes from search.

Mapping decisions (field types, analyzers) are mostly permanent. To change them you create a new index and reindex, which is why aliases are used to switch over.

Under the hood

An index is split into shards, each a Lucene index made of immutable segments. Search runs on every shard in parallel and the coordinating node merges the top results. Deletes and updates mark documents and are cleaned up when segments merge in the background.

Common mistakes

  • Using it as the primary database.
  • Expecting immediate visibility of writes.
  • Dynamic mappings that explode with arbitrary keys.
  • Deep pagination with from and size on huge result sets.

How Unravel recognises it

When you paste code, Unravel looks for these signs of elasticsearch and opensearch search. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Elasticsearch and OpenSearch search?

Elasticsearch is a search engine built on inverted indexes. You index documents, then ask full-text, filtered and aggregated questions that ordinary databases answer slowly or not at all.

What are the most common mistakes with Elasticsearch and OpenSearch search?

Using it as the primary database. Expecting immediate visibility of writes. Dynamic mappings that explode with arbitrary keys. Deep pagination with from and size on huge result sets.

How can I see Elasticsearch and OpenSearch search in real code?

Paste your own Java into Unravel and it will point out where elasticsearch and opensearch search appears, explain how it works at the detail level you choose, and draw the flow of the code around it.