3

I'm using Solr as part of a property booking engine- my entries have a multivalued date field which stores the dates that the property is already booked, and thus, not available. I want to be able to query against this, and return entries that have no dates within the window specified.

I'm half way there- but right now Solr appears to be returning the entry if it has even one free date- I want it to only return entries that are totally empty within the range. Example of my entity:

<doc>
    <arr name="DateBlockDates">
        <date>2011-02-25T00:00:00Z</date>
        <date>2011-02-26T00:00:00Z</date>
        <date>2011-02-27T00:00:00Z</date>
    </arr>
</doc>

The query works great in this instance:

-DateBlockDates:[2011-02-25T00:00:00Z TO 2011-02-27T00:00:00Z]

Because the entity does have date blocks for every one of those days. However, when I run:

-DateBlockDates:[2011-02-25T00:00:00Z TO 2011-02-28T00:00:00Z]

The entity gets returned, because it doesn't have an entry for 2011-02-28.

To put my question into old-school SQL, I want to do a "count(DateBlockDates) = 0". Any ideas?

1 Answer 1

3

This is the best solution I've come up with- not using a range for the date query, and instead using:

-DateBlockDates:"2011-02-25T00:00:00.000Z"
AND -DateBlockDates:"2011-02-26T00:00:00.000Z"
AND -DateBlockDates:"2011-02-27T00:00:00.000Z"
AND -DateBlockDates:"2011-02-28T00:00:00.000Z"

It's messy, but it works. Thankfully I'm going to be dealing with relatively small date ranges, so the query won't get too long. But if there is a better solution out there I'd love to hear it.

2
  • Hi Alastair, did you ever find a better solution to this? I'm trying to store multiple datetime values and be able to do a date range search over them.
    – Alex B
    Jan 9, 2012 at 12:26
  • consider transforming date/time to UTC (if not already) then to an integer value representing the seconds since the epoch. Should allow for more empirical date range queries
    – xentek
    Oct 30, 2012 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.