# Nested query in elastic search

**URL:** <https://discuss.elastic.co/t/nested-query-in-elastic-search/20958>\
**Category:** Elasticsearch\
**Created:** [November 26, 2014, 6:35pm UTC](https://discuss.elastic.co/t/nested-query-in-elastic-search/20958 "2014-11-26T18:35:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vijay\_Tiwary](https://avatars.discourse-cdn.com/v4/letter/v/3da27b/32.png) [@Vijay\_Tiwary](https://discuss.elastic.co/u/Vijay_Tiwary)\
**Post date:** [November 26, 2014, 6:35pm UTC](https://discuss.elastic.co/t/nested-query-in-elastic-search/20958/1 "2014-11-26T18:35:15Z")

</div>

I storing documents as shown below:  
"keywords": [  
{  
"name": "#nfl",  
"type": "general hashtag word",  
"postag": "nnp"  
},  
{  
"name": "#bill",  
"type": "general hashtag word",  
"postag": "nnp"  
},  
{  
"name": "video",  
"type": "general word noun "keywords" is nested  
fieldchunk",  
"postag": "noun"  
}  
]  
_"keywords" is nested field_  
Now how can I query so that the document returned should only have those  
objects from the "keywords" field which has "type" as "general hashtag word"

I am trying the following query . However with this query all the objects  
are returned if any one of the object in the keywords field have type as  
"general hashtag word"  
{  
"size":20,

"query" : {  
"filtered" : {  
"filter" : {  
"nested" : {  
"path" : "keywords",  
"filter" : {

```
           "term" : {"type" : "general hashtag word" }
        }
   
            
        }
    }
  }

```

}  
}

--  
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/cc947f27-8df8-4973-a254-0ddb9163358b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc947f27-8df8-4973-a254-0ddb9163358b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Vijay\_Tiwary](https://avatars.discourse-cdn.com/v4/letter/v/3da27b/32.png) [@Vijay\_Tiwary](https://discuss.elastic.co/u/Vijay_Tiwary)\
**Post date:** [November 27, 2014, 6:18am UTC](https://discuss.elastic.co/t/nested-query-in-elastic-search/20958/2 "2014-11-27T06:18:21Z")

</div>

Actually i want to aggreagate by "name" of keywords field. However I only  
want those keywords which are of type "general hashtag word". I am using  
this query  
{  
"from" : 0,  
"size" : 0,  
"query" : {  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"bool" : {  
"must" : [ {  
"term" : {  
"brand\_context\_id" : "4"  
}  
},{  
"nested" : {  
"filter" : {  
"term" : {  
"keywords.type" : "general hashtag word"  
}  
},  
"path" : "keywords"  
}  
} ]  
}  
}  
}  
},  
"aggregations" : {  
"keywords" : {  
"nested" : {  
"path" : "keywords"  
},  
"aggregations" : {  
"keywords\_count" : {  
"terms" : {  
"field" : "keywords.name",  
"size": 0  
}  
}  
}  
}  
}  
}  
This agggregate is also giving keywords which are not marked as type  
"general hashtag word". How can I only aggregate by kewords which are  
marked as type "general hashtag word"

On Thursday, November 27, 2014 12:05:15 AM UTC+5:30, Vijay Tiwary wrote:

> I storing documents as shown below:  
> "keywords": [  
> {  
> "name": "#nfl",  
> "type": "general hashtag word",  
> "postag": "nnp"  
> },  
> {  
> "name": "#bill",  
> "type": "general hashtag word",  
> "postag": "nnp"  
> },  
> {  
> "name": "video",  
> "type": "general word noun "keywords" is nested  
> fieldchunk",  
> "postag": "noun"  
> }  
> ]  
> _"keywords" is nested field_  
> Now how can I query so that the document returned should only have those  
> objects from the "keywords" field which has "type" as "general hashtag word"
> 
> I am trying the following query . However with this query all the objects  
> are returned if any one of the object in the keywords field have type as  
> "general hashtag word"  
> {  
> "size":20,
> 
> "query" : {  
> "filtered" : {  
> "filter" : {  
> "nested" : {  
> "path" : "keywords",  
> "filter" : {
> 
> ```
> "term" : {"type" : "general hashtag word" }
> }
>    
>             
> }
> }
> }
> 
> ```
> 
> }  
> }

--  
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/5aa37181-30ea-4163-8baf-9271ee19fd8a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5aa37181-30ea-4163-8baf-9271ee19fd8a%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, 12:47am UTC](https://discuss.elastic.co/t/nested-query-in-elastic-search/20958/3 "2017-07-06T00:47:21Z")

</div>


