# How to use search\_after with match all query and without sorting?

**URL:** https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179
**Category:** Elasticsearch
**Created:** [March 15, 2018, 10:53pm UTC](https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179 "2018-03-15T22:53:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![majid\_zarrin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/majid_zarrin/32/29103_2.png) [@majid\_zarrin](https://discuss.elastic.co/u/majid_zarrin)
#### Post date: [March 15, 2018, 10:53pm UTC](https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179/1 "2018-03-15T22:53:25Z")

</div>

Hi,  
I had billions of document in an index. I read half of them with java-high-level-client until my code got an exception. so I need to skip these documents. I found [search\_after](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-request-search-after.html) as a solution but I have no idea how it is working on the match-all query and without sorting.  
here is the simplified version of my search request :

> SearchRequest searchRequest = new SearchRequest("Index");  
> searchRequest.types("type");  
> SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
> searchSourceBuilder.query(QueryBuilders.matchAllQuery());  
> searchSourceBuilder.size(5000);  
> // searchSourceBuilder.searchAfter() // need to skip x documents or a given uid  
> searchRequest.source(searchSourceBuilder);  
> sourceBuilder.timeout(new TimeValue(10, TimeUnit.MINUTES));  
> searchRequest.scroll(new TimeValue(10, TimeUnit.MINUTES));  
> SearchResponse searchResponse = highLevelClient.search(searchRequest);  
> ....

thank you.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 15, 2018, 11:09pm UTC](https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179/2 "2018-03-15T23:09:07Z")

</div>

It needs a sort key.  
The problem is that with match all you don't have anything else than \_score: 1 which can't help.

I'd recommend starting again from start but sort by \_uid which is anyway the most efficient.

BTW you should use scroll API instead.

---

<div class="post-metadata">

### Author: ![majid\_zarrin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/majid_zarrin/32/29103_2.png) [@majid\_zarrin](https://discuss.elastic.co/u/majid_zarrin)
#### Post date: [March 16, 2018, 12:12am UTC](https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179/3 "2018-03-16T00:12:40Z")

</div>

> [@dadoonet](#):
>
> It needs a sort key.
> 
> The problem is that with match all you don't have anything else than \_score: 1 which can't help.
> 
> I'd recommend starting again from start but sort by \_uid which is anyway the most efficient.
> 
> BTW you should use scroll API instead.

Thank you so much  
I didn't sort the match all query before and my code did its work on the billions document. so I start the project again with scroll API (read and skip 1 million document per minute on my VPS ).

---

<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: [April 13, 2018, 12:12am UTC](https://discuss.elastic.co/t/how-to-use-search-after-with-match-all-query-and-without-sorting/124179/4 "2018-04-13T00:12:41Z")

</div>

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