mardi 10 octobre 2017

elastic search: get random distinct document based on a specific field

In relational database there are two table 1. company and 2. product. One company can have many products. In elasticsearch, I have "product" fields inside the index "my_index". The product document structure is as follows:

"product"  :    {
         "id" :  1,
         "name" : "some name",
         "price": 100,
         "company_id" : 405,
         "company_name" : "company name",
         "company_search_position" : 1
    }

When I search for product, I get multiple document results along with its company details. But problem is when one company has 3 products, all those 3 products are retrieved. I want to get only one product of a particular company i.e distinct product document based on that company and that too randomly. For example, in first request if I ask for 3 products, they should be randomly picked products with all having distinct company_id. In the second request, if we ask for 3 products, they should be randomly picked products with distinct company_id.

The question is already open in elastic QA forum here but with no response.




Aucun commentaire:

Enregistrer un commentaire