# Multiple AND on nested objects not working

**URL:** <https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644>\
**Category:** Elasticsearch\
**Created:** [September 17, 2013, 11:51am UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644 "2013-09-17T11:51:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joa/32/1807_2.png) [@joa](https://discuss.elastic.co/u/joa)\
**Post date:** [September 17, 2013, 11:51am UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/1 "2013-09-17T11:51:46Z")

</div>

Hi, my I am using the following mapping for a flexible metadata structure  
(k = key/name, i = integer values, s = string values):

{  
"my\_index" : {  
"my\_type" : {  
"properties" : {  
"metadata" : {  
"type" : "nested",  
"properties" : {  
"i" : {  
"type" : "integer"  
},  
"k" : {  
"type" : "string"  
},  
"s" : {  
"type" : "string"  
}  
}  
}  
}  
}  
}  
}

Here is how sample data might look like:  
{  
"metadata": [  
{  
"k": "autor",  
"s": "Arlene Franklin"  
},  
{  
"k": "year1",  
"i": 2001  
},  
{  
"k": "year2",  
"i": 2013  
}  
]  
}

I am now trying to do queries like: All docs where year1 greater 1990 AND  
where autor is "Arlene Franklin".  
I've tried the following without success (query works, but I got no hits):  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"nested": {  
"path": "metadata",  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"and": [  
{  
"term": {  
"metadata.k": "year1"  
}  
},  
{  
"range": {  
"metadata.i": {  
"gt": 1990  
}  
}  
}  
]  
},  
{  
"and": [  
{  
"term": {  
"metadata.k": "autor"  
}  
},  
{  
"term": {  
"metadata.s": "Arlene Franklin"  
}  
}  
]  
}  
]  
}  
}  
}  
}  
}  
}  
}  
}

Is my mapping working at all for queries like this? Thanks!

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 17, 2013, 1:43pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/2 "2013-09-17T13:43:49Z")

</div>

The use of the nested seems correct. Can you try to change the `term`  
filter into a `match` query that is wrapped a `query` filter?

The term filter (and query) doesn't take query time text analysis into  
account, the match query does. ("Arlene Franklin" is handled at a single  
token while it should be handled as ["arlene", "franklin"], so is also how  
a string field is indexed as default).

On 17 September 2013 13:51, joa [joafeldmann@gmail.com](mailto:joafeldmann@gmail.com) wrote:

> Hi, my I am using the following mapping for a flexible metadata structure  
> (k = key/name, i = integer values, s = string values):
> 
> {
> 
> "my\_index" : {  
> "my\_type" : {
> 
> ```
> "properties" : {
> "metadata" : {
> 
> "type" : "nested",
> 
> "properties" : {
> "i" : {
> 
> "type" : "integer"
> },
> 
> "k" : {
> "type" : "string"
> 
> },
> "s" : {
> 
> "type" : "string"
> }
> 
> }
> }
> }
> }
> 
> ```
> 
> }  
> }
> 
> Here is how sample data might look like:  
> {  
> "metadata": [  
> {  
> "k": "autor",  
> "s": "Arlene Franklin"  
> },  
> {  
> "k": "year1",  
> "i": 2001  
> },  
> {  
> "k": "year2",  
> "i": 2013  
> }  
> ]  
> }
> 
> I am now trying to do queries like: All docs where year1 greater 1990 AND  
> where autor is "Arlene Franklin".  
> I've tried the following without success (query works, but I got no hits):  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "nested": {  
> "path": "metadata",  
> "query": {  
> "filtered": {  
> "filter": {  
> "and": [  
> {  
> "and": [  
> {  
> "term": {  
> "metadata.k": "year1"  
> }  
> },  
> {  
> "range": {  
> "metadata.i": {  
> "gt": 1990  
> }  
> }  
> }  
> ]  
> },  
> {  
> "and": [  
> {  
> "term": {  
> "metadata.k": "autor"  
> }  
> },  
> {  
> "term": {  
> "metadata.s": "Arlene Franklin"  
> }  
> }  
> ]  
> }  
> ]  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Is my mapping working at all for queries like this? Thanks!
> 
> --  
> 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).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![joa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joa/32/1807_2.png) [@joa](https://discuss.elastic.co/u/joa)\
**Post date:** [September 17, 2013, 1:48pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/3 "2013-09-17T13:48:28Z")

</div>

Hi Martijn, thanks. I'll try that. But even working with two range queries  
returns no hits:

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"nested": {  
"path": "metadata",  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"and": [  
{  
"term": {  
"metadata.k": "year1"  
}  
},  
{  
"range": {  
"metadata.i": {  
"gt": 1990  
}  
}  
}  
]  
},  
{  
"and": [  
{  
"term": {  
"metadata.k": "year2"  
}  
},  
{  
"range": {  
"metadata.i": {  
"lt": 2020  
}  
}  
}  
]  
}  
]  
}  
}  
}  
}  
}  
}  
}  
}

