# Getting unique list from array of nested objects

**URL:** <https://discuss.elastic.co/t/getting-unique-list-from-array-of-nested-objects/9623>\
**Category:** Elasticsearch\
**Created:** [November 8, 2012, 7:50am UTC](https://discuss.elastic.co/t/getting-unique-list-from-array-of-nested-objects/9623 "2012-11-08T07:50:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Huang\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_huang_2/32/2641_2.png) [@Mark\_Huang\_2](https://discuss.elastic.co/u/Mark_Huang_2)\
**Post date:** [November 8, 2012, 7:50am UTC](https://discuss.elastic.co/t/getting-unique-list-from-array-of-nested-objects/9623/1 "2012-11-08T07:50:41Z")

</div>

Hi guys,

I am using couchdb with elasticsearch and up until recently, I've been a  
very happy and satisfied user. Recently there was an issue that I could  
not figure how to solve using pure ElasticSearch. I wanted to ask whether  
such a thing is even possible. So here it goes:

I have a two types of documents:

1. product
2. productionOrder

A productionOrder may contain one or many products. So obviously if I  
wanted to retrieve a list of products that are contained within  
productionOrders (I need to retrieve products that have productionOrders  
being made) I'll get a list that will most-probably have duplicate  
products.

First off, I needed to devise a query to obtain the products found in  
productionOrders. I read through the docs and it seems like I have to map  
my products within productionOrders using the type "nested". After this I  
can do a query on products using the has\_parent filter with the parent\_type  
set to "productionOrder". _Is this correct?_  
\*  
\*  
Next off, I need to remove the duplicate products from the resultset. Now,  
I have no idea how to do this purely in elasticsearch. I need a pure  
elasticsearch query/filter option because I use it for  
paging/sorting/filtering my kendoUI grids. I'm using pyes. _How would I  
remove the duplicates, if the only thing that differentiates a product from  
another is their id attribute?_  
\*  
\*

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [November 8, 2012, 12:28pm UTC](https://discuss.elastic.co/t/getting-unique-list-from-array-of-nested-objects/9623/2 "2012-11-08T12:28:31Z")

</div>

Hi Mark,

In order to use the has\_parent filter you need to use the \_parent  
field in your mapping instead of the nested type.  
When using the parent child capabilities of elasticsearch you need to  
determine what is your parent type and what is  
your child type. From what I understand from your email it seems that  
productionOrder is the parent. Take a look at about the \_parent field:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

If you configured the \_parent field properly you can use the  
top\_children, has\_child and has\_parent queries and filters.  
I just uploaded my slides about document relations: (this also covers  
nested objects and parent child in ES):

> **[Document relations](https://www.slideshare.net/martijnvg/document-relations)**
>
> Document relations - Download as a PDF or view online for free

Elastic search doesn't support removing duplicates from the result set  
(also knows as result grouping) yet. You can achieve psuedo result  
grouping by using faceting on productOrderId field (the field in the  
product mapping referring to the productOrder it belongs to) in  
combination with subsequent search requests.

Martijn

On 8 November 2012 08:50, Mark Huang [zhenghao12@gmail.com](mailto:zhenghao12@gmail.com) wrote:

> Hi guys,
> 
> I am using couchdb with elasticsearch and up until recently, I've been a  
> very happy and satisfied user. Recently there was an issue that I could not  
> figure how to solve using pure Elasticsearch. I wanted to ask whether such  
> a thing is even possible. So here it goes:
> 
> I have a two types of documents:
> 
> 1. product
> 2. productionOrder
> 
> A productionOrder may contain one or many products. So obviously if I  
> wanted to retrieve a list of products that are contained within  
> productionOrders (I need to retrieve products that have productionOrders  
> being made) I'll get a list that will most-probably have duplicate products.
> 
> First off, I needed to devise a query to obtain the products found in  
> productionOrders. I read through the docs and it seems like I have to map  
> my products within productionOrders using the type "nested". After this I  
> can do a query on products using the has\_parent filter with the parent\_type  
> set to "productionOrder". Is this correct?
> 
> Next off, I need to remove the duplicate products from the resultset. Now,  
> I have no idea how to do this purely in elasticsearch. I need a pure  
> elasticsearch query/filter option because I use it for  
> paging/sorting/filtering my kendoUI grids. I'm using pyes. How would I  
> remove the duplicates, if the only thing that differentiates a product from  
> another is their id attribute?
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<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:** [July 6, 2017, 3:05am UTC](https://discuss.elastic.co/t/getting-unique-list-from-array-of-nested-objects/9623/3 "2017-07-06T03:05:24Z")

</div>


