# How to get distinct document based on a specific field

**URL:** <https://discuss.elastic.co/t/how-to-get-distinct-document-based-on-a-specific-field/93564>\
**Category:** Elasticsearch\
**Created:** [July 18, 2017, 11:14am UTC](https://discuss.elastic.co/t/how-to-get-distinct-document-based-on-a-specific-field/93564 "2017-07-18T11:14:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surajshrestha](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@surajshrestha](https://discuss.elastic.co/u/surajshrestha)\
**Post date:** [July 18, 2017, 11:14am UTC](https://discuss.elastic.co/t/how-to-get-distinct-document-based-on-a-specific-field/93564/1 "2017-07-18T11:14:51Z")

</div>

In relational database there are two table 1. company and 2. product. One company can have many products.  
In elasticsearch I created a type "product" inside the index "my\_index". The product document structure is as follows:

```
"product" : {
         "id" : 1,
         "name" : "some name",
        "price": 100,
        "company" : {
              "id" : 405,
             "name" : "company name",
            "search_position" : 1
       }
    }

```

When I search for product I get multiple document results along with its company object. 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.  
I tried with aggregation but got result in bucktes and I'm unable to paginate bucktes. Also I need to sort result by company search position.  
Please help me. I need to fix it asap.  
Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 15, 2017, 11:14am UTC](https://discuss.elastic.co/t/how-to-get-distinct-document-based-on-a-specific-field/93564/2 "2017-08-15T11:14:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
