views:

148

answers:

6

Say you have a website for users to search for "hotel rooms for rent".

I don't know how the logics of current systems work, which is why I am asking you guys.

I currently have two fields in the form:

  Date available from: //example 2010-04-01 // 1st april 2010
  Date available to:   //example 2010-05-01 // 1st may 2010

Then Submit!

Here comes my problem: My search engine is called "Solr" and it searches like this:

 dateFrom:[$date_from TO *] AND dateTo:[* TO $date_to] 
 // $date_from and $date_to = the date inputted by the user in the html form

the above would search for all matches where $date_from TO infinite AND infinite TO $date_to.

Is this correct? I don't think so personally...

How does this logic work on booking sites?

If you need more input let me know! Thanks

UPDATE:

Btw, whenever users specify a "room for rent" they specify a range, example from 1st of march TO 1st of april.

Also as an example: Say we have a room which is free between 1st april TO 1st may.

+2  A: 

Assuming you store when dates are booked for rather than when they are free, you could, for each day in the date range, find a list of rooms free on that day (by taking the total list of rooms and subtracting rooms that have a booking on that day). Then just take this list and filter it down to rooms that are free on each day in the range.

matt b
Check update please...
Camran
I don't think your update changes my suggested algorithm at all. Or are you looking for someone to specify the exact query to use?
matt b
+1  A: 

The logic given does look correct, although it's an odd way of presenting it.

$date_from TO infinite is equivalent to !someDate.before($date_from). Likewise, $infinite TO date_to == !someDate.after($date_to).

So, you're looking for each room's bookings to make sure there are none between 1 April and 1 May, inclusively. If there are bookings between those dates, the room is not available.

Matthew Flynn
A: 

Isn't this the example must teachers use to convince students that they really need to learn how to apply binary (bitwise) operations in applications programming?

like:

for ($day=$firstday; $day<$lastday; $day++) {
  $testbooking=++$testbooking<<1;
  foreach ($rooms as $id=>$room) {
     $avail[$id]=($avail[$id] + (has_booking($id) ? 0 : 1)) <<1;
  }
 }
 foreach ($avail as $id=>$free_pattern) {
    if ($free_pattern & $testbooking == $testbooking) {
       return $id;
    }
 }
 return false;

Scary - but its how some DBMS optimizers wold try and solve it.

C.

symcbean
A: 

Maybe it is easier to have a table with the count of free rooms for each day.

     DATE | FREE
    ======+=====
    10.03 | 10
    11.03 |  7
    12.03 |  6
    ...

To check a request just select the minimum free room count for that period and compare it with the number of requested rooms.

    select min(FREE) from TABLE where DATE >= DATE_FROM and DATE < DATE_TO

(Sorry, I don't know "Solr")
This works since normally there is no need to have a concrete room associated to the reservation.
The rooms will be distributed at check-in.

The table must be updated when a reservation is created/confirmed or canceled.

Carlos Heuberger
A: 

Your logic above is pretty much correct, however the trick is that the database should not store rooms, but spans of free time in a room since each room could have more than one span, as such your main table may contain multiple entries per room

For example to start out suppose you have in the table

 room      start_avail     end_avail
 1         Jan 1           Mar 1
 2         Feb 15          Mar 15

And someone books room 1 for Febuary 2-15, now you hve

 1        Jan 1            Feb 1
 1        Feb 16           Mar 1
 2        Feb 15           Mar 15

When a request comes in, you basically compare it to each freetime-span in the database and find one who starts on or before the requested start date and ends on or after the requested end date. Once booked you remove the timespan from the database and insert the remaining timespans that aren't used (may be 0 1 or 2 depending on how good a fit the booking was.

If you plan to show the user all possible rooms and let them chose one, you're done. However if you have control of which room gets chosen, you will need to consider a heuristic to best use your room resources. For example, a 'best fit' heuristic would always chose the room that leaves the smallest extra times unbooked, so a 1 day booking would prefer a 2 day free span vs taking a single day in the middle of a two week span and cutting it into two freespans. Otherwise, suppose there was a room free March 15 and one all of March. If a booking came in, it could use either room. But next suppose someone came to the site looking for a room for all of March. If the first booking had used the second room, you'd be unable to fufill the request.

bdk