How to manage bookings with es nested structure


I am working on a booking management system for vehicles, I have a nested structure for bookings inside the vehicle document as shown below. Each booking will include 2 timestamps: fromTime and tillTime which indicates the start and end timestamps for that particular booking. And there are many such vehicle documents.

requirement: Now for an API request, I have to return if any vehicle is available for the requested model_id, fromTime and tillTime (model_id) indicates the type of vehicle.

vehicle document sample:
{ "id": <vehicle_number>, "seater": 5, "model_info": { "id": "toyota_corolla" }, "bookings": [ { "fromTime": 1573488000, "tillTime": 1573495200, "booking_id": "DerS8T9kPLHede9" }, { "fromTime": 1573488000, "tillTime": 1573495200, "booking_id": "KmGS8T9kPLHed6F" } ] }

Also, this is the initial draft of the vehicle document structure, we are ready to adopt a new structure if this structure has flaws. Any feedback is really appreciated.

Hi @_avinashks,

Can you provide more information about why you need to store the bookings in a nested field inside your vehicle document?

I also built a booking application and I store the data separately booking in booking index and mybooked things (vehicle for you) in it's own document and so far I didn't had a need of nested structure. so just by curiosity, about the problem you try to solve.
If you store all the booking logs for a given vehicle your document will be really fat and will need a lot of update...

Hi @gabriel_tessier,

Thanks for the quick reply. This is just my initial thoughts, and I'm still figuring out if the structure works out. The reason for trying to embed the bookings into this was to try and avoid one more query to ES.

So you are suggesting to have a separate index for bookings altogether.
something like this:

// sample for bookings document
DerS8T9kPLHede9 // id of bookings document
    "fromTime" : 1573488000,
    "tillTime" : 1573495200,
    "model_id": "toyota_corolla",
    "vehicle_number": <vehicle_number>

So bookings are separated from vehicles and can be searched using vehicle_number?

Yes something like a logs of bookings, one record for each booking with the details you listed.
You can always use msearch if you are worry about making too much request, but as you can't use join you don't have other choice than making several requests.

Not related with the booking problem but if you setup APM it will really help to figure out the slow page of you application to apply msearch on the page with too much elastic calls.

1 Like

Thanks @gabriel_tessier, will check the APM tool for monitoring.

Back to the problem, I'll have to now fetch all bookings where model_id must match 'toyota_corolla' and what should the range operation on timestamps look like ?

I meant when can I say toyota_corolla is available for requested fromTime and tillTime ? if the result is empty ? Can you share some sample code for this ?

thanks again!

I need to check the code (I'll reply later) I forget how we solve this one but we scratch our head during days and not sure that we go with an easy to reuse solution, more a Frankenstein way still under construction.

Check about range date field it solve a lot of problems with search and filter, forget about details but as I remember it was good.

@gabriel_tessier thank you for your time!

Do post the sample code once you get time :slightly_smiling_face:

Progress so far:

  1. Initial query would be to get all the vehicle_numbers matching the model (toyota_corolla)
  2. next step will be to check if each vehicle is available

Achieving step 2:

// this is query to search bookings document
  "query": {
  "bool": {
  "must": [
    {"match":{"vehicle_id": <vehicle_number>}},
      "bool": {
        "must_not": [
            "bool": {
              "should": [
                { "range": { "fromTime": { "gte": 1573471800 } } },
                { "range": { "tillTime": { "lte": 1573470000 } } }
              "minimum_should_match": 1

the result will contain the document if booking matches, else the result is empty if the vehicle is available

Hi @_avinashks

I checked the code and I maybe mistake with another search, the one you provide do the trick we have similar one, with a couple of other filter but the base is same.
Just one note about the maintenance, that you certainly need. If our resources are not available during a period for maintenance we store it in another index.
When we search we make a search to the maintenance index to know which resource is not available get the ids and then filter out this ids in the main search. The maintenance search ids can be cached as it not change often.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.