# What are differences between search\_after and range filter?

**URL:** <https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224>\
**Category:** Elasticsearch\
**Created:** [July 30, 2018, 5:04pm UTC](https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224 "2018-07-30T17:04:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![porunov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/porunov/32/32268_2.png) [@porunov](https://discuss.elastic.co/u/porunov)\
**Post date:** [July 30, 2018, 5:04pm UTC](https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224/1 "2018-07-30T17:04:51Z")

</div>

Is there any differences in performance or traversal logic when we use search\_after or range filter?

For example, this query:

```
GET twitter/_search
{
    "size": 10,
    "query": {
        "range" : {
            "my_id" : {
                "gt" : 1234
            }
        }
    },
    "sort": [
        {"my_id": "asc"}
    ]
}

```

vs this query:

```
GET twitter/_search
{
    "size": 10,
    "search_after": [1234],
    "sort": [
        {"my_id": "asc"}
    ]
}

```

Are there any differences or they will perform equally?

Best regards

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2018, 11:27am UTC](https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224/2 "2018-08-02T11:27:20Z")

</div>

Those two things are something completely different and cannot be compared.

A range query allows you to filter for documents where a field value is inside of a certain range. It allows you to reduce the total number of documents being returned.

The search after functionality allows you to do deep pagination with much less resource than specifying `from/size` parameters,as the search after parameter is basically a pointer where to start the search for the documents that are about to be returned. Consider it a state, that is offloaded to the client if you want, it will however not change the result set, just where the search starts.

See [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-search-after.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-search-after.html)

---

<div class="post-metadata">

**Author:** ![porunov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/porunov/32/32268_2.png) [@porunov](https://discuss.elastic.co/u/porunov)\
**Post date:** [August 2, 2018, 12:16pm UTC](https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224/3 "2018-08-02T12:16:06Z")

</div>

@spinscale Thank you.  
I have read the documentation.  
The thing is that `search_after` is using last data from sorted fields. And basically it looks like `search_after` is just a little bit more "convenient" method to use `range` filter with `gt`.  
I.e. logically both `search_after` and `range` with `gt` are preventing deep pagination problem.  
The only differences I see are:

1. `search_after` looks better than `query` -\> `range` -\> `gt`.
2. If you are using `range` -\> `gt` you have to use sorted fields of the last returned data to query next page. If you are using `search_after` then sorted fields of the last returned data is automatically returned in `search_after` parameter.

That is why I compare those two methods (`range` filter and `search_after` parameter for pagination). That is why I asked this question. Is there any backend (logical or algorithmic) difference for pagination when we are using `search_after` or `range`?

I think that a lot of people were using `scroll` for pagination and they didn't know how to scroll without it. Possibly, Elasticsearch team was always asked a question "How to paginate without storing a context?" and Elasticsearch decided to make `search_after` parameter to hide `range` usage because it's not very clear that `range` can be used for deep pagination. But again, it's just my assumption. I didn't find any information which explains why `search_after` was created or how is it different to `range` query.

As I understand resource and performance will be equal for for both `search_after` and `range` when we use them for pagination.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 30, 2018, 12:16pm UTC](https://discuss.elastic.co/t/what-are-differences-between-search-after-and-range-filter/142224/4 "2018-08-30T12:16:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
