1

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.

syllogismos
  • 600
  • 2
  • 15
  • 39

1 Answers1

2

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"
      }
    }
  }
}
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • How do I get the count of all the aggregations, here all the users. I just want the number of unique users, I don't need other stats like doc count of each user that satisfy a given query. Do you recommend using aggregations, or just query all the docs with `user` projection and then get unique users myself without relying on elastic search.. – syllogismos Nov 28 '14 at 11:08
  • 1
    Then use this instead: `"aggs": { "unique_users": { "cardinality": { "field": "user" } } }` as a replacement for the `aggs` in my proposed solution. – Andrei Stefan Nov 28 '14 at 11:22
  • Sorry I'm new to Elastic search. I read all these things, after you tell me the answer I think to myself ofcourse. Its just too much to learn at a time, at least for me. Cardinality approximates a little and just gives the number of unique users that satisfy a particular query. I also want all the unique users. – syllogismos Nov 28 '14 at 11:36
  • Isn't this what you wanted? Unique users that satisfy a particular query... I'm puzzled.... – Andrei Stefan Nov 28 '14 at 11:37
  • cardinality is just giving me the number of unique users, I also want the list of them.. not just the count.. – syllogismos Nov 28 '14 at 11:39
  • I got it.. thanks.. `terms` gives me the users, and `cardinality` gives me the count.. – syllogismos Nov 28 '14 at 11:41
  • Are there any cheat sheets for elastic search rest api? and some recommendations for blogs/books like learnyouahaskell for haskell.. – syllogismos Nov 28 '14 at 11:42
  • There is a book, which is really good but is quite long. If you want to properly learn, though, I highly recommend it: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/index.html – Andrei Stefan Nov 28 '14 at 12:20