Search on availability dates

We are currently building a booking website using algolia to power our search. I need help figuring out what to do to search for hotels based on dates available in the record. Your help will be much appreciated.

My fields are: id, name, availability

My availability field contains an array of dates, such as: [“2018-01-15”, “2018-01-16”, “2018-01-17”, “2018-01-18”] - This is how my data looks on Algolia index browser.

I am using a daterange picker as in the examples provided by Algolia (instantsearch.js):

    <script type="text/javascript">
  const search = instantsearch({
    appId: '',
    apiKey: '',
    indexName: 'resorts',
    routing: true
  });

  // initialize SearchBox
  search.addWidget(
    instantsearch.widgets.searchBox({
      container: '#SearchKeywords',
      placeholder: 'Search for resorts',
    })
  );
    const datePicker = instantsearch.connectors.connectRange(
    (options, isFirstRendering) => {
      if (!isFirstRendering) return;

      const { refine } = options;

      current_date = new Date();
      end_date = addDays(new Date(), 10);

      new Calendar({
        element: $('#calendar'),
        callback: function() {
          const start = new Date(this.start_date).getTime();
          const end = new Date(this.end_date).getTime();

          refine([start, end]);
        },
        // Some good parameters based on our dataset:
        start_date: current_date,
        end_date: end_date,
        earliest_date: new Date(),
        latest_date: new Date('01/01/2020'),
        presets: false
      });
    }
  );

  search.addWidget(
    datePicker({
      attributeName: 'availability',
    })
  );

  search.start();

Now my question is, how can I use filters (numeric?) to make sure that each date in the date range is in the availability array? If any date is missing, the result should not be shown.

Hi there,

  1. We have discussed your use case with our search team, thanks for all the information you provided us. At this point, I need to say that in your particular use case Algolia might not be a perfect fit and you might end up doing a lot of extra work compared to trying to implement this using a regular database. Not only may this require a lot of work and implementation advice from Algolia (that is only available at the business plan) but it might not even work the way you want in the end. The booking use cases at Algolia have been very challenging to handle for us and is a blind spot of our product we are trying to address. So I would encourage you at exploring other possibilities along with Algolia and see what’s the best for you.

  2. If you still want to pursue in the Algolia direction, here’s how you could format your data:

[{
  objectID: '120',
  hotelName: 'Super Hotel',
  availabilityStart: timestampLastIndexing,
  availabilityEnd: timestampOneYearInTheFutureFromLastIndexing
}]

This record would be the state when you start your index, without any booking, it means there would be one record per hotel and availabilityStart would be the time of last indexing, availabilityEnd would be one year in the future.

Then if someone makes a booking for July 12 for this hotel (120, Super Hotel), you would have to update your index this way:

[{
  objectID: '120',
  hotelName: 'Super Hotel',
  availabilityStart: timestampLastIndexing,
  availabilityEnd: timestampJuly11EndOfTheDay
}, {
  objectID: '120',
  hotelName: 'Super Hotel',
  availabilityStart: timestampJuly13StartOfTheDay,
  availabilityEnd: timestampOneYearInTheFutureFromJuly13StartOfTheDay
}]

You will have to update your index everytime a booking is made and merge adjacent days while also making sure that someone in the reservation process does not have a bad surprise when the about to complete if someone else finished before (the classic booking design issue).

On your frontend, you would have to make requests this way:

index.search({
  query: ..,
  filters: 'userSelectedTimestamp >= availabilityStart AND userSelectedTimestamp <= availabilityEnd'
})

As you can see, Algolia and booking use cases are very complex so I would advise you to really dig into other solutions with your colleagues if feasible.

Let me know how it goes or if you have more questions,
Happy Coding!

Hi there Jason, thanks so much for your prompt and detailed reply as well as for the possible solutions you provided. In the end I decided to go with the normal database solution and I believe this will be fine for my use case as you mentioned.

Thanks again,
Louis

1 Like

Glad to hear you were able to find a solution to work for your use case.
Happy Coding!

Bit disheartening to hear the responses to this query from the Algolia team.

We have invested a lot into Algolia and the plan was to introduce availability search into our website in the future.

I’m still hopeful it can be done in a non-complex way and without degrading performance too much.

To @alloutgear - please see a topic I started a couple of years ago on this. There are a couple of options on how to do it there:

Just noticed one of the showcase demos on the Instantsearch website is “Tourism”

The form shows a date selector but these are disabled and have no influence on the results displayed beneath. A little bit misleading to demonstrate it as one of the 3 use cases for Instantsearch and yet not only is it not working in the demo, but Algolia team members here are suggesting people use alternate solutions rather than Algolia to deliver this :frowning: