# Range queries with no upper bound (Tire)

**URL:** <https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064>\
**Category:** Elasticsearch\
**Created:** [March 20, 2012, 12:47am UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064 "2012-03-20T00:47:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robin\_Boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_boutros/32/1727_2.png) [@Robin\_Boutros](https://discuss.elastic.co/u/Robin_Boutros)\
**Post date:** [March 20, 2012, 12:47am UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/1 "2012-03-20T00:47:21Z")

</div>

Hey,

Sorry if it's not directly an elasticsearch problem...

I'm tring to search for upcoming events.  
An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
optional.

Upcoming events are defined like that:

ends\_at is present AND ends\_at \> now  
OR  
ends\_at is missing AND starts\_at \> now

I'm using Tire, the best I found is:

should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }  
should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }

I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html#Range](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹

Thanks!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 20, 2012, 10:51am UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/2 "2012-03-20T10:51:17Z")

</div>

Use \*, it should work.

On Tue, Mar 20, 2012 at 2:47 AM, Robin Boutros [niuage@gmail.com](mailto:niuage@gmail.com) wrote:

> Hey,
> 
> Sorry if it's not directly an elasticsearch problem...
> 
> I'm tring to search for upcoming events.  
> An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
> optional.
> 
> Upcoming events are defined like that:
> 
> ends\_at is present AND ends\_at \> now  
> OR  
> ends\_at is missing AND starts\_at \> now
> 
> I'm using Tire, the best I found is:
> 
> should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }
> 
> should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }
> 
> I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![Robin\_Boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_boutros/32/1727_2.png) [@Robin\_Boutros](https://discuss.elastic.co/u/Robin_Boutros)\
**Post date:** [March 20, 2012, 2:44pm UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/3 "2012-03-20T14:44:48Z")

</div>

Thanks! but it doesn't seem to work.

Here is the ES query generated by Tire: [Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/dJPxX/1/)

And the error:

nested: ParseException[Cannot parse '_exists_:to AND to:[2012-03-20 10:43:15 -0400 TO \*]': Encountered " \<RANGEIN\_GOOP\> "-0400 "" at line 1, column 40.\nWas expecting:\n "]" ...\n ];

On Tuesday, March 20, 2012 6:51:17 AM UTC-4, kimchy wrote:

> Use \*, it should work.
> 
> On Tue, Mar 20, 2012 at 2:47 AM, Robin Boutros \<\> wrote:
> 
> > Hey,
> > 
> > Sorry if it's not directly an elasticsearch problem...
> > 
> > I'm tring to search for upcoming events.  
> > An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
> > optional.
> > 
> > Upcoming events are defined like that:
> > 
> > ends\_at is present AND ends\_at \> now  
> > OR  
> > ends\_at is missing AND starts\_at \> now
> > 
> > I'm using Tire, the best I found is:
> > 
> > should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }
> > 
> > should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(:lucene)} TO #{1.year.since.to\_s(:lucene)}]" }
> > 
> > I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([Apache Lucene - Query Parser Syntax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹
> > 
> > Thanks!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 20, 2012, 8:16pm UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/4 "2012-03-20T20:16:52Z")

</div>

I think the problem comes from the timezone in the query\_string, where it  
has an extra space. Try and provide it without the space? Also, you can  
convert the query\_string query into its counterpart query with range query  
in it.

On Tue, Mar 20, 2012 at 4:44 PM, Robin Boutros [niuage@gmail.com](mailto:niuage@gmail.com) wrote:

> Thanks! but it doesn't seem to work.
> 
> Here is the ES query generated by Tire: [Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/dJPxX/1/)
> 
> And the error:
> 
> nested: ParseException[Cannot parse '_exists_:to AND to:[2012-03-20 10:43:15 -0400 TO \*]': Encountered " \<RANGEIN\_GOOP\> "-0400 "" at line 1, column 40.\nWas expecting:\n "]" ...\n ];
> 
> On Tuesday, March 20, 2012 6:51:17 AM UTC-4, kimchy wrote:
> 
> > Use \*, it should work.
> > 
> > On Tue, Mar 20, 2012 at 2:47 AM, Robin Boutros \<\> wrote:
> > 
> > > Hey,
> > > 
> > > Sorry if it's not directly an elasticsearch problem...
> > > 
> > > I'm tring to search for upcoming events.  
> > > An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
> > > optional.
> > > 
> > > Upcoming events are defined like that:
> > > 
> > > ends\_at is present AND ends\_at \> now  
> > > OR  
> > > ends\_at is missing AND starts\_at \> now
> > > 
> > > I'm using Tire, the best I found is:
> > > 
> > > should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(:**lucene)} TO #{1.year.since.to\_s(:lucene)}]**" }
> > > 
> > > should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(:**lucene)} TO #{1.year.since.to\_s(:lucene)}]**" }
> > > 
> > > I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([http://lucene.apache.org/\*\*core/old\_versioned\_docs/\*\*versions/3\_0\_0/\*\*queryparsersyntax.html#Range](http://lucene.apache.org/ **core/old_versioned_docs/** versions/3_0_0/**queryparsersyntax.html#Range) [http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html#Range](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹
> > > 
> > > Thanks!

---

<div class="post-metadata">

**Author:** ![Robin\_Boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_boutros/32/1727_2.png) [@Robin\_Boutros](https://discuss.elastic.co/u/Robin_Boutros)\
**Post date:** [March 20, 2012, 8:37pm UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/5 "2012-03-20T20:37:45Z")

</div>

You're right! My date was not formatted correctly, and I didn't pay  
attention to the error enough.  
Basically, I commented out this line at some  
point: Time::DATE\_FORMATS.update :lucene =\> "%Y-%m-%dT%H:%M:%S%z" and  
forgot to add it back when I tested your solution.

Not sure how to do what you're suggesting with Tire... Is it just nicer or  
is it more performant?

Also, when I was thinking about my problem, I was wondering if it wouldn't  
be better to just index the "ends\_at" date as the "starts\_at" date if the  
ends\_at date is not present... Do you have an opinion on this?

Anyways, thanks for your help, my problem is solved.

On Tuesday, March 20, 2012 4:16:52 PM UTC-4, kimchy wrote:

> I think the problem comes from the timezone in the query\_string, where it  
> has an extra space. Try and provide it without the space? Also, you can  
> convert the query\_string query into its counterpart query with range query  
> in it.
> 
> On Tue, Mar 20, 2012 at 4:44 PM, Robin Boutros \<\> wrote:
> 
> > Thanks! but it doesn't seem to work.
> > 
> > Here is the ES query generated by Tire: [Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/dJPxX/1/)
> > 
> > And the error:
> > 
> > nested: ParseException[Cannot parse '_exists_:to AND to:[2012-03-20 10:43:15 -0400 TO \*]': Encountered " \<RANGEIN\_GOOP\> "-0400 "" at line 1, column 40.\nWas expecting:\n "]" ...\n ];
> > 
> > On Tuesday, March 20, 2012 6:51:17 AM UTC-4, kimchy wrote:
> > 
> > > Use \*, it should work.
> > > 
> > > On Tue, Mar 20, 2012 at 2:47 AM, Robin Boutros \<\> wrote:
> > > 
> > > > Hey,
> > > > 
> > > > Sorry if it's not directly an elasticsearch problem...
> > > > 
> > > > I'm tring to search for upcoming events.  
> > > > An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
> > > > optional.
> > > > 
> > > > Upcoming events are defined like that:
> > > > 
> > > > ends\_at is present AND ends\_at \> now  
> > > > OR  
> > > > ends\_at is missing AND starts\_at \> now
> > > > 
> > > > I'm using Tire, the best I found is:
> > > > 
> > > > should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(:**lucene)} TO #{1.year.since.to\_s(:lucene)}]**" }
> > > > 
> > > > should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(:**lucene)} TO #{1.year.since.to\_s(:lucene)}]**" }
> > > > 
> > > > I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([http://lucene.apache.org/\*\*core/old\_versioned\_docs/\*\*versions/3\_0\_0/\*\*queryparsersyntax.html#Range](http://lucene.apache.org/ **core/old_versioned_docs/** versions/3_0_0/**queryparsersyntax.html#Range) [http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html#Range](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹
> > > > 
> > > > Thanks!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 25, 2012, 10:12am UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/6 "2012-03-25T10:12:49Z")

</div>

On Tue, Mar 20, 2012 at 10:37 PM, Robin Boutros [niuage@gmail.com](mailto:niuage@gmail.com) wrote:

> You're right! My date was not formatted correctly, and I didn't pay  
> attention to the error enough.  
> Basically, I commented out this line at some  
> point: Time::DATE\_FORMATS.update :lucene =\> "%Y-%m-%dT%H:%M:%S%z" and  
> forgot to add it back when I tested your solution.
> 
> Not sure how to do what you're suggesting with Tire... Is it just nicer or  
> is it more performant?

You mean explicit range query? It just usually simpler to build the query  
then constructing the query string syntax.

> Also, when I was thinking about my problem, I was wondering if it wouldn't  
> be better to just index the "ends\_at" date as the "starts\_at" date if the  
> ends\_at date is not present... Do you have an opinion on this?

So you can get away with the missing part? Not sure I follow...

> Anyways, thanks for your help, my problem is solved.
> 
> On Tuesday, March 20, 2012 4:16:52 PM UTC-4, kimchy wrote:
> 
> > I think the problem comes from the timezone in the query\_string, where it  
> > has an extra space. Try and provide it without the space? Also, you can  
> > convert the query\_string query into its counterpart query with range query  
> > in it.
> > 
> > On Tue, Mar 20, 2012 at 4:44 PM, Robin Boutros \<\> wrote:
> > 
> > > Thanks! but it doesn't seem to work.
> > > 
> > > Here is the ES query generated by Tire: [http://jsfiddle.net/\*\*dJPxX/1/](http://jsfiddle.net/**dJPxX/1/)[http://jsfiddle.net/dJPxX/1/](http://jsfiddle.net/dJPxX/1/)
> > > 
> > > And the error:
> > > 
> > > nested: ParseException[Cannot parse '_exists_:to AND to:[2012-03-20 10:43:15 -0400 TO \*]': Encountered " \<RANGEIN\_GOOP\> "-0400 "" at line 1, column 40.\nWas expecting:\n "]" ...\n ];
> > > 
> > > On Tuesday, March 20, 2012 6:51:17 AM UTC-4, kimchy wrote:
> > > 
> > > > Use \*, it should work.
> > > > 
> > > > On Tue, Mar 20, 2012 at 2:47 AM, Robin Boutros \<\> wrote:
> > > > 
> > > > > Hey,
> > > > > 
> > > > > Sorry if it's not directly an elasticsearch problem...
> > > > > 
> > > > > I'm tring to search for upcoming events.  
> > > > > An event has "starts\_at" and "ends\_at" dates. The "ends\_at" date is  
> > > > > optional.
> > > > > 
> > > > > Upcoming events are defined like that:
> > > > > 
> > > > > ends\_at is present AND ends\_at \> now  
> > > > > OR  
> > > > > ends\_at is missing AND starts\_at \> now
> > > > > 
> > > > > I'm using Tire, the best I found is:
> > > > > 
> > > > > should { string "_exists_:ends\_at AND ends\_at:[#{Time.now.to\_s(: **luce** ne)} TO #{1.year.since.to\_s(:lucene)}]\*\*\*\*" }
> > > > > 
> > > > > should { string "_missing_:ends\_at AND starts\_at:[#{Time.now.to\_s(: **lu** cene)} TO #{1.year.since.to\_s(:lucene)}]\*\*\*\*" }
> > > > > 
> > > > > I'm wondering if there is a way, using a query string, to do the range query without having a upper bound (1 year in this example). When I look at the Lucene doc ([http://lucene.apache.org/\*\*core\*\*/old\_versioned\_docs/\*\*versions/\*\*3\_0\_0/\*\*queryparsersyntax.html#\*\*Range](http://lucene.apache.org/ **core** /old_versioned_docs/ **versions/** 3_0_0/ **queryparsersyntax.html#** Range) [http://lucene.apache.org/core/old\_versioned\_docs/versions/3\_0\_0/queryparsersyntax.html#Range](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range) Searches) it does not seem possible ☹
> > > > > 
> > > > > Thanks!

---

<div class="post-metadata">

**Author:** ![Robin\_Boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_boutros/32/1727_2.png) [@Robin\_Boutros](https://discuss.elastic.co/u/Robin_Boutros)\
**Post date:** [March 25, 2012, 3:06pm UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/7 "2012-03-25T15:06:37Z")

</div>

If I index the ends\_at date as the starts\_at (+ X hours maybe) when it's  
missing, then I can always assume the ends\_date is present in my search,  
and I can avoid having to use this "complexe" query. That's what I ended up  
doing and it does what I want.

---

<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, 3:34am UTC](https://discuss.elastic.co/t/range-queries-with-no-upper-bound-tire/7064/8 "2017-07-06T03:34:53Z")

</div>


