Solr, search only by time among datetime fields?

2019-07-19 16:34发布

问题:

I have a result list:

{
    entry: 'name1',
    at: '2013-04-01 13:44:02'
},
{
    entry: 'name2',
    at: '2013-11-11 23:34:11'
},
{
    entry: 'name3',
    at: '2013-05-09 03:58:32'
},

now I would want to write a search filter condition that results hits in '12:00:00' and '14:00:00', so filtering in times, not dates, ignoring the date itself. Something like:

at:[*T12:00:00Z TO *T14:00:00Z]

回答1:

then you need to put the time only information in another field, say a tint, making the time a single integer, 12:44:32 would be 124432 and then you can easily run the range queries you need.

To create that time field you have several options:

  1. add it to the document from the client side itself
  2. use a copyField to copy into the time field, and use a PatternReplaceFilter to remove the date part of the value
  3. add the time field inside a UpdateRequestProcessor

1 or 2 are the easiest by far.



回答2:

I read a post where you could also use regex to match a partial date. But this seems to me an ugly solution.

I think it would be far better, for performance and usability, if you add a new field with the time as number of seconds (or minutes) from the midnight.



标签: datetime solr