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

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7dd1fba8-a5fc-4234-9f95-88995ab9e1fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/3e18a660-fb00-424b-b762-7bd9c52f679f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.