# How to get a previous page from elasticsearch using search\_after?

**URL:** <https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028>\
**Category:** Elasticsearch\
**Created:** [October 24, 2017, 9:38am UTC](https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028 "2017-10-24T09:38:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgeniy\_Kosinsky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/evgeniy_kosinsky/32/23343_2.png) [@Evgeniy\_Kosinsky](https://discuss.elastic.co/u/Evgeniy_Kosinsky)\
**Post date:** [October 24, 2017, 9:38am UTC](https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028/1 "2017-10-24T09:38:44Z")

</div>

We are trying to make fast pagination with search\_after.  
It is possible to move forward by search\_after.  
We are using next request:

```
{
    "size":5,
    "search_after": [1508827804000, "data#123"],
     "sort": [{
		"date": {
			"order": "desc"
		}
	}, {
		"_uid": {
			"order": "desc"
		}
	}]
}

```

But how to get previous page? Is it possible to make it only by Elasticsearch API?  
Our solution: for getting pointer on previous page we build such query

Into ES 2.x we used this request

```
{
"index": "my_index",
"type": "data",
"body": {
	"size": 11,
	"query": {
		"bool": {
			"filter": {
				"bool": {
					"must": [{
						"range": {
							"date": {
								"gte": "2017-10-17T09:26:00"
							}
						}
					}, {
						"range": {
							"date": {
								"lte": "2017-10-24T09:26:00"
							}
						}
					}, {
						"bool": {
							"should": [{
								"bool": {
									"must": [{
										"term": {
											"date": "2017-10-24T06:50:03+0000"
										}
									}, {
										"range": {
											"_uid": {
												"gt": "data#59eee29b367df1860f335c32"
											}
										}
									}]
								}
							}, {
								"range": {
									"data": {
										"gt": "2017-10-24T06:50:03+0000"
									}
								}
							}]
						}
					}]
				}
			}
		}
	},
	"sort": [{
		"date": {
			"order": "asc"
		}
	}, {
		"cID": {
			"order": "asc"
		}
	}]
}

```

}  
If we needed to move forward, we would change gt to lt and sort from asc to desc.  
This request doesn't work into ES 5.x and higher.

---

<div class="post-metadata">

**Author:** ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)\
**Post date:** [November 1, 2017, 8:40pm UTC](https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028/2 "2017-11-01T20:40:16Z")

</div>

This query looks very complicated. Generally, you can use `search_after` but reverse the sort order to get the page before. See also a similar discussion on GitHub: [https://github.com/elastic/elasticsearch/issues/24364](https://github.com/elastic/elasticsearch/issues/24364)

---

<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:** [November 29, 2017, 8:45pm UTC](https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028/3 "2017-11-29T20:45:34Z")

</div>

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