# Unique Doc related to one \`field\`

**URL:** <https://discuss.elastic.co/t/unique-doc-related-to-one-field/333344>\
**Category:** Elasticsearch\
**Created:** [May 13, 2023, 6:02am UTC](https://discuss.elastic.co/t/unique-doc-related-to-one-field/333344 "2023-05-13T06:02:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_zogaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_zogaj/32/120941_2.png) [@\_zogaj](https://discuss.elastic.co/u/_zogaj)\
**Post date:** [May 13, 2023, 6:02am UTC](https://discuss.elastic.co/t/unique-doc-related-to-one-field/333344/1 "2023-05-13T06:02:38Z")

</div>

Hey, I am using pagination and i want to filter duplicated doc related to one field. For the moment i am trying it with `Collapse` functionality to filter duplicated and with `Cardinality aggregation` to get the total unique files. But it is not working as expected.(Since i have docs with same `name` and different values for `isValid` flag. I get the correct total number of unique docs from cardinality, but latest pages of pagination have no results(Probably Collapse provide less results). Is there a way how to fix it to have the same number of doc from collapse and the same number of dos from cardinality ? Or is there another way, ( even without using Collapse ) to get the unique docs with pagination ?

```auto
const coreSearch = {
			index: 'images',
			body: {
				query: {
					bool: {
						must: [
							{ term: { type: 'png' } },
							{ term: { isValid: false } }
						]
					},
				},
				collapse: { field: 'name.keyword' },
				from: from,
				size: 50,
				aggs: {
					total_files: { cardinality: { field: 'name.keyword' } }
				}
			},
		}

```

---

<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:** [June 10, 2023, 6:02am UTC](https://discuss.elastic.co/t/unique-doc-related-to-one-field/333344/2 "2023-06-10T06:02:53Z")

</div>

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