Nathan
Nathan

Reputation: 138

ElasticSearch Count Distinct Value from Pair

Say we have a sql table store information like (sale-time, book, sale-person) which stores the time when one sale person sold the book. We want to have a popular book list which is sold by most sales.

The sql is like this

select book, count (sale-person)
from select distinct (book, sale-person) from table
group by book
order desc

How to achieve such search in elasticsearch?

Suppose our data is like this

{ "time": "time-value",
  "book" : "book name",
  "sales" : "person"
}

Thanks.

Upvotes: 0

Views: 105

Answers (1)

Thomas Decaux
Thomas Decaux

Reputation: 22691

If I understood fine, you want to do a nested aggregation?

Something like:

GET /my_index/_search
{
   "size": 0,
   "query": {
      "match_all" : {}
   },
   "aggs": {
      "time_windows": {
         "date_histogram": {
            "field": "time",
            "interval" : "1h"
         },
         "aggs": {
            "books": {
               "terms": {
                  "size": 5,
                  "field": "book"
               }
            }
         }
      }
   }
}

Upvotes: 1

Related Questions