質問

Date Query against single date field is possible using solr. But I have an eligible start date(say 2001/01/01) and end date(say 2012/01/01) in my data. Now I have to query date range from 2006/01/01 to 2013/01/01. This should give me data from 2006/01/01 (search start date > eligibility date) to 2012/01/01 (search end date falls outside eligibility end date).

Querying on 2 different fields does not always give the right result. Is there any way to query such kind of date range vs date range query ?

役に立ちましたか?

解決

you can easily query Solr to get the relevant docs returned, but, what I don't think you can easily get that result like that.

You might try to use FunctionQueries to build this as a returned field, look at the ms section, but I am not sure you could achieve it, if you do, it will be with a very convoluted function...

Of course you can always calculate the overlapping range yourself in the client side.

他のヒント

I don't know solr specifically, but in most query languages I've used you'd use something like the following:

SELECT *
FROM table
WHERE   @EligStartDate <= [EligEndDate]
    AND @EligEndDate >= [EligStartDate]

(@EligStartDate is your input start date and @EligEndDate is your input end date)

This would find all records where there is some overlap between the two eligibility date ranges. Now, if you want to trim the result to what you have in the database, you can use something like a MIN/MAX function:

/* Use the latest start date and the earliest end date */
SELECT MAX( @EligStartDate, [EligStartDate] )
     , MIN( @EligEndDate, [EligEndDate] )
...

Check out Chris Hostetter's slide deck, Spatial Search Tricks for People Who Don't Have Spatial Data

There is also doc in the Solr wiki: SpatialForTimeDurations

The take-away is that you can represent the time in an X,Y coordinate system and Solr will tell you if the "boxes" overlap. It's not super intuitive, but it's a cool trick.

This is what specifically what Solr DateRangeField was designed to support, but you might have to upgrade to at least Solr 1.5 to get it. Both ends of the range are indexed into a single value in the field. Multiple ranges (values) are supported.

Solr's DateRangeField supports the same point in time date syntax described above (with date math described below) and more to express date ranges. One class of examples is truncated dates, which represent the entire date span to the precision indicated. The other class uses the range syntax ([ TO ]). Here are some examples:

2000-11    – The entire month of November, 2000.
2000-11T13 – Likewise but for an hour of the day (1300 to before 1400, i.e. 1pm to 2pm).
-0009      – The year 10 BC.  A 0 in the year position is 0 AD, and is also considered 1 BC. 
[2000-11-01 TO 2014-12-01] – The specified date range at a day resolution.
[2014 TO 2014-12-01]       – From the start of 2014 till the end of the first day of December.
[* TO 2014-12-01]          – From the earliest representable time thru till the end of the day on 2014-12-01.

As an additional bonus, the new field type appears to be faster for common date queries than its predecessors, too.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top