# Search\_after query doesn't return correct results for pagination

**URL:** <https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999>\
**Category:** Elasticsearch\
**Created:** [August 19, 2021, 6:15pm UTC](https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999 "2021-08-19T18:15:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_McGovern](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_mcgovern/32/90131_2.png) [@Matt\_McGovern](https://discuss.elastic.co/u/Matt_McGovern)\
**Post date:** [August 19, 2021, 6:15pm UTC](https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999/1 "2021-08-19T18:15:04Z")

</div>

I have a process that queries the watcher-history indices for a specified time, and the amount of results is over 10000 docs, which is the limit. So I tried to use the search\_after query, after adding a sort value, but the results are incorrect.

The following query example would return total of 385 docs, so two queries use a size of 200 and a sort field, and the 2nd query also uses the search\_after referencing the last field in the first query's return dataset. My problem is both queries return 200 records. Why is this and what am I doing wrong? (NOTE: my search\_after was at first the result.execution\_time by itself, then I added the \_id of the watcher, then I added the sort value, etc.)

1st query:  
GET .watcher-history\*/\_search

```auto
{
	"size": 200,
	"_source": [
		"result.execution_time",
		"result.condition.met",
		"result.condition.status",
    "result.actions",
		"metadata.name",
    "state"
	],
	"query": {
	  "bool": {
		"must": [
      {
        "query_string": {
          "query": "metadata.name: Digital_*",
          "analyze_wildcard": true,
          "default_field": "*"
        }
      },
			{
				"range": {
					"result.execution_time": {
						"gte": "now-12m"
					}
				}
			}
		]
	  }
	},
	"sort": [ 
		{"result.execution_time": {"order": "asc"}},
		{"_id": "asc"}
	]
}

```

2nd query:

```auto
GET .watcher-history*/_search
{
	"size": 200,
	"_source": [
		"result.execution_time",
		"result.condition.met",
		"result.condition.status",
    "result.actions",
		"metadata.name",
    "state"
	],
	"query": {
	  "bool": {
		"must": [
      {
        "query_string": {
          "query": "metadata.name: Digital_*",
          "analyze_wildcard": true,
          "default_field": "*"
        }
      },
			{
				"range": {
					"result.execution_time": {
						"gte": "now-12m"
					}
				}
			}
		]
	  }
	},
	"sort": [ 
		{"result.execution_time": {"order": "asc"}},
		{"_id": "asc"}
	],
	"search_after": [                                
		1629395698152,
		"Digital_dev_httpsctp-gateway-stgcodebig2net_alternate_11ee33da-42c8-40ac-8f53-04ce42c9692a-2021-08-19T17:54:58.152683Z"
	]
}

```

---

<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 25, 2021, 9:55am UTC](https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999/2 "2021-08-25T09:55:05Z")

</div>

Is it possible, that the watcher history gets updated between those two calls because of watches running? While search\_after optimizes to require less memory, it is not using a point-in-time snapshot of the data.

You may want to take a look at scroll search or if you have a newer Elasticsearch version go with [Point in Time readers](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/point-in-time-api.html) for this task.

---

<div class="post-metadata">

**Author:** ![Matt\_McGovern](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_mcgovern/32/90131_2.png) [@Matt\_McGovern](https://discuss.elastic.co/u/Matt_McGovern)\
**Post date:** [August 25, 2021, 1:45pm UTC](https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999/3 "2021-08-25T13:45:03Z")

</div>

I can't use Point-in-time because the watcher-history doesn't have an alias, and the PIT call requires an alias. I can't use a specific watcher-index name, because this call is dynamic (the watcher-history name is a moving target).

---

<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:** [September 22, 2021, 1:45pm UTC](https://discuss.elastic.co/t/search-after-query-doesnt-return-correct-results-for-pagination/281999/4 "2021-09-22T13:45:41Z")

</div>

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