# Nested Array Document Query (similar to @tsolakp's problem)

**URL:** <https://discuss.elastic.co/t/nested-array-document-query-similar-to-tsolakps-problem/4946>\
**Category:** Elasticsearch\
**Created:** [July 25, 2011, 8:06pm UTC](https://discuss.elastic.co/t/nested-array-document-query-similar-to-tsolakps-problem/4946 "2011-07-25T20:06:17Z")\
**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, 8:06pm UTC](https://discuss.elastic.co/t/nested-array-document-query-similar-to-tsolakps-problem/4946/1 "2011-07-25T20:06:17Z")

</div>

I imagine tsolakp and I are having the same problem, but I did get a nested query to run on a nested field that's a simple Hash. However, I'm having problem querying a nested document that contains an array of hashes (exactly like obj1 here: [http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)).

Here's the mapping I've got.

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

and I indexed this document

{  
"name" : "Jane",  
"comments" : [  
{"text" : "this is text", "username" : "Jane"},  
{"text" : "this is more text", "username" : "Jack"}  
]  
}

I'm trying to search the comments section by username and I've issued the following query:

{  
"query" : {  
"nested" : {  
"path" : "comments",  
"query" : {  
"bool" : {  
"must" : [  
{  
"text" : {  
"comments.username" : "Jane"  
}  
}  
]  
}  
}  
}  
}  
}

However I'm not getting any hits....

I basically followed the documentation exactly and am not sure why this is happening. Any ideas?

---UPDATE

turns out this query works, but the text query does not still looking into it....

'{  
"query" : {  
"nested" : {  
"path" : "comments",  
"query" : {  
"query\_string" : {  
"default\_field" : "comments.text",  
"query" : "this is more text"}}}}}'

--UPDATE2  
I tried a simple text query on comments.text and it returned no hits. I must be missing something about how the text query works...

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 27, 2011, 4:38am UTC](https://discuss.elastic.co/t/nested-array-document-query-similar-to-tsolakps-problem/4946/2 "2011-07-27T04:38:41Z")

</div>

Both your mapping and your query are not valid jsons (use jsonlint for  
example to test that: [http://jsonlint.com/](http://jsonlint.com/)). Here is a gist of the valid  
ones: [gist:1108683 · GitHub](https://gist.github.com/1108683).

A note to users posting on the list, it will be much simpler to help you  
with gisted curl recreations, and not pasting random bits and pieces into  
the mail.

On Mon, Jul 25, 2011 at 11:06 PM, kikster \<  
[christopherdavidolivares@gmail.com](mailto:christopherdavidolivares@gmail.com)\> wrote:

> I imagine tsolakp and I are having the same problem, but I did get a nested  
> query to run on a nested field that's a simple Hash. However, I'm having  
> problem querying a nested document that contains an array of hashes  
> (exactly  
> like obj1 here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)).
> 
> Here's the mapping I've got.
> 
> {  
> "tweet" : {  
> "properties" : {  
> "name" : {  
> "type" : "string"  
> },  
> "comments" : {  
> "properties" : {  
> "text" : {  
> "type" : "string"  
> },  
> "username" : {  
> "type" : "string"  
> },  
> },  
> "type" : "nested"  
> }  
> }  
> }  
> }
> 
> and I indexed this document
> 
> {  
> "name" : "Jane",  
> "comments" : [  
> {"text" : "this is text", "username" : "Jane"},  
> {"text" : "this is more text", "username" : "Jack"}  
> ]  
> }
> 
> I'm trying to search the comments section by username and I've issued the  
> following query:
> 
> {  
> "query" : {  
> "nested" : {  
> "path" : "comments",  
> "query" : {  
> "bool" : {  
> "must" : [  
> "text" : {  
> "comments.text" : "this"  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> However I'm getting a parsing error like this:  
> Parse Failure [Failed to parse source [{"query" : {"nested" ..........
> 
> I basically followed the documentation exactly (after help from @favre) but  
> am not sure why I'm still getting an error. Any ideas?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Nested-Array-Doc-Query-similar-to-tsolakp-problem-tp3198558p3198558.html](http://elasticsearch-users.115913.n3.nabble.com/Nested-Array-Doc-Query-similar-to-tsolakp-problem-tp3198558p3198558.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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-array-document-query-similar-to-tsolakps-problem/4946/3 "2017-07-06T03:59:09Z")

</div>


