Unravel › Java concepts › NoSQL and caches
Elasticsearch and OpenSearch search
Elasticsearch is a search engine built on inverted indexes.
At a glance
The index at the back of a book.
- 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.
- 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.
- 3Bulk requests index many documents per call and are essential for throughputAggregations compute counts and statistics for faceted navigation.
- 4Spring Data Elasticsearch offers repositories and ElasticsearchOperations so queries can be derived from method names or built with a fluent query
- It is a search index, not your system of record
- Writes become visible after a refresh (one second by default), so "save then immediately search" can miss the new document
- An index is split into shards, each a Lucene index made of immutable segments
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);- line 1 uses SearchSourceBuilder, the core of Elasticsearch and OpenSearch search
- line 2 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
- line 3 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
- line 4 uses QueryBuilders, the core of Elasticsearch and OpenSearch search
- line 6 uses SearchRequest, the core of Elasticsearch and OpenSearch search
- 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:
- Types:
ElasticsearchClientRestHighLevelClientElasticsearchOperationsElasticsearchRepositorySearchRequestSearchSourceBuilderQueryBuildersBoolQueryBuilderIndexRequestBulkRequestSearchResponseSearchHitOpenSearchClientNativeQueryand 3 more
Part of a learning path
- Data stores beyond SQL · previous: MongoDB documents and queries · next: Cassandra and wide-column modelling
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.