My sample document looks like this.
{
"user": "dslfjd",
"productLength": 68,
"productPrice": 4500,
"action": "Made Purchse"
}
I want to get all the users that brought products whose price is between 4000 and 10000 and whose length is between 50 and 100. The following query returns all the documents that satisfy the above conditions.
{
"query": {
"bool": {
"must": [
{
"term": {
"act": "Made Purchase"
}
},
{
"range": {
"productPrice": {
"gte": 4000,
"lte": 10000
}
}
},
{
"range": {
"length": {
"gte": 50,
"lte": 100
}
}
}
]
}
}
}
Here I will get all the documents that satisfy the above query clauses, I can even project my response by just specifying "_source" = ["user"]
so that I don't get the entire document but just the user
Instead what I want is a list of all the unique distinct users. Instead of all the documents that may have user
field repeated.
An aggregation like below
{
"aggs": {
"unique_users": {
"terms": {
"field": "user"
}
}
}
}
aggregates all the documents, instead I want aggregation on documents that satisfy any query. I feel like I'm missing a simple thing like defining my query inside my aggregation. But I don't know what it is.
It's too simple, or you need something else from what you described:
GET /some_index/some_type/_search?search_type=count
{
"query": {
"bool": {
"must": [
{
"term": {
"action": "Made Purchase"
}
},
{
"range": {
"productPrice": {
"gte": 4000,
"lte": 10000
}
}
},
{
"range": {
"productLength": {
"gte": 50,
"lte": 100
}
}
}
]
}
},
"aggs": {
"unique_users": {
"terms": {
"field": "user"
}
}
}
}