
Elasticsearch Search After is a feature that allows you to narrow down your search results by specifying a minimum value for a specific field. This can be particularly useful when searching for data with a large range of values.
By using Search After, you can significantly reduce the number of documents that need to be scanned, resulting in faster search times. For example, if you're searching for documents with a date field, specifying a minimum date can filter out documents that are older than that date.
This approach can also improve the accuracy of your search results, as you're not getting overwhelmed by irrelevant data. By setting a minimum value, you can focus on the most relevant documents first.
Intriguing read: Elasticsearch Date Range Query
Search and Pagination
Elasticsearch offers two primary pagination methods: the from/size method, ideal for smaller indexes, and the search_after method, designed for larger datasets.
The search_after method requires each document to have a sortable field, such as a document ID or a timestamp. This approach eliminates the need to skip documents, making it efficient for large indexes.
Additional reading: Document in Elasticsearch
Elasticsearch currently provides three different techniques for fetching many results: pagination, Search-After, and Scroll. Each use case calls for a different technique.
The default mechanism to fetch many results in Elasticsearch is pagination. It returns the first, or most relevant, 10 documents by default.
Here's a comparison of the two pagination methods:
The search_after method does not have the same 10,000 document limit and does not require the from parameter. This makes it a good choice when you need to fetch many results without being limited by the default pagination mechanism.
Discover more: Gmail Search for Not Important
Implementation and Examples
Let's dive into the implementation and examples of Elasticsearch's search_after method.
To apply the from/size method for searching, you need to have an index with documents stored.
The from/size method is straightforward, but be aware that you shouldn't exceed 10,000 documents, as Elasticsearch will raise an error if you do.
To use the search_after method, first make an initial request to retrieve the first n documents, where n is defined by the size parameter.
Take the sort values from the last document of the previous batch and pass them to the search method, replacing the from parameter used in the from/size method.
The search_after parameter is used to fetch the next batch of documents.
Comparison and Benchmarking
In benchmarking tests, the search_after method consistently delivers faster response times, clustered around 5-7 milliseconds. This is a significant advantage over other methods.
The from/size method, on the other hand, displays a broader range of response times, peaking at 22.5 milliseconds. This suggests that search_after offers superior performance for pagination.
As we attempt to retrieve deeper documents, the response time significantly increases for the from/size method, whereas the search_after method remains relatively stable.
Point in Time API
The Point in Time API is a game-changer for search experiences. It allows users to see the same version of the index over a certain period of time, making updates invisible to them.
This consistency is crucial for a seamless search experience, where users won't be surprised by documents suddenly popping up when clicking back and forth across search result pages.
By using the Point in Time API, you can extend pagination and Search-After functionality to make them stateful, which means users will always see the same data.
This approach is particularly useful for applications where data consistency is paramount, ensuring that users have a smooth and predictable experience.
Additional reading: Elasticsearch Bulk Search
Benchmarking

Benchmarking is a crucial step in determining the performance of different methods. The benchmark in this case compared the from/size method and the search_after method.
The results showed that the response time for the search_after method remained relatively stable, while the from/size method significantly increased as we attempted to retrieve deeper documents. This indicates a major performance difference between the two methods.
The histogram revealed that the search_after method consistently delivered faster response times, peaking at 5-7 milliseconds, whereas the from/size method displayed a broader range of response times, peaking at 22.5 milliseconds. This suggests that search_after offers superior performance for pagination.
Search_after is a more efficient method, especially when showing many hits, as it uses a tie breaker from the last hit of the previous search request, rather than keeping the whole score-ID list in memory.
Key Features and Capabilities
Elasticsearch search after is incredibly powerful, and I'd like to highlight some of its key features and capabilities.
Elasticsearch search after boasts millisecond-latency search, powered by Lucene, which delivers instant answers no matter how fast data moves.
One of the standout features is its ability to scale effortlessly, with Elasticsearch autoscaling, rebalancing, replicating, and powering through petabytes – all without downtime or drama.
Elasticsearch is a versatile platform that can handle various data types, including text, timestamps, and vectors, making it a one-stop-shop for search, observability, and security.
With Elasticsearch, you get advanced search and relevance features, such as fuzzy, semantic, or precise search, as well as full-text search, vector search, and hybrid search with filters, ranking, and reranking for unmatched relevance.
Here are some of the key features that make Elasticsearch search after so capable:
- Millisecond-latency search
- Advanced search and relevance
- Smart storage and deep search
- Connected and integrated
Elasticsearch delivers performant, searchable, cost-effective data storage, with options for hot tier on local disks and searchable snapshots on object storage – all without compromising on speed or savings.
A different take: Elasticsearch Storage
Featured Images: pexels.com


