# SELECT DISTINCT documents by a specisific field

**URL:** <https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [December 17, 2019, 9:28pm UTC](https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232 "2019-12-17T21:28:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnWhite2000](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JohnWhite2000](https://discuss.elastic.co/u/JohnWhite2000)\
**Post date:** [December 17, 2019, 9:28pm UTC](https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232/1 "2019-12-17T21:28:20Z")

</div>

I have documents with these fields

- id
- userId
- userFirstName
- userLastName
- userEmail
- projectId

which is something like a many to many, where multiple documents with the same userId can exist.  
I need to retrieve all the documents which are unique by userId, I am interested in all user fields, I can ignore project.  
If it was relational I would done something like `SELECT DISTINCT(userId), userFirstName, userLastName FROM users;` but in ES I am not sure. Looking into aggregations but can not make it work yet.

A little help please 🙂  
Thank you!

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [December 18, 2019, 11:24am UTC](https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232/2 "2019-12-18T11:24:58Z")

</div>

Hi @JohnWhite2000.

Thanks

`I think DISTINCT NOT implemented Yet ES.`

> You have to use aggrigation if possible in your case but this will fetch single records and that is like :- below

[aggregations\_ES](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation)

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![JohnWhite2000](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JohnWhite2000](https://discuss.elastic.co/u/JohnWhite2000)\
**Post date:** [December 18, 2019, 12:49pm UTC](https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232/3 "2019-12-18T12:49:41Z")

</div>

This seems to give me what I want, not sure if there is a better way, but for now it will do

```
"aggs": {
    "groupedByUserId": {
        "terms": {
            "field": "userId"
        },
        "aggs": {
        	"oneRecord": {
        		"top_hits": {
        			"size": 1
        		}
        	}
        }
    }
}
```

---

<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:** [January 15, 2020, 12:50pm UTC](https://discuss.elastic.co/t/select-distinct-documents-by-a-specisific-field/212232/4 "2020-01-15T12:50:09Z")

</div>

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