Solr- multivalued date field, range queries to mat

2019-07-22 21:16发布

问题:

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:

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.



标签: lucene solr