lundi 30 octobre 2017

elasticsearch: get random distinct field values?

We have elastic search document with dealerId "field". Multiple documents can have the same "dealerId". We want to pick "N" random dealers from it.

What I have done so far: The following query would return max 1000 "dealerId" and their count in descending order. We will then randomly pick "N" records client side.

    {
      "from":0,
      "size":0,
          "aggs":{
            "CityIdCount":{
              "terms":{
                "field":"dealerId",
                "size":1000
              }
            }
          }
     }

The downside with this approach is that:

  1. If in future, we have more than 1K unique dealers, this approach would fail as it would pick only top 1K dealerId occurence. What should we put as "size" for this?
  2. We are fetching all the data although we just require random "N" i.e. 3 or 4 random "dealerId" from elastic server to the client. Can we somehow do this randomization in elastic query itself?

I have read something similar here but trying to check if we have some solution for this now.




Aucun commentaire:

Enregistrer un commentaire