# Nested Query

**URL:** <https://discuss.elastic.co/t/nested-query/4941>\
**Category:** Elasticsearch\
**Created:** [July 25, 2011, 1:45am UTC](https://discuss.elastic.co/t/nested-query/4941 "2011-07-25T01:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kikster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kikster/32/3117_2.png) [@kikster](https://discuss.elastic.co/u/kikster)\
**Post date:** [July 25, 2011, 1:45am UTC](https://discuss.elastic.co/t/nested-query/4941/1 "2011-07-25T01:45:13Z")

</div>

I created a tweet mapping with a nested field calls "comments" (localhost:9200/twitter/tweet/\_mapping)

{"tweet":{"properties":{"message":{"type":"string"},"user":{"type":"string"},"comments":{"properties":{"text":{"type":"string"},"name":{"type":"string"}},"type":"nested"}}}}

I added a few entries to it and tried to issue a nested query as follows.

curl -XGET '[http://localhost.com:9200/twitter/tweet/\_search](http://localhost.com:9200/twitter/tweet/_search)' -d  
'{"nested" :  
{ "path": "comments",  
"query" :  
{"query\_string" :  
{"query" : "Johnny"}}}}'

However I get the following error:

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[pVbYE8kxSnKqAI1Fn9FMWA][twitter][0]: SearchParseException[[twitter][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" : {"path" : "tweet.comments", "query" : "hello"}}]]]; nested: SearchParseException[[twitter][0]: from[-1],size[-1]: Parse Failure [No parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][1]: SearchParseException[[twitter][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" : {"path" : "tweet.comments", "query" : "hello"}}]]]; nested: SearchParseException[[twitter][1]: from[-1],size[-1]: Parse Failure [No parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][2]: SearchParseException[[twitter][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" : {"path" : "tweet.comments", "query" : "hello"}}]]]; nested: SearchParseException[[twitter][2]: from[-1],size[-1]: Parse Failure [No parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][3]: SearchParseException[[twitter][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" : {"path" : "tweet.comments", "query" : "hello"}}]]]; nested: SearchParseException[[twitter][3]: from[-1],size[-1]: Parse Failure [No parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][4]: SearchParseException[[twitter][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" : {"path" : "tweet.comments", "query" : "hello"}}]]]; nested: SearchParseException[[twitter][4]: from[-1],size[-1]: Parse Failure [No parser for element [nested]]]; }]","status":500}

I've read the documentation a million times and am pretty sure I've done everything required. Any idea as to why the query is failing to parse?

---

<div class="post-metadata">

**Author:** ![ofavre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ofavre/32/116819_2.png) [@ofavre](https://discuss.elastic.co/u/ofavre)\
**Post date:** [July 25, 2011, 8:17am UTC](https://discuss.elastic.co/t/nested-query/4941/2 "2011-07-25T08:17:05Z")

</div>

The documentation is not clear enough for beginners. It needs some practice  
to better understand it.

You missed (at least) one thing:  
The excerpts given (like in  
[Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html))  
are only a part of the whole query.  
May be this page will help you too:  
[Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.html)  
As queries can be nested, the documentation skips the... wrapping (I can't  
find a better term...)  
A minimum wrapping in order to create a "request body" from a simple query  
is:  
{  
"query":  
{ ...put your query here... }  
}

Here is what your call should look like:  
curl -XGET '[http://www.tutorcrew.com:9200/twitter/tweet/\_search](http://www.tutorcrew.com:9200/twitter/tweet/_search)' -d  
'_{_

- "query" : \*{  
"nested" : {  
"path" : "comments",  
"query" : {  
"query\_string" : {  
"query" : "Johnny"  
}  
}  
}  
}  
_}_'

Also, make sure your mapping is correct:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

It should be just like the "object" mapping (  
[Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type.html)), but  
with "type":"nested" instead of "type":"object".  
And like for the query, there is a minimum wrapping for integrating the  
documentation excerpts.  
The following command should help you see what the current mapping is (not  
sure how it works for dynamic mappings):  
curl -XGET '[http://www.tutorcrew.com:9200/twitter/tweet/\_mapping?pretty=1](http://www.tutorcrew.com:9200/twitter/tweet/_mapping?pretty=1)'

## Happy experiencing with Elasticsearch!

Olivier Favre

[www.yakaz.com](http://www.yakaz.com)

2011/7/25 cdolive [via Elasticsearch Users] \<  
[ml-node+3196303-72493033-393975@n3.nabble.com](mailto:ml-node+3196303-72493033-393975@n3.nabble.com)\>

> I created a tweet mapping with a nested field calls "comments"  
> (localhost:9200/twitter/tweet/\_mapping)
> 
> {"tweet":{"properties":{"message":{"type":"string"},"user":{"type":"string"},"comments":{"properties":{"text":{"type":"string"},"name":{"type":"string"}},"type":"nested"}}}}
> 
> I added a few entries to it and tried to issue a nested query as follows.
> 
> curl -XGET '[http://www.tutorcrew.com:9200/twitter/tweet/\_search](http://www.tutorcrew.com:9200/twitter/tweet/_search)' -d  
> '{"nested" :  
> { "path": "comments",  
> "query" :  
> {"query\_string" :  
> {"query" : "Johnny"}}}}'
> 
> However I get the following error:
> 
> {"error":"SearchPhaseExecutionException[Failed to execute phase [query],  
> total failure; shardFailures {[pVbYE8kxSnKqAI1Fn9FMWA][twitter][0]:  
> SearchParseException[[twitter][0]: from[-1],size[-1]: Parse Failure [Failed  
> to parse source [{"nested" : {"path" : "tweet.comments", "query" :  
> "hello"}}]]]; nested: SearchParseException[[twitter][0]:  
> from[-1],size[-1]: Parse Failure [No parser for element [nested]]];  
> }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][1]: SearchParseException[[twitter][1]:  
> from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" :  
> {"path" : "tweet.comments", "query" : "hello"}}]]]; nested:  
> SearchParseException[[twitter][1]: from[-1],size[-1]: Parse Failure [No  
> parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][2]:  
> SearchParseException[[twitter][2]: from[-1],size[-1]: Parse Failure [Failed  
> to parse source [{"nested" : {"path" : "tweet.comments", "query" :  
> "hello"}}]]]; nested: SearchParseException[[twitter][2]:  
> from[-1],size[-1]: Parse Failure [No parser for element [nested]]];  
> }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][3]: SearchParseException[[twitter][3]:  
> from[-1],size[-1]: Parse Failure [Failed to parse source [{"nested" :  
> {"path" : "tweet.comments", "query" : "hello"}}]]]; nested:  
> SearchParseException[[twitter][3]: from[-1],size[-1]: Parse Failure [No  
> parser for element [nested]]]; }{[pVbYE8kxSnKqAI1Fn9FMWA][twitter][4]:  
> SearchParseException[[twitter][4]: from[-1],size[-1]: Parse Failure [Failed  
> to parse source [{"nested" : {"path" : "tweet.comments", "query" :  
> "hello"}}]]]; nested: SearchParseException[[twitter][4]:  
> from[-1],size[-1]: Parse Failure [No parser for element [nested]]];  
> }]","status":500}
> 
> I've read the documentation a million times and am pretty sure I've done  
> everything required. Any idea as to why the query is failing to parse?
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Nested-Query-tp3196303p3196303.html](http://elasticsearch-users.115913.n3.nabble.com/Nested-Query-tp3196303p3196303.html)  
> To start a new topic under Elasticsearch Users, email  
> [ml-node+115913-1699315842-393975@n3.nabble.com](mailto:ml-node+115913-1699315842-393975@n3.nabble.com)  
> To unsubscribe from Elasticsearch Users, click here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=115913&code=b2xpdmllckB5YWthei5jb218MTE1OTEzfDIxMjI2MTYwOTc=](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=115913&code=b2xpdmllckB5YWthei5jb218MTE1OTEzfDIxMjI2MTYwOTc=).

---

<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:59am UTC](https://discuss.elastic.co/t/nested-query/4941/3 "2017-07-06T03:59:37Z")

</div>


