# Searching by nested fields

**URL:** <https://discuss.elastic.co/t/searching-by-nested-fields/18302>\
**Category:** Elasticsearch\
**Created:** [June 24, 2014, 5:03pm UTC](https://discuss.elastic.co/t/searching-by-nested-fields/18302 "2014-06-24T17:03:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danylo\_Vivchar](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@Danylo\_Vivchar](https://discuss.elastic.co/u/Danylo_Vivchar)\
**Post date:** [June 24, 2014, 5:03pm UTC](https://discuss.elastic.co/t/searching-by-nested-fields/18302/1 "2014-06-24T17:03:04Z")

</div>

I have a trouble searching by nested field.

For example my document is:

{  
"chat": [  
{  
"messages": [  
{  
"id": "61",  
"text": "some text here"  
},  
{  
"id": "62",  
"text": "some62 text62 here62"  
},  
{  
"id": "63",  
"text": "some63 text63 here63"  
},  
{  
"id": "64",  
"text": "some64 text64 here64"  
}  
]  
}  
]  
}

I'm searching through messages.text. If I search

"text": "some"

{  
"query" : {  
"nested": {  
"path": "messages",  
"query": {  
"bool": {  
"must": [{  
"match": {  
"text": "some"  
}  
}]  
}  
}  
}  
}  
}

I want output something like

{  
"chat": [  
{  
"messages": [  
{  
"id": "61",  
"text": "some text here"  
}  
]  
}  
]  
}

and not the whole document. I suppose I should use nested property, so  
messages in chat is mapped as "nested". Please anyone help me.

--  
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/0ade073a-38b1-4384-b56b-623ca92de033%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0ade073a-38b1-4384-b56b-623ca92de033%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:** [June 26, 2014, 2:44am UTC](https://discuss.elastic.co/t/searching-by-nested-fields/18302/2 "2014-06-26T02:44:20Z")

</div>

That feature is currently not supported by Elasticsearch. You would need to  
change to parent/child documents in order to get back only the relevant  
documents.

--  
Ivan

On Tue, Jun 24, 2014 at 10:03 AM, Danylo Vivchar [vivchar.danylo@gmail.com](mailto:vivchar.danylo@gmail.com)  
wrote:

> I have a trouble searching by nested field.
> 
> For example my document is:
> 
> {  
> "chat": [  
> {  
> "messages": [  
> {  
> "id": "61",  
> "text": "some text here"  
> },  
> {  
> "id": "62",  
> "text": "some62 text62 here62"  
> },  
> {  
> "id": "63",  
> "text": "some63 text63 here63"  
> },  
> {  
> "id": "64",  
> "text": "some64 text64 here64"  
> }  
> ]  
> }  
> ]  
> }
> 
> I'm searching through messages.text. If I search
> 
> "text": "some"
> 
> {  
> "query" : {  
> "nested": {  
> "path": "messages",  
> "query": {  
> "bool": {  
> "must": [{  
> "match": {  
> "text": "some"  
> }  
> }]  
> }  
> }  
> }  
> }  
> }
> 
> I want output something like
> 
> {  
> "chat": [  
> {  
> "messages": [  
> {  
> "id": "61",  
> "text": "some text here"  
> }  
> ]  
> }  
> ]  
> }
> 
> and not the whole document. I suppose I should use nested property, so  
> messages in chat is mapped as "nested". Please anyone help me.
> 
> --  
> 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/0ade073a-38b1-4384-b56b-623ca92de033%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0ade073a-38b1-4384-b56b-623ca92de033%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/0ade073a-38b1-4384-b56b-623ca92de033%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0ade073a-38b1-4384-b56b-623ca92de033%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%3DcQBh57aZbu2%3DxwZDPyR3sQx4Jv9Ny%3DJa3FKd9%3D8pHSRZ-Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBh57aZbu2%3DxwZDPyR3sQx4Jv9Ny%3DJa3FKd9%3D8pHSRZ-Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Danylo\_Vivchar](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@Danylo\_Vivchar](https://discuss.elastic.co/u/Danylo_Vivchar)\
**Post date:** [June 27, 2014, 2:33pm UTC](https://discuss.elastic.co/t/searching-by-nested-fields/18302/3 "2014-06-27T14:33:00Z")

</div>

> thanx, parent/child documents helped me

--  
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/4de2deb5-da24-44a6-a1e8-b29e556850f7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4de2deb5-da24-44a6-a1e8-b29e556850f7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:19am UTC](https://discuss.elastic.co/t/searching-by-nested-fields/18302/4 "2017-07-06T01:19:16Z")

</div>


