# Relative date range skipping weekends (saturdays and sundays)

**URL:** <https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802>\
**Category:** Elasticsearch\
**Created:** [February 19, 2013, 3:23pm UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802 "2013-02-19T15:23:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SomeUser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/someuser/32/2423_2.png) [@SomeUser](https://discuss.elastic.co/u/SomeUser)\
**Post date:** [February 19, 2013, 3:23pm UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802/1 "2013-02-19T15:23:52Z")

</div>

Hi,

Is it possible to write a query such as "created:[now-2d TO now]" where  
now-2d will skip weekends. So if it is Monday then "now-2d" should also  
yield results for Tuesday and Friday.

Is this possible in some way?

--  
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:** ![Jon\_Shea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jon_shea/32/2263_2.png) [@Jon\_Shea](https://discuss.elastic.co/u/Jon_Shea)\
**Post date:** [February 19, 2013, 3:52pm UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802/2 "2013-02-19T15:52:03Z")

</div>

It definitely is not. If this is something you want to do, then you can add  
a flag to your documents `isCreatedOnWeekend: Boolean` at index time, and  
set it to true if the document was created on a weekend. Then at query time  
you can `and` together your date range with a boolean filter on the  
`isCreatedOnWeekend`.

Also, if you’re going to do range filters, it’s a good practice, if  
possible, to do something like `created: [DateMidnight() - Period.days(2) TO DateMidnight()]`. This way the filter will be the same all day long, and  
it can be effectively cached which can make queries a lot faster. The way  
you have it written, each filter will cover a slightly different time span,  
and you’ll never get a cache hit.

-Jon

--  
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:** [February 19, 2013, 4:42pm UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802/3 "2013-02-19T16:42:53Z")

</div>

An alternative approach would be to use a range filter (for your date  
range) and use it together with a script filter inside a not filter.  
The script filter would contain the following script:  
"doc['created'].date.dayOfWeek == 6 || doc['created'].date.dayOfWeek == 7"

Because the script filter is put in a not filter it would exclude all docs  
with create date in the weekend. I think it is beneficial the cache the not  
filter. You can do this setting the `_cache` field to `true` on the not  
filter.

On 19 February 2013 16:52, Jon Shea [jonshea@foursquare.com](mailto:jonshea@foursquare.com) wrote:

> It definitely is not. If this is something you want to do, then you can  
> add a flag to your documents `isCreatedOnWeekend: Boolean` at index time,  
> and set it to true if the document was created on a weekend. Then at query  
> time you can `and` together your date range with a boolean filter on the  
> `isCreatedOnWeekend`.
> 
> Also, if you’re going to do range filters, it’s a good practice, if  
> possible, to do something like `created: [DateMidnight() - Period.days(2) TO DateMidnight()]`. This way the filter will be the same all day long, and  
> it can be effectively cached which can make queries a lot faster. The way  
> you have it written, each filter will cover a slightly different time span,  
> and you’ll never get a cache hit.
> 
> -Jon
> 
> --  
> 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:** ![SomeUser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/someuser/32/2423_2.png) [@SomeUser](https://discuss.elastic.co/u/SomeUser)\
**Post date:** [February 20, 2013, 10:49am UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802/4 "2013-02-20T10:49:54Z")

</div>

I guess we will have to do it in our application then. I was kinda hoping  
there was some kind of neat option that I had missed.

Thanks for the recommendation on caching best practice though.

On Tue, Feb 19, 2013 at 5:42 PM, Martijn v Groningen \<  
[martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)\> wrote:

> An alternative approach would be to use a range filter (for your date  
> range) and use it together with a script filter inside a not filter.  
> The script filter would contain the following script:  
> "doc['created'].date.dayOfWeek == 6 || doc['created'].date.dayOfWeek == 7"
> 
> Because the script filter is put in a not filter it would exclude all docs  
> with create date in the weekend. I think it is beneficial the cache the not  
> filter. You can do this setting the `_cache` field to `true` on the not  
> filter.
> 
> On 19 February 2013 16:52, Jon Shea [jonshea@foursquare.com](mailto:jonshea@foursquare.com) wrote:
> 
> > It definitely is not. If this is something you want to do, then you can  
> > add a flag to your documents `isCreatedOnWeekend: Boolean` at index time,  
> > and set it to true if the document was created on a weekend. Then at query  
> > time you can `and` together your date range with a boolean filter on the  
> > `isCreatedOnWeekend`.
> > 
> > Also, if you’re going to do range filters, it’s a good practice, if  
> > possible, to do something like `created: [DateMidnight() - Period.days(2) TO DateMidnight()]`. This way the filter will be the same all day long, and  
> > it can be effectively cached which can make queries a lot faster. The way  
> > you have it written, each filter will cover a slightly different time span,  
> > and you’ll never get a cache hit.
> > 
> > -Jon
> > 
> > --  
> > 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).

--  
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:50am UTC](https://discuss.elastic.co/t/relative-date-range-skipping-weekends-saturdays-and-sundays/10802/5 "2017-07-06T02:50:34Z")

</div>


