# Range search on nested object

**URL:** <https://discuss.elastic.co/t/range-search-on-nested-object/11227>\
**Category:** Elasticsearch\
**Created:** [March 20, 2013, 5:50pm UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227 "2013-03-20T17:50:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![james\_lewis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/james_lewis/32/2296_2.png) [@james\_lewis](https://discuss.elastic.co/u/james_lewis)\
**Post date:** [March 20, 2013, 5:50pm UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227/1 "2013-03-20T17:50:16Z")

</div>

Hi,

I have an object that looks like this[https://gist.github.com/getsometoast/5206743](https://gist.github.com/getsometoast/5206743)and I'm trying to do a range query on the source -\>date property. At the  
moment, my query looks like this[https://gist.github.com/getsometoast/5206826](https://gist.github.com/getsometoast/5206826)but it's not returning any results. I can confirm that in my index I have  
documents that should be returned by the query - so I'm guessing it's the  
syntax of the query that's wrong. Any ideas?

Regards,  
James

## --

This email, including attachments, is private and confidential. If you have  
received this email in error please notify the sender and delete it from  
your system. Emails are not secure and may contain viruses. No liability  
can be accepted for viruses that might be transferred by this email or any  
attachment. Any unauthorised copying of this message or unauthorised  
distribution and publication of the information contained herein are  
prohibited. 7digital Limited. Registered office: Unit F, Lower Ground  
Floor, 5-25 Scrutton Street, Zetland House London EC2A 4HJ. Registered in  
England and Wales. Registered No. 04843573.

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 21, 2013, 11:58am UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227/2 "2013-03-21T11:58:13Z")

</div>

On Wed, 2013-03-20 at 10:50 -0700, [james.lewis@7digital.com](mailto:james.lewis@7digital.com) wrote:

> Hi,
> 
> I have an object that looks like this and I'm trying to do a range  
> query on the source -\>date property. At the moment, my query looks  
> like this but it's not returning any results. I can confirm that in  
> my index I have documents that should be returned by the query - so  
> I'm guessing it's the syntax of the query that's wrong. Any ideas?

Partial gists seldom provide enough info 🙂

You're using a nested query, but you don't specify whether "source" is  
{type: "nested"} or {type: "object"}

If you haven't set it to "nested" specifically, then it is "object", in  
which case don't use a "nested" query.

Also, we're not sure that your "date" field is actually mapped as a  
date.

clint

--  
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:** ![james\_lewis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/james_lewis/32/2296_2.png) [@james\_lewis](https://discuss.elastic.co/u/james_lewis)\
**Post date:** [March 21, 2013, 12:42pm UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227/3 "2013-03-21T12:42:04Z")

</div>

Interesting... especially the date bit...

I haven't set the mappings up explicitly so "source" will be an object as  
you pointed out. I think I'm using the term nested incorrectly here - I  
chose the nested query not because I thought "date" was part of a nested  
type, but because I thought date was _nested_ within "source" (if that  
makes sense?!)

OK let me rephrase my question. Given that I'm using a default dynamic  
mapping and that I've indexed a bunch of documents as per my  
gist[https://gist.github.com/getsometoast/5206743](https://gist.github.com/getsometoast/5206743)and assuming the  
date were mapped as a date, how would you write a range  
query that matched any documents where the source.date field is between  
from {some-date} and to {some-date}.

Regards,  
James

On Thu, Mar 21, 2013 at 11:58 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> On Wed, 2013-03-20 at 10:50 -0700, [james.lewis@7digital.com](mailto:james.lewis@7digital.com) wrote:
> 
> > Hi,
> > 
> > I have an object that looks like this and I'm trying to do a range  
> > query on the source -\>date property. At the moment, my query looks  
> > like this but it's not returning any results. I can confirm that in  
> > my index I have documents that should be returned by the query - so  
> > I'm guessing it's the syntax of the query that's wrong. Any ideas?
> 
> Partial gists seldom provide enough info 🙂
> 
> You're using a nested query, but you don't specify whether "source" is  
> {type: "nested"} or {type: "object"}
> 
> If you haven't set it to "nested" specifically, then it is "object", in  
> which case don't use a "nested" query.
> 
> Also, we're not sure that your "date" field is actually mapped as a  
> date.
> 
> clint
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/6mfLJZeRrsI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/6mfLJZeRrsI/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, 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).

## --

This email, including attachments, is private and confidential. If you have  
received this email in error please notify the sender and delete it from  
your system. Emails are not secure and may contain viruses. No liability  
can be accepted for viruses that might be transferred by this email or any  
attachment. Any unauthorised copying of this message or unauthorised  
distribution and publication of the information contained herein are  
prohibited. 7digital Limited. Registered office: Unit F, Lower Ground  
Floor, 5-25 Scrutton Street, Zetland House London EC2A 4HJ. Registered in  
England and Wales. Registered No. 04843573.

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 21, 2013, 3:43pm UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227/4 "2013-03-21T15:43:03Z")

</div>

> I haven't set the mappings up explicitly so "source" will be an object  
> as you pointed out. I think I'm using the term nested incorrectly  
> here - I chose the nested query not because I thought "date" was part  
> of a nested type, but because I thought date was nested within  
> "source" (if that makes sense?!)
> 
> OK let me rephrase my question. Given that I'm using a default  
> dynamic mapping and that I've indexed a bunch of documents as per my  
> gist and assuming the date were mapped as a date, how would you write  
> a range query that matched any documents where the source.date field  
> is between from {some-date} and to {some-date}.

Just remove the nested query, leaving:

{  
"query" : {  
"range" : {  
"date" : {  
"to" : "2013-01-02T00:00:00+00:00",  
"from" : "2013-01-01T00:00:00+00:00"  
}  
}  
}  
}

Or, if you want to use a range filter instead, perhaps in combination  
with some other query clause:

{  
"query" : {  
"query" : {  
"match\_all" : {}  
},  
"filtered" : {  
"filter" : {  
"range" : {  
"date" : {  
"to" : "2013-01-02T00:00:00+00:00",  
"from" : "2013-01-01T00:00:00+00:00"  
}  
}  
}  
}  
}  
}

And if you want to find out if your date field has been recognised as a  
date field, just check the mapping:

curl -XGET '[http://127.0.0.1:9200/\_all/\_mapping?pretty=1](http://127.0.0.1:9200/_all/_mapping?pretty=1)'

clint

--  
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:45am UTC](https://discuss.elastic.co/t/range-search-on-nested-object/11227/5 "2017-07-06T02:45:21Z")

</div>


