<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kutay Öndersev on Medium]]></title>
        <description><![CDATA[Stories by Kutay Öndersev on Medium]]></description>
        <link>https://medium.com/@kutayondersev?source=rss-e79333dabcb1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EklRyEseBAa4Om6O.jpg</url>
            <title>Stories by Kutay Öndersev on Medium</title>
            <link>https://medium.com/@kutayondersev?source=rss-e79333dabcb1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:25:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kutayondersev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Elasticsearch Pagination]]></title>
            <link>https://medium.com/@kutayondersev/elasticsearch-pagination-1ab1dc429a8b?source=rss-e79333dabcb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ab1dc429a8b</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[search]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Kutay Öndersev]]></dc:creator>
            <pubDate>Tue, 23 Jul 2024 14:50:51 GMT</pubDate>
            <atom:updated>2024-07-23T14:50:51.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVrdEFQX8AgyiuPytaXwlA.png" /></figure><p>Elasticsearch is an open-source, distributed, and highly scalable near-real time search and analytics engine built on top of the Apache Lucene library. It is known for its speed, flexibility, and ability to handle large volumes of data efficiently.</p><p><strong>You can visit </strong><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/near-real-time.html"><strong>here</strong></a><strong> for understand what defines near real-time search.</strong></p><p>When processing large datasets, pagination methods are essential to improve performance and reduce resource usage. In this article, we will examine pagination methods in Elasticsearch using the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">SearchAPI</a>.</p><h4><strong>From/Size</strong></h4><p>In this method, from and size parameters are added to our query. The from parameter defines the number of hits to skip, defaulting to 0. The size parameter specifies the maximum number of hits to return. Together, these two parameters define a page of results.</p><p>Since the search operation is distributed across different shards, each shard loads the results it hits and the previous page results into memory. This can significantly increase CPU and memory usage with large datasets, adversely affecting node performance.</p><p><em>Query example</em></p><pre>GET /_search<br>{<br>  &quot;from&quot;: 5,<br>  &quot;size&quot;: 20,<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;user.id&quot;: &quot;kimchy&quot;<br>    }<br>  }<br>}</pre><blockquote>By default, you cannot use from and size to page through more than 10,000 hits. This limit is a safeguard set by the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-max-result-window">index.max_result_window</a> index setting. If you need to page through more than 10,000 hits, use the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after">search_after</a> parameter instead.</blockquote><h4>Scroll</h4><p>Before starting, note that Elasticsearch no longer supports deep search with Scroll API for new use cases.</p><blockquote>We no longer recommend using the scroll API for deep pagination. If you need to preserve the index state while paging through more than 10,000 hits, use the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after">search_after</a> parameter with a point in time (PIT).</blockquote><p>When the Scroll API is used, it creates a search context with a specified time to live (TTL). Each scroll operation within this context renews its lifespan, ensuring uninterrupted processing during long operations.</p><p>To prevent issues caused by having too many scrolls open, users cannot open scrolls beyond a certain limit (default is 500). This limit can be adjusted with the search.max_open_scroll_context cluster setting.</p><p>The search context captures all data like a snapshot in time when a query is made, and subsequent changes do not affect this context. Therefore, we can rely on the consistency of the same scroll operation containing the same data each time. However, it’s important to remember that it’s not live data, so it’s possible that data may have changed or been deleted while scrolling.</p><p>When creating a query, we establish the context by passing the scroll parameter. Subsequent requests are then continued using the scrollId returned from the initial query.</p><pre>POST /my-index-000001/_search?scroll=1m<br>{<br>  &quot;size&quot;: 100,<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;message&quot;: &quot;foo&quot;<br>    }<br>  }<br>}</pre><pre>POST /_search/scroll                                                               <br>{<br>  &quot;scroll&quot; : &quot;1m&quot;,                                                                 <br>  &quot;scroll_id&quot; : &quot;DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==&quot; <br>}</pre><h4>Search_after</h4><p>When we retrieve data using sort values in our queries, we obtain a sort value for each hit in the response. We can use this value by providing it to the search_after parameter, which allows us to obtain the data for the next page.</p><p><em>Query example with sort values</em></p><pre>GET twitter/_search<br>{<br>    &quot;query&quot;: {<br>        &quot;match&quot;: {<br>            &quot;title&quot;: &quot;elasticsearch&quot;<br>        }<br>    },<br>    &quot;sort&quot;: [<br>        {&quot;date&quot;: &quot;asc&quot;},<br>        {&quot;tie_breaker_id&quot;: &quot;asc&quot;}      <br>    ]<br>}</pre><blockquote>Elasticsearch uses Lucene’s internal doc IDs as tie-breakers. These internal doc IDs can be completely different across replicas of the same data. When paging search hits, you might occasionally see that documents with the same sort values are not ordered consistently.</blockquote><p>Response</p><pre>{<br>  &quot;took&quot; : 17,<br>  &quot;timed_out&quot; : false,<br>  &quot;_shards&quot; : ...,<br>  &quot;hits&quot; : {<br>    &quot;total&quot; : ...,<br>    &quot;max_score&quot; : null,<br>    &quot;hits&quot; : [<br>      ...<br>      {<br>        &quot;_index&quot; : &quot;twitter&quot;,<br>        &quot;_id&quot; : &quot;654322&quot;,<br>        &quot;_score&quot; : null,<br>        &quot;_source&quot; : ...,<br>        &quot;sort&quot; : [<br>          1463538855,<br>          &quot;654322&quot;<br>        ]<br>      },<br>      {<br>        &quot;_index&quot; : &quot;twitter&quot;,<br>        &quot;_id&quot; : &quot;654323&quot;,<br>        &quot;_score&quot; : null,<br>        &quot;_source&quot; : ...,<br>        &quot;sort&quot; : [                                <br>          1463538857,<br>          &quot;654323&quot;<br>        ]<br>      }<br>    ]<br>  }<br>}</pre><p>Now, we can paginate the results using the sort values from this response with the search_after parameter.</p><pre>GET twitter/_search<br>{<br>    &quot;query&quot;: {<br>        &quot;match&quot;: {<br>            &quot;title&quot;: &quot;elasticsearch&quot;<br>        }<br>    },<br>    &quot;search_after&quot;: [1463538857, &quot;654323&quot;],<br>    &quot;sort&quot;: [<br>        {&quot;date&quot;: &quot;asc&quot;},<br>        {&quot;tie_breaker_id&quot;: &quot;asc&quot;}<br>    ]<br>}</pre><blockquote>Repeat this process by updating the search_after array every time you retrieve a new page of results. If a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/near-real-time.html">refresh</a> occurs between these requests, the order of your results may change, causing inconsistent results across pages. To prevent this, you can create a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/point-in-time-api.html">point in time (PIT)</a> to preserve the current index state over your searches.</blockquote><h4>Search_after with PIT</h4><p>A search request by default executes against the most recent visible data of the target indices, known as a Point In Time (PIT). Elasticsearch PIT is a lightweight view into the state of the data as it existed when initiated. In some cases, it’s preferred to perform multiple search requests using the same PIT.</p><blockquote>All PIT search requests add an implicit sort tiebreaker field called _shard_doc, which can also be provided explicitly. If you cannot use a PIT, we recommend that you include a tiebreaker field in your sort. This tiebreaker field should contain a unique value for each document. If you don’t include a tiebreaker field, your paged results could miss or duplicate hits.</blockquote><p>Create a PIT</p><pre>POST /my-index-000001/_pit?keep_alive=1m<br></pre><p>Obtain the PIT ID</p><pre>{<br>  &quot;id&quot;: &quot;46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==&quot;<br>}</pre><p>Execute Query Using PIT ID</p><pre>GET /_search<br>{<br>  &quot;size&quot;: 10000,<br>  &quot;query&quot;: {<br>    &quot;match&quot; : {<br>      &quot;user.id&quot; : &quot;elkbee&quot;<br>    }<br>  },<br>  &quot;pit&quot;: {<br>    &quot;id&quot;:  &quot;46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==&quot;, <br>    &quot;keep_alive&quot;: &quot;1m&quot;<br>  },<br>  &quot;sort&quot;: [ <br>    {&quot;@timestamp&quot;: {&quot;order&quot;: &quot;asc&quot;, &quot;format&quot;: &quot;strict_date_optional_time_nanos&quot;}}<br>  ]<br>}</pre><p>To get the next page of results, rerun the previous search using the last hit’s sort values (including the tiebreaker) as the search_after argument and the latest PIT ID in the pit.id parameter. The search&#39;s query and sort arguments must remain unchanged. If provided, the from argument must be 0 (default) or -1.</p><pre>GET /_search<br>{<br>  &quot;size&quot;: 10000,<br>  &quot;query&quot;: {<br>    &quot;match&quot; : {<br>      &quot;user.id&quot; : &quot;elkbee&quot;<br>    }<br>  },<br>  &quot;pit&quot;: {<br>    &quot;id&quot;:  &quot;46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==&quot;, <br>    &quot;keep_alive&quot;: &quot;1m&quot;<br>  },<br>  &quot;sort&quot;: [<br>    {&quot;@timestamp&quot;: {&quot;order&quot;: &quot;asc&quot;, &quot;format&quot;: &quot;strict_date_optional_time_nanos&quot;}}<br>  ],<br>  &quot;search_after&quot;: [                                <br>    &quot;2021-05-20T05:30:04.832Z&quot;,<br>    4294967298<br>  ]                     <br>}</pre><p>In this article, I have shared pagination methods in Elasticsearch, their differences, and usage examples. Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ab1dc429a8b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>