开发者

solr search for a time within a time range

I'm aware that Solr provides a date field which can store a time instance and then range queries can be performed to match all documents which have that field within a particular range.

My problem is the inverse of this. I need to associate multiple time ranges with docume开发者_如何学JAVAnts and then search for all documents which have the searched time within one of those ranges.

For e.g. I'm indexing outlets and have 3-4 ranges during which the outlet is open. I need to search for all outlets which are open at a particular time instance.

One way of doing this is to index start time and end time of the durations as separate date fields and compare during search like

(time1_1 > t AND time1_2 < t) OR (time2_1 > t AND time2_2 < t) OR (time3_1 > t AND time3_2 < t)

Is there a better/faster/cleaner way to do this?


Your example looks like the entities of your index are the outlet stores and you store their opening and closing times in separate (probably dynamic) fields.

If you ask for a different approach you have to consider to restructure the existing schema or to even create an additional one that uses another entity.

It may seem unusual at first, but if this query is the most essential one to your app then you should consider making the entity of your new index to what you acutally want to query: the particular time instance. I take it, time instance is either a whole day, or maybe half or quarter of a day.

The schema would include fields like the ID, the startdate of the day or half day or whatever you choose, the end of it, and a multivalued list of ids that point to the outlets (stored in your current index (use a multi core setup)).

Even if you choose quarter days to handle morning, afternoon and night hours separately, and even with a preview of several years, data should not explode.

This different schema setup allows you to:

  • do the most important computation during import so that it is easily accessible when querying,
  • simple query that returns in one hit what you seek

You could even forgo Date fields by using a custom way to identify the ranges. I am thinking of creating the identifier from the date and a string that indicates whether it is morning or afternoon etc. This would be used as the unique ID in SOLR. If you can create such an ID from any "time instance" that is queried you'd end up with a simple ID lookup.

e.g. What is open on 2013/03/03 in the morning?

/solr/openhours/select?q=id:2013_03_03_am

returns: Array of outlet ids.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