--  
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:** ![joa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joa/32/1807_2.png) [@joa](https://discuss.elastic.co/u/joa)\
**Post date:** [September 17, 2013, 1:59pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/4 "2013-09-17T13:59:50Z")

</div>

Should I replace all term filters or only the one used by the autor ("metadata.s":  
"Arlene Franklin") ? If you mean all term filters, it the following as you  
suggested?

...  
"and": [  
{  
"query": {  
"match": {  
"metadata.k": "year1"  
}  
}  
},  
{  
"range": {  
"metadata.i": {  
"gt": 1990  
}  
}  
}  
]...

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 17, 2013, 2:10pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/5 "2013-09-17T14:10:14Z")

</div>

Only change the term filter with: ("metadata.s": "Arlene Franklin")

Also I think you need to change to top level `and` filter in the second  
filtered query in a `or` filter, because there is no inner nested object  
with year1 AND year2.

On 17 September 2013 15:59, joa [joafeldmann@gmail.com](mailto:joafeldmann@gmail.com) wrote:

> Should I replace all term filters or only the one used by the autor ("metadata.s":  
> "Arlene Franklin") ? If you mean all term filters, it the following as  
> you suggested?
> 
> ...  
> "and": [  
> {  
> "query": {  
> "match": {  
> "metadata.k": "year1"  
> }  
> }  
> },  
> {  
> "range": {  
> "metadata.i": {  
> "gt": 1990  
> }  
> }  
> }  
> ]...

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![joa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joa/32/1807_2.png) [@joa](https://discuss.elastic.co/u/joa)\
**Post date:** [September 17, 2013, 2:36pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/6 "2013-09-17T14:36:58Z")

</div>

I am not sure if I get you right, but changing any of the "and"s to an "or"  
will lead to different results: I really want to get only documents, where  
e.g. year1=2004 AND year2=2006. I think using an or filter would change my  
results?  
As this is not a productive environment I can also change my mapping, if  
you think the current is not optimal for this. Thanks again.

--  
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:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [September 17, 2013, 4:02pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/7 "2013-09-17T16:02:10Z")

</div>

Hi

This query is right?  
I try to send sevral pattern query.  
Sorry, I don't know different between this query and your query...

{  
"explain": true,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and": [  
{  
"nested": {  
"path": "metadata",  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"term": {  
"metadata.k": "year1"  
}  
},  
{  
"range": {  
"metadata.i": {  
"gt": 1990  
}  
}  
}  
]  
}  
}  
}  
}  
},  
{  
"nested": {  
"path": "metadata",  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"term": {  
"metadata.k": "autor"  
}  
},  
{  
"query": {  
"match": {  
"metadata.s": "Arlene Franklin"  
}  
}  
}  
]  
}  
}  
}  
}  
}  
]  
}  
}  
}  
}

2013/9/17 joa [joafeldmann@gmail.com](mailto:joafeldmann@gmail.com)

> I am not sure if I get you right, but changing any of the "and"s to an  
> "or" will lead to different results: I really want to get only documents,  
> where e.g. year1=2004 AND year2=2006. I think using an or filter would  
> change my results?  
> As this is not a productive environment I can also change my mapping, if  
> you think the current is not optimal for this. Thanks again.
> 
> --  
> 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).

## --

Jun Ohtani  
blog : [http://blog.johtani.info](http://blog.johtani.info)

--  
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:** ![joa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joa/32/1807_2.png) [@joa](https://discuss.elastic.co/u/joa)\
**Post date:** [September 17, 2013, 4:22pm UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/8 "2013-09-17T16:22:44Z")

</div>

Hi Jun,

your query works, thanks! Including nested": { "path": "metadata" } mutiple  
times like you did solved it. I wonder if there a are more elegant ways to  
write this?

--  
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:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [September 18, 2013, 3:11am UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/9 "2013-09-18T03:11:30Z")

</div>

Hi

> I wonder if there a are more elegant ways to write this?

Sorry, I don't know elegant way.  
Maybe "path" field is need to each filter, because "nested" filter requires "path" field.

* * *

Jun Ohtani  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

On 2013/09/18, at 1:22, joa [joafeldmann@gmail.com](mailto:joafeldmann@gmail.com) wrote:

> Hi Jun,
> 
> your query works, thanks! Including nested": { "path": "metadata" } mutiple times like you did solved it. I wonder if there a are more elegant ways to write this?
> 
> --  
> 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:15am UTC](https://discuss.elastic.co/t/multiple-and-on-nested-objects-not-working/13644/10 "2017-07-06T02:15:51Z")

</div>


