3
votes

I am creating a hotel booking system using Elasticsearch and am trying to find a way to return hotels that have a variable number of sequential dates available (for example 7 days) across a range of dates

I am currently storing dates and prices as a child document to the hotel but am unsure how to undertake the search or if it is even possible with my current setup?

Edit: added mappings

Hotel Mapping

{
  "hotel":{
    "properties":{
      "city":{
        "type":"string"
      },
      "hotelid":{
        "type":"long"
      },
      "lat":{
        "type":"double"
      },
      "long":{
        "type":"double"
      },
      "name":{
        "type":"multi_field",
        "fields":{
          "name":{
            "type":"string"
          },
          "name.exact":{
            "type":"string",
            "index":"not_analyzed",
            "omit_norms":true,
            "index_options":"docs",
            "include_in_all":false
          }
        }
      },
      "star":{
        "type":"double"
      }
    }
  }
}

Date Mapping

{
    "dates": {
        "_parent": {
            "type": "hotel"
        },
        "_routing": {
            "required": true
        },
        "properties": {
            "date": {
                "type": "date",
                "format": "dateOptionalTime"
            },
            "price": {
                "type": "double"
            }
        }
    }
}

I am currently using a date range to select the available dates and then a field query to match the city - the other fields will be used later

2
I suggest providing actual docs and concrete examples of what you want to achieve. Would make this question much easier to answer. - DrTech
Can you post at least one document source? - Vova Bilyachat
I've added the mappings for the hotel (parent) and dates (child) documents - sorry about the formatting, I can't seem to indent.. - user3270454
I saw your question from a few minutes ago, swleighton, and couldn't find how to contact you so I decided to comment on this question. Here's an answer: stackoverflow.com/questions/21566334/… If you want something less efficient but is simpler to think of you can run this function on a sorted array: - Jesus is Lord
function findStartOfLowestNConsecutiveElements(array, N) { var low = 0; var i; for (i = 0; i < array.length; i++) { if (i - low === N) { return array.slice(low, low + N); } if (array[i] !== array[i - 1] + 1) { low = i; } } if (i - low === N) { return array.slice(low, low + N); } } - Jesus is Lord

2 Answers

1
votes

I had to to something similar and i ended up storing every day the hotel(room) was booked during indexing. (So a list of [2014-02-15, 2014-02-16, 2014-02-17, etc])

After that is was fairy trivial to write a query to find all hotel rooms that were free during a certain date range.

Still seems like there should be am ore elegant solution, but this ended up working great for me.

0
votes

In my project we have limited period of stays, so I created an object in mapping, and specified available booking dates for each period, like:

  • list of available booking dates for 2 days stay
  • list of available booking dates for 3 days stay
  • ....
  • list of available booking dates for 10 days stay