# Nested query

**URL:** <https://discuss.elastic.co/t/nested-query/11125>\
**Category:** Elasticsearch\
**Created:** [March 13, 2013, 6:26am UTC](https://discuss.elastic.co/t/nested-query/11125 "2013-03-13T06:26:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)\
**Post date:** [March 13, 2013, 6:26am UTC](https://discuss.elastic.co/t/nested-query/11125/1 "2013-03-13T06:26:20Z")

</div>

Hi

I've created the following Index and mapping :

Index : curl -XPUT '[http://10.118.192.165:9200/matches](http://10.118.192.165:9200/matches)'

Mapping:

curl -XPUT '[http://10.118.192.165:9200/matches/tags\_nested/\_mapping](http://10.118.192.165:9200/matches/tags_nested/_mapping)' -d '  
{  
"tags\_nested":{  
"properties":{  
"name": {  
"type": "string"  
},  
"total\_runs": {  
"type": "integer"  
},  
"matches\_played": {  
"type": "string"  
},  
"highest\_score": {  
"type": "string"  
},  
"tags": {  
"type" : "nested"  
}

}  
}  
}'

the data inserted is :

curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/1](http://10.118.192.165:9200/matches/tags_nested/1)' -d'  
{  
"name" : "sachin",  
"total\_runs" : "500",  
"matches\_played" : "4",  
"highest\_score" : "200\*",  
"tags" :[  
{  
"user" : "kuwar",  
"tag" : "master blaster"  
},  
{  
"user" : "nipun",  
"tag" : "world class"  
},  
{  
"user" : "mayank",  
"tag" : "200 not out"  
}  
]

}'

curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/2](http://10.118.192.165:9200/matches/tags_nested/2)' -d'  
{  
"name" : "hayden",  
"total\_runs" : "300",  
"matches\_played" : "4",  
"highest\_score" : "110",  
"tags" :[  
{  
"user" : "kuwar",  
"tag" : "good knock"  
},  
{  
"user" : "nipun",  
"tag" : "match winning"  
}  
]

}'

I want to find the record where the user "kuwar" has a tag "master blaster"  
. Have designed the following query but it does not give me the result.

curl -XGET  
'[http://10.118.192.165:9200/matches/tags\_nested/\_search?pretty=true](http://10.118.192.165:9200/matches/tags_nested/_search?pretty=true)' -d '  
{  
"query": {  
"nested": {  
"path": "tags",  
"query":{  
"filtered": {  
"query": { "match\_all": {}},  
"filter": {  
"and": [  
{"term": {"tags.user": "kuwar"}},  
{"term": {"tags.tag": "master blaster"}}  
]  
}  
}  
}  
}  
}  
}'

the query does not give me the result. however if i change tags.tag to  
"master" instead of "master blaster" the output comes.

how to solve this problem ?  
thanks in advance.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [March 13, 2013, 7:31am UTC](https://discuss.elastic.co/t/nested-query/11125/2 "2013-03-13T07:31:14Z")

</div>

Hello Kuwar,

I think the problem here is that your tags are analyzed when you index them  
(eg: "master blaster" is broken into terms "master" and "blaster"), while  
your term queries are not analyzed. So in your example, you're searching  
for the term "master blaster", which isn't there.

If you search for "master" or "blaster", it should find what you're looking  
for.

Alternatively, if you want to have exact matches only (eg: match only  
"master blaster"), then you need to set that "tag" field in the mapping to  
"not\_analyzed".

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Mar 13, 2013 at 8:26 AM, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com) wrote:

> Hi
> 
> I've created the following Index and mapping :
> 
> Index : curl -XPUT '[http://10.118.192.165:9200/matches](http://10.118.192.165:9200/matches)'
> 
> Mapping:
> 
> curl -XPUT '[http://10.118.192.165:9200/matches/tags\_nested/\_mapping](http://10.118.192.165:9200/matches/tags_nested/_mapping)' -d '  
> {  
> "tags\_nested":{  
> "properties":{  
> "name": {  
> "type": "string"  
> },  
> "total\_runs": {  
> "type": "integer"  
> },  
> "matches\_played": {  
> "type": "string"  
> },  
> "highest\_score": {  
> "type": "string"  
> },  
> "tags": {  
> "type" : "nested"  
> }
> 
> }  
> }  
> }'
> 
> the data inserted is :
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/1](http://10.118.192.165:9200/matches/tags_nested/1)' -d'  
> {  
> "name" : "sachin",  
> "total\_runs" : "500",  
> "matches\_played" : "4",  
> "highest\_score" : "200\*",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "master blaster"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "world class"  
> },  
> {  
> "user" : "mayank",  
> "tag" : "200 not out"  
> }  
> ]
> 
> }'
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/2](http://10.118.192.165:9200/matches/tags_nested/2)' -d'  
> {  
> "name" : "hayden",  
> "total\_runs" : "300",  
> "matches\_played" : "4",  
> "highest\_score" : "110",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "good knock"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "match winning"  
> }  
> ]
> 
> }'
> 
> I want to find the record where the user "kuwar" has a tag "master  
> blaster" . Have designed the following query but it does not give me the  
> result.
> 
> curl -XGET '  
> [http://10.118.192.165:9200/matches/tags\_nested/\_search?pretty=true](http://10.118.192.165:9200/matches/tags_nested/_search?pretty=true)' -d '  
> {  
> "query": {  
> "nested": {  
> "path": "tags",  
> "query":{  
> "filtered": {  
> "query": { "match\_all": {}},  
> "filter": {  
> "and": [  
> {"term": {"tags.user": "kuwar"}},  
> {"term": {"tags.tag": "master blaster"}}  
> ]  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> the query does not give me the result. however if i change tags.tag to  
> "master" instead of "master blaster" the output comes.
> 
> how to solve this problem ?  
> thanks in advance.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Suraj\_Bhansali](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Suraj\_Bhansali](https://discuss.elastic.co/u/Suraj_Bhansali)\
**Post date:** [March 13, 2013, 8:59am UTC](https://discuss.elastic.co/t/nested-query/11125/3 "2013-03-13T08:59:08Z")

</div>

Hello dadoonet,

Thanks for quick reply,

I have created template that source was disabled but other fields are  
stored and indexed still. I dont want to remove doc, I want only specific  
field with value from this doc. And those fields will be available into  
Field list in Elastic Search cluster. I have created template with those  
field and they are added into list. But when I push data from couchbase to  
Elasticsearch then it added all fields.

Can you please let me now some steps for create template for same.

Thanks  
Suraj Bhansali

On Wed, Mar 13, 2013 at 11:56 AM, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com)wrote:

> Hi
> 
> I've created the following Index and mapping :
> 
> Index : curl -XPUT '[http://10.118.192.165:9200/matches](http://10.118.192.165:9200/matches)'
> 
> Mapping:
> 
> curl -XPUT '[http://10.118.192.165:9200/matches/tags\_nested/\_mapping](http://10.118.192.165:9200/matches/tags_nested/_mapping)' -d '  
> {  
> "tags\_nested":{  
> "properties":{  
> "name": {  
> "type": "string"  
> },  
> "total\_runs": {  
> "type": "integer"  
> },  
> "matches\_played": {  
> "type": "string"  
> },  
> "highest\_score": {  
> "type": "string"  
> },  
> "tags": {  
> "type" : "nested"  
> }
> 
> }  
> }  
> }'
> 
> the data inserted is :
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/1](http://10.118.192.165:9200/matches/tags_nested/1)' -d'  
> {  
> "name" : "sachin",  
> "total\_runs" : "500",  
> "matches\_played" : "4",  
> "highest\_score" : "200\*",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "master blaster"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "world class"  
> },  
> {  
> "user" : "mayank",  
> "tag" : "200 not out"  
> }  
> ]
> 
> }'
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/2](http://10.118.192.165:9200/matches/tags_nested/2)' -d'  
> {  
> "name" : "hayden",  
> "total\_runs" : "300",  
> "matches\_played" : "4",  
> "highest\_score" : "110",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "good knock"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "match winning"  
> }  
> ]
> 
> }'
> 
> I want to find the record where the user "kuwar" has a tag "master  
> blaster" . Have designed the following query but it does not give me the  
> result.
> 
> curl -XGET '  
> [http://10.118.192.165:9200/matches/tags\_nested/\_search?pretty=true](http://10.118.192.165:9200/matches/tags_nested/_search?pretty=true)' -d '  
> {  
> "query": {  
> "nested": {  
> "path": "tags",  
> "query":{  
> "filtered": {  
> "query": { "match\_all": {}},  
> "filter": {  
> "and": [  
> {"term": {"tags.user": "kuwar"}},  
> {"term": {"tags.tag": "master blaster"}}  
> ]  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> the query does not give me the result. however if i change tags.tag to  
> "master" instead of "master blaster" the output comes.
> 
> how to solve this problem ?  
> thanks in advance.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 13, 2013, 9:02am UTC](https://discuss.elastic.co/t/nested-query/11125/4 "2013-03-13T09:02:09Z")

</div>

Please, don't hijack other threads and answer in your own thread!

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 13 mars 2013 à 09:59, Suraj Bhansali [bhansalisuraj@gmail.com](mailto:bhansalisuraj@gmail.com) a écrit :

> Hello dadoonet,
> 
> Thanks for quick reply,
> 
> I have created template that source was disabled but other fields are stored and indexed still. I dont want to remove doc, I want only specific field with value from this doc. And those fields will be available into Field list in Elastic Search cluster. I have created template with those field and they are added into list. But when I push data from couchbase to Elasticsearch then it added all fields.
> 
> Can you please let me now some steps for create template for same.
> 
> Thanks  
> Suraj Bhansali
> 
> On Wed, Mar 13, 2013 at 11:56 AM, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com) wrote:  
> Hi
> 
> I've created the following Index and mapping :
> 
> Index : curl -XPUT '[http://10.118.192.165:9200/matches](http://10.118.192.165:9200/matches)'
> 
> Mapping:
> 
> curl -XPUT '[http://10.118.192.165:9200/matches/tags\_nested/\_mapping](http://10.118.192.165:9200/matches/tags_nested/_mapping)' -d '  
> {  
> "tags\_nested":{  
> "properties":{  
> "name": {  
> "type": "string"  
> },  
> "total\_runs": {  
> "type": "integer"  
> },  
> "matches\_played": {  
> "type": "string"  
> },  
> "highest\_score": {  
> "type": "string"  
> },  
> "tags": {  
> "type" : "nested"  
> }
> 
> }  
> }  
> }'
> 
> the data inserted is :
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/1](http://10.118.192.165:9200/matches/tags_nested/1)' -d'  
> {  
> "name" : "sachin",  
> "total\_runs" : "500",  
> "matches\_played" : "4",  
> "highest\_score" : "200\*",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "master blaster"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "world class"  
> },  
> {  
> "user" : "mayank",  
> "tag" : "200 not out"  
> }  
> ]
> 
> }'
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/2](http://10.118.192.165:9200/matches/tags_nested/2)' -d'  
> {  
> "name" : "hayden",  
> "total\_runs" : "300",  
> "matches\_played" : "4",  
> "highest\_score" : "110",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "good knock"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "match winning"  
> }  
> ]
> 
> }'
> 
> I want to find the record where the user "kuwar" has a tag "master blaster" . Have designed the following query but it does not give me the result.
> 
> curl -XGET '[http://10.118.192.165:9200/matches/tags\_nested/\_search?pretty=true](http://10.118.192.165:9200/matches/tags_nested/_search?pretty=true)' -d '  
> {  
> "query": {  
> "nested": {  
> "path": "tags",  
> "query":{  
> "filtered": {  
> "query": { "match\_all": {}},  
> "filter": {  
> "and": [  
> {"term": {"tags.user": "kuwar"}},  
> {"term": {"tags.tag": "master blaster"}}  
> ]  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> the query does not give me the result. however if i change tags.tag to "master" instead of "master blaster" the output comes.
> 
> how to solve this problem ?  
> thanks in advance.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![benjamin\_leviant](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjamin\_leviant](https://discuss.elastic.co/u/benjamin_leviant)\
**Post date:** [March 13, 2013, 9:40am UTC](https://discuss.elastic.co/t/nested-query/11125/5 "2013-03-13T09:40:05Z")

</div>

Hello,

The query with "master blaster" does not match with your documents because,  
your field tags.tag is by default tokenised during the indexation but the  
term filter does not analyze its searched term.

You should configure in your mapping, the field tags.tag with the option  
"index":"not\_analyzed".

I hope it can help you.

Benjamin

On Wed, Mar 13, 2013 at 7:26 AM, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com) wrote:

> Hi
> 
> I've created the following Index and mapping :
> 
> Index : curl -XPUT '[http://10.118.192.165:9200/matches](http://10.118.192.165:9200/matches)'
> 
> Mapping:
> 
> curl -XPUT '[http://10.118.192.165:9200/matches/tags\_nested/\_mapping](http://10.118.192.165:9200/matches/tags_nested/_mapping)' -d '  
> {  
> "tags\_nested":{  
> "properties":{  
> "name": {  
> "type": "string"  
> },  
> "total\_runs": {  
> "type": "integer"  
> },  
> "matches\_played": {  
> "type": "string"  
> },  
> "highest\_score": {  
> "type": "string"  
> },  
> "tags": {  
> "type" : "nested"  
> }
> 
> }  
> }  
> }'
> 
> the data inserted is :
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/1](http://10.118.192.165:9200/matches/tags_nested/1)' -d'  
> {  
> "name" : "sachin",  
> "total\_runs" : "500",  
> "matches\_played" : "4",  
> "highest\_score" : "200\*",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "master blaster"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "world class"  
> },  
> {  
> "user" : "mayank",  
> "tag" : "200 not out"  
> }  
> ]
> 
> }'
> 
> curl XPUT '[http://10.118.192.165:9200/matches/tags\_nested/2](http://10.118.192.165:9200/matches/tags_nested/2)' -d'  
> {  
> "name" : "hayden",  
> "total\_runs" : "300",  
> "matches\_played" : "4",  
> "highest\_score" : "110",  
> "tags" :[  
> {  
> "user" : "kuwar",  
> "tag" : "good knock"  
> },  
> {  
> "user" : "nipun",  
> "tag" : "match winning"  
> }  
> ]
> 
> }'
> 
> I want to find the record where the user "kuwar" has a tag "master  
> blaster" . Have designed the following query but it does not give me the  
> result.
> 
> curl -XGET '  
> [http://10.118.192.165:9200/matches/tags\_nested/\_search?pretty=true](http://10.118.192.165:9200/matches/tags_nested/_search?pretty=true)' -d '  
> {  
> "query": {  
> "nested": {  
> "path": "tags",  
> "query":{  
> "filtered": {  
> "query": { "match\_all": {}},  
> "filter": {  
> "and": [  
> {"term": {"tags.user": "kuwar"}},  
> {"term": {"tags.tag": "master blaster"}}  
> ]  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> the query does not give me the result. however if i change tags.tag to  
> "master" instead of "master blaster" the output comes.
> 
> how to solve this problem ?  
> thanks in advance.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:46am UTC](https://discuss.elastic.co/t/nested-query/11125/6 "2017-07-06T02:46:50Z")

</div>


