# Nested Documents - how to search for nested elements, not whole documents

**URL:** <https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457>\
**Category:** Elasticsearch\
**Created:** [October 28, 2014, 9:23am UTC](https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457 "2014-10-28T09:23:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrik\_behrens](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@henrik\_behrens](https://discuss.elastic.co/u/henrik_behrens)\
**Post date:** [October 28, 2014, 9:23am UTC](https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457/1 "2014-10-28T09:23:59Z")

</div>

Hi,

I have hierarchical data: orders consist of several order items.

How can I search for a certain orderItem without getting returned the other  
items of the same order?  
I tried to use nested documents, but I always receive the whole order and  
not the requested order item.

Example:

POST /salesorder7  
{  
"mappings": {  
"complete": {  
"properties": {  
"orderDetails": {  
"type": "nested",  
"properties": {  
"lineItemNumber": {  
"type": "string"  
},  
"productInformation": {  
"properties": {  
"id": {  
"type": "string"  
},  
"name": {  
"type": "string"  
}  
}  
},  
"totalLineItemPrice": {  
"properties": {  
"amount": {  
"type": "float"  
},  
"currency": {  
"type": "string"  
}  
}  
},  
"orderHeader": {  
"properties": {  
"orderNumber": {  
"type": "string"  
}  
}  
}  
}  
}  
}  
}  
}  
}

PUT /salesorder7/complete/1  
{  
"orderHeader": {  
"orderNumber": "1"  
},  
"orderDetails": [  
{  
"lineItemNumber": "11",  
"productInformation": {  
"name": "product1",  
"id": "p1"  
},  
"totalLineItemPrice": {  
"amount": "105.04",  
"currency": "EUR"  
}  
},  
{  
"totalLineItemPrice": {  
"amount": "9.99",  
"currency": "EUR"  
},  
"lineItemNumber": "12",  
"productInformation": {  
"name": "product2",  
"id": "p2"  
}  
}  
]  
}

POST /salesorder7/complete/\_search  
{  
"fields": [  
"orderHeader.orderNumber",  
"orderDetails.unitPrice.amount",  
"orderDetails.productInformation.name"  
],  
"query": {  
"nested": {  
"path": "orderDetails",  
"score\_mode": "avg",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"orderDetails.productInformation.name":  
"product1"  
}  
}  
]  
}  
}  
}  
}  
}

This returns

{  
"took": 7,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 1.4054651,  
"hits": [  
{  
"\_index": "salesorder7",  
"\_type": "complete",  
"\_id": "1",  
"\_score": 1.4054651,  
"fields": {  
"orderDetails.productInformation.name": [  
"product1",  
"product2"  
],  
"orderHeader.orderNumber": [  
"1"  
]  
}  
}  
]  
}  
}

I want it to return product1 only, and not product1 and product2.

How can I achive this?

Best regards  
Henrik

P.S. Sorry for not using CURL, under Windows it seems to only accept single  
line nonspaced documents or documents in files.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 28, 2014, 1:01pm UTC](https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457/2 "2014-10-28T13:01:48Z")

</div>

This behavior is currently not possible. The Elasticsearch team is working  
on a solution.

--  
Ivan  
On Oct 28, 2014 5:24 AM, [henrik.behrens@shs-viveon.com](mailto:henrik.behrens@shs-viveon.com) wrote:

> Hi,
> 
> I have hierarchical data: orders consist of several order items.
> 
> How can I search for a certain orderItem without getting returned the  
> other items of the same order?  
> I tried to use nested documents, but I always receive the whole order and  
> not the requested order item.
> 
> Example:
> 
> POST /salesorder7  
> {  
> "mappings": {  
> "complete": {  
> "properties": {  
> "orderDetails": {  
> "type": "nested",  
> "properties": {  
> "lineItemNumber": {  
> "type": "string"  
> },  
> "productInformation": {  
> "properties": {  
> "id": {  
> "type": "string"  
> },  
> "name": {  
> "type": "string"  
> }  
> }  
> },  
> "totalLineItemPrice": {  
> "properties": {  
> "amount": {  
> "type": "float"  
> },  
> "currency": {  
> "type": "string"  
> }  
> }  
> },  
> "orderHeader": {  
> "properties": {  
> "orderNumber": {  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> PUT /salesorder7/complete/1  
> {  
> "orderHeader": {  
> "orderNumber": "1"  
> },  
> "orderDetails": [  
> {  
> "lineItemNumber": "11",  
> "productInformation": {  
> "name": "product1",  
> "id": "p1"  
> },  
> "totalLineItemPrice": {  
> "amount": "105.04",  
> "currency": "EUR"  
> }  
> },  
> {  
> "totalLineItemPrice": {  
> "amount": "9.99",  
> "currency": "EUR"  
> },  
> "lineItemNumber": "12",  
> "productInformation": {  
> "name": "product2",  
> "id": "p2"  
> }  
> }  
> ]  
> }
> 
> POST /salesorder7/complete/\_search  
> {  
> "fields": [  
> "orderHeader.orderNumber",  
> "orderDetails.unitPrice.amount",  
> "orderDetails.productInformation.name"  
> ],  
> "query": {  
> "nested": {  
> "path": "orderDetails",  
> "score\_mode": "avg",  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "match": {  
> "orderDetails.productInformation.name":  
> "product1"  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> This returns
> 
> {  
> "took": 7,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 1.4054651,  
> "hits": [  
> {  
> "\_index": "salesorder7",  
> "\_type": "complete",  
> "\_id": "1",  
> "\_score": 1.4054651,  
> "fields": {  
> "orderDetails.productInformation.name": [  
> "product1",  
> "product2"  
> ],  
> "orderHeader.orderNumber": [  
> "1"  
> ]  
> }  
> }  
> ]  
> }  
> }
> 
> I want it to return product1 only, and not product1 and product2.
> 
> How can I achive this?
> 
> Best regards  
> Henrik
> 
> P.S. Sorry for not using CURL, under Windows it seems to only accept  
> single line nonspaced documents or documents in files.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4eaf18d4-2db3-4264-8dfc-767ca34d5f28%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCpR3LP%2ByeJDhD9j\_kHuwSHQuU79%3DdHR\_18-YfAMNsGOw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCpR3LP%2ByeJDhD9j_kHuwSHQuU79%3DdHR_18-YfAMNsGOw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![chakkasm](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@chakkasm](https://discuss.elastic.co/u/chakkasm)\
**Post date:** [September 7, 2016, 4:10am UTC](https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457/3 "2016-09-07T04:10:39Z")

</div>

Is this feature implemented in Elasticsearch 2.3.4 version?? I also have a similar requirement..

---

<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 5, 2017, 10:22pm UTC](https://discuss.elastic.co/t/nested-documents-how-to-search-for-nested-elements-not-whole-documents/20457/4 "2017-07-05T22:22:08Z")

</div>

Closed on July 5, 2017, 10:22pm UTC


