# How do I get all the distinct field values that satisfy a particular query

**URL:** <https://discuss.elastic.co/t/how-do-i-get-all-the-distinct-field-values-that-satisfy-a-particular-query/20974>\
**Category:** Elasticsearch\
**Created:** [November 28, 2014, 7:55am UTC](https://discuss.elastic.co/t/how-do-i-get-all-the-distinct-field-values-that-satisfy-a-particular-query/20974 "2014-11-28T07:55:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Karaka](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Anil\_Karaka](https://discuss.elastic.co/u/Anil_Karaka)\
**Post date:** [November 28, 2014, 7:55am UTC](https://discuss.elastic.co/t/how-do-i-get-all-the-distinct-field-values-that-satisfy-a-particular-query/20974/1 "2014-11-28T07:55:05Z")

</div>

My sample document looks like this,

{  
"user": "adfasdk",  
"act": "Made Purchase",  
"productPrice": 5099"  
}

What I want is all the unique users that satisfy a particular query.

Say for example I have the query like below, that gets all the documents  
whose productPrice is between 5000 and 10000, and whose length is between  
50, 100. The returned documents will have repeated "user"s. I just need  
all the unique users. Do I have calculate all the unique users from this  
result myself or is it possible to do using aggregation.

{  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"productPrice": {  
"gte": 5000,  
"lte": 10000  
}  
}  
},  
{  
"range": {  
"length": {  
"gte": 50,  
"lte": 100  
}  
}  
}  
]  
}  
},  
"\_source": [  
"user"  
]  
}

Below aggregation aggregates all my documents,  
{  
"aggs": {  
"unique\_users": {  
"terms": {  
"field": "user"  
}  
}  
}  
}  
Instead I want to put my query from above into my aggregation of unique  
users.. Is it possible? Or do I have to just do my normal query and  
calculate the unique users myself from the query result?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7dd1fba8-a5fc-4234-9f95-88995ab9e1fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7dd1fba8-a5fc-4234-9f95-88995ab9e1fd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Anil\_Karaka](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Anil\_Karaka](https://discuss.elastic.co/u/Anil_Karaka)\
**Post date:** [December 1, 2014, 7:04am UTC](https://discuss.elastic.co/t/how-do-i-get-all-the-distinct-field-values-that-satisfy-a-particular-query/20974/2 "2014-12-01T07:04:49Z")

</div>

This is a silly question.  
All I need to do is treat "aggs" like "size", "query", "\_source" fields in  
the json

"size" limits the number of queries that I will get in the end.  
"aggs" aggregates the given query results accordingly.

So my resultant aggregation query would be

{  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"productPrice": {  
"gte": 5000,  
"lte": 10000  
}  
}  
},  
{  
"range": {  
"length": {  
"gte": 50,  
"lte": 100  
}  
}  
}  
]  
}  
},  
"\_source": [  
"user"  
],  
"aggs": {  
"unique\_users": {  
"terms": {  
"field": "user"  
}  
}  
}  
}

Thank you.

On Friday, November 28, 2014 1:25:05 PM UTC+5:30, Anil Karaka wrote:

> My sample document looks like this,
> 
> {  
> "user": "adfasdk",  
> "act": "Made Purchase",  
> "productPrice": 5099"  
> }
> 
> What I want is all the unique users that satisfy a particular query.
> 
> Say for example I have the query like below, that gets all the documents  
> whose productPrice is between 5000 and 10000, and whose length is between  
> 50, 100. The returned documents will have repeated "user"s. I just need  
> all the unique users. Do I have calculate all the unique users from this  
> result myself or is it possible to do using aggregation.
> 
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "range": {  
> "productPrice": {  
> "gte": 5000,  
> "lte": 10000  
> }  
> }  
> },  
> {  
> "range": {  
> "length": {  
> "gte": 50,  
> "lte": 100  
> }  
> }  
> }  
> ]  
> }  
> },  
> "\_source": [  
> "user"  
> ]  
> }
> 
> Below aggregation aggregates all my documents,  
> {  
> "aggs": {  
> "unique\_users": {  
> "terms": {  
> "field": "user"  
> }  
> }  
> }  
> }  
> Instead I want to put my query from above into my aggregation of unique  
> users.. Is it possible? Or do I have to just do my normal query and  
> calculate the unique users myself from the query result?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3e18a660-fb00-424b-b762-7bd9c52f679f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3e18a660-fb00-424b-b762-7bd9c52f679f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:46am UTC](https://discuss.elastic.co/t/how-do-i-get-all-the-distinct-field-values-that-satisfy-a-particular-query/20974/3 "2017-07-06T00:46:57Z")

</div>


