# Nested Query & Filter Query

**URL:** <https://discuss.elastic.co/t/nested-query-filter-query/18696>\
**Category:** Elasticsearch\
**Created:** [July 16, 2014, 10:58am UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696 "2014-07-16T10:58:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 10:58am UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/1 "2014-07-16T10:58:36Z")

</div>

Hi Elastic Searchers,

I am completely new to Elasticsearch but have been progressing well this  
week, I'm just hoping one of you may be able to help nudge me over the  
final hurdle.

I have a complete gist here that populates an index with 2 rock bands and  
their members and runs a successful nested search query to find bands  
containing members with the name Roger (2 results).

> <https://gist.github.com/saiglen/ad8b89836547c7e0ee60>

However, what I also need is to be able to filter the results by other  
attributes, such as the year the band formed, or perhaps part of the band's  
name, 'Pink' for example. 1 or more additional filters.

I have been through the documentation and tried at least a dozen different  
JSON structures to try to get that final search to filter by year or band  
name in addition to the nested query, but without success 😕

Everything I have tried so far has either returned no results at all or has  
just produced an error and failed entirely.

Can someone please help advise.

Cheers for any help.

--  
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/a61b1bed-d4cf-4ad2-8234-fc37d53190c9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a61b1bed-d4cf-4ad2-8234-fc37d53190c9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 16, 2014, 11:39am UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/2 "2014-07-16T11:39:56Z")

</div>

Here is an example based on your data: [https://gist.github.com/dadoonet/64458f7423863d93c49e](https://gist.github.com/dadoonet/64458f7423863d93c49e)

HTH

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

Le 16 juillet 2014 à 12:58:40, Dark Globe (glen@henleydev.co.uk) a écrit:

Hi Elastic Searchers,

I am completely new to Elasticsearch but have been progressing well this week, I'm just hoping one of you may be able to help nudge me over the final hurdle.

I have a complete gist here that populates an index with 2 rock bands and their members and runs a successful nested search query to find bands containing members with the name Roger (2 results).

> <https://gist.github.com/saiglen/ad8b89836547c7e0ee60>

However, what I also need is to be able to filter the results by other attributes, such as the year the band formed, or perhaps part of the band's name, 'Pink' for example. 1 or more additional filters.

I have been through the documentation and tried at least a dozen different JSON structures to try to get that final search to filter by year or band name in addition to the nested query, but without success 😕

Everything I have tried so far has either returned no results at all or has just produced an error and failed entirely.

Can someone please help advise.

## Cheers for any help.

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/a61b1bed-d4cf-4ad2-8234-fc37d53190c9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a61b1bed-d4cf-4ad2-8234-fc37d53190c9%40googlegroups.com).  
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/etPan.53c6648c.6b8b4567.86d7%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.53c6648c.6b8b4567.86d7%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 12:56pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/3 "2014-07-16T12:56:15Z")

</div>

Wow!, Thanks so much David.

--  
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/b93f3370-3b52-456d-a828-1f612f1bb354%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b93f3370-3b52-456d-a828-1f612f1bb354%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 1:17pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/4 "2014-07-16T13:17:36Z")

</div>

Hi again,

Sorry, I've been looking through the  
documentation: [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html)  
to try to work out how to supply multiple filters.

Once again though I am struggling to work out how to build the array as  
I've tried several different approaches but all result in an error,  
including my latest effort which you can see in my original gist which I  
have now updated to include your successful filter and my subsequent  
failure.

Clearly there is something fundamental about how these JSON objects are  
structured that I simply don't understand. What am I missing?

Thanks again for any help.

--  
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/2dc68147-79a5-4281-987d-a81f3bda3839%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2dc68147-79a5-4281-987d-a81f3bda3839%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 2:07pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/5 "2014-07-16T14:07:13Z")

</div>

Just found that replacing "match" for "term" in my second filter condition  
runs without error...but doesn't actually work so no results are returned  
when 1 should be 😕

I'm sure I'm getting closer and I'm sure I shouldn't be finding this such a  
struggle!

Any pointers are welcome and appreciated.

--  
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/f706abeb-461f-49e7-8f15-5cf6bf86cc0f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f706abeb-461f-49e7-8f15-5cf6bf86cc0f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 16, 2014, 3:05pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/6 "2014-07-16T15:05:11Z")

</div>

Match only exists as a query.  
Term exists as a filter and as a query.

Think about queries as full text search. Filters are like SQL. They "only" limit the dataset your are going to query on.

You should read [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/search.html)

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 16 juil. 2014 à 16:07, Dark Globe [glen@henleydev.co.uk](mailto:glen@henleydev.co.uk) a écrit :
> 
> Just found that replacing "match" for "term" in my second filter condition runs without error...but doesn't actually work so no results are returned when 1 should be 😕
> 
> I'm sure I'm getting closer and I'm sure I shouldn't be finding this such a struggle!
> 
> ## Any pointers are welcome and appreciated.
> 
> 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/f706abeb-461f-49e7-8f15-5cf6bf86cc0f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f706abeb-461f-49e7-8f15-5cf6bf86cc0f%40googlegroups.com).  
> 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/D566800F-B75E-4F9C-BD38-C400FC11D81A%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/D566800F-B75E-4F9C-BD38-C400FC11D81A%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 4:09pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/7 "2014-07-16T16:09:50Z")

</div>

Thanks David, I did read that section a couple of days back and again just  
now but I still don't understand why the final search in my gist does not  
return one result.

If I remove { "term": { "title" : "Floyd" }} and run the script, the final  
search returns one result, Pink Floyd.

But I think it should also return that same result with the term filter { "term":  
{ "title" : "Floyd" }} added. But it doesn't.

I still do not understand why this is or what I am doing wrong 😕

--  
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/20ead9ad-bead-497f-b0f1-6786e9fc8ab0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/20ead9ad-bead-497f-b0f1-6786e9fc8ab0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 16, 2014, 4:51pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/8 "2014-07-16T16:51:06Z")

</div>

Floyd needs to be lowercased if you use a Term Filter or query

As it has been indexed in lowercase.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 16 juil. 2014 à 18:09, Dark Globe [glen@henleydev.co.uk](mailto:glen@henleydev.co.uk) a écrit :
> 
> Thanks David, I did read that section a couple of days back and again just now but I still don't understand why the final search in my gist does not return one result.
> 
> If I remove { "term": { "title" : "Floyd" }} and run the script, the final search returns one result, Pink Floyd.
> 
> But I think it should also return that same result with the term filter { "term": { "title" : "Floyd" }} added. But it doesn't.
> 
> ## I still do not understand why this is or what I am doing wrong 😕
> 
> 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/20ead9ad-bead-497f-b0f1-6786e9fc8ab0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/20ead9ad-bead-497f-b0f1-6786e9fc8ab0%40googlegroups.com).  
> 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/2EB2635B-5B67-4B9C-8650-31AF29B43DEC%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/2EB2635B-5B67-4B9C-8650-31AF29B43DEC%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dark\_Globe](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Globe](https://discuss.elastic.co/u/Dark_Globe)\
**Post date:** [July 16, 2014, 5:00pm UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/9 "2014-07-16T17:00:24Z")

</div>

YES!

Thank you so much David. I've been pulling my hair out trying different  
structures when it was that little gotcha all along grrr!

I wil take some comfort, however, in the fact that I had actually got the  
structure right this time.

Thank you so much David for all your help - thanks to you, I will be able  
to demonstrate ElasticSearch as a viable solution tomorrow.

--  
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/295c6fbe-1904-41c0-ab17-b24a47d4694d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/295c6fbe-1904-41c0-ab17-b24a47d4694d%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:15am UTC](https://discuss.elastic.co/t/nested-query-filter-query/18696/10 "2017-07-06T01:15:25Z")

</div>


