# How to get unique documents with Search\_After

**URL:** <https://discuss.elastic.co/t/how-to-get-unique-documents-with-search-after/339098>\
**Category:** Elasticsearch\
**Created:** [July 24, 2023, 12:38pm UTC](https://discuss.elastic.co/t/how-to-get-unique-documents-with-search-after/339098 "2023-07-24T12:38:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FTOR](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@FTOR](https://discuss.elastic.co/u/FTOR)\
**Post date:** [July 24, 2023, 12:38pm UTC](https://discuss.elastic.co/t/how-to-get-unique-documents-with-search-after/339098/1 "2023-07-24T12:38:11Z")

</div>

Hello,

I would like to get unique documents basing on a field (unique\_id\_field) and by using search\_after algorithm  
GET /my\_index/\_search  
{

```
"query": {
	"match_all": {}
	
}
"size": 10000,
"search_after": [sort_field_value], 
"sort": [
	{
		"sort_field": {
			"order": "asc"
		}
	}
]

```

}

I tried to add an aggregation, but it doesn't work with search\_after. I need to set the size to zero in order to get a result. But, by setting the size to zero I can no more use sear\_after

GET /my\_index/\_search  
{

```
"query": {
	"match_all": {}
	
},
    "aggs":{
              "unique_ids": {
                  "terms": {
                      "field": "unique_id_field",
                      "size": 10000
                  },
                  "aggs":{
                   "dedup_docs":{
                     "top_hits":{
                       "size":1
                      }
                    }
                  }   
              }
        },
"size": 0,
"sort": [
	{
		"sort_field": {
			"order": "asc"
		}
	}
]

```

}

---

<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 21, 2023, 12:38pm UTC](https://discuss.elastic.co/t/how-to-get-unique-documents-with-search-after/339098/2 "2023-08-21T12:38:51Z")

</div>

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