简体   繁体   English

搜索“可用酒店房间”的逻辑需要帮助

[英]Logics of searching for “available hotel rooms” help needed

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: 这是我的问题:我的搜索引擎称为“ Solr”,它的搜索如下:

 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. 上面将搜索where $date_from TO infinite AND infinite TO $date_to.所有匹配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. 顺便说一句,只要用户指定“出租房”,他们就指定范围,例如从3月1日到4月1日。

Also as an example: Say we have a room which is free between 1st april TO 1st may. 又例如:假设我们有一个房间,该房间在4月1日至5月1日之间是免费的。

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. 然后,只需获取此列表并将其过滤到该范围内每天都有空的房间即可。

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). $ date_from至TO无限等于!someDate.before($ date_from)。 Likewise, $infinite TO date_to == !someDate.after($date_to). 同样,$ 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. 因此,您正在寻找每个房间的预订,以确保在4月1日至5月1日(含)之间没有预订。 If there are bookings between those dates, the room is not available. 如果在这些日期之间有预订,该房间不可用。

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. 吓人-但是某些DBMS优化器如何设法解决它。

C. C。

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") (对不起,我不知道“ 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. 创建,确认或取消预订时,必须更新该表。

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 有人预定2月2日至15日的房间1,现在您已经

 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. 预订后,您可以从数据库中删除时间跨度,然后插入未使用的剩余时间跨度(可以为0 1或2,具体取决于预订的适合程度。

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. 例如,“最合适”的启发式方法总是会选择没有预定最少额外时间的房间,因此1天的预订更喜欢2天的免费跨度,而不是在两周的中间花一天的时间来削减分成两个自由跨度。 Otherwise, suppose there was a room free March 15 and one all of March. 否则,假设3月15日有一个空闲的房间,3月中有一个空闲的房间。 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. 但是接下来假设有人来该站点寻找3月整个房间。 If the first booking had used the second room, you'd be unable to fufill the request. 如果第一笔预订使用的是第二间客房,那么您将无法满足要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM