Search for available dates?

Hi ,

We want to make a search engine for available rooms based on date user input. I have searched and come up with this solution.

PUT /rooms
{
  "mappings": {
    "room": {
      "properties": {
        "room_num": {
          "type": "integer"
        }
      }
    },
    "availability": {
      "_parent": {
        "type": "room"
      },
      "properties": {
        "date": {
          "type": "date",
          "format": "date"
        },
        "available": {
          "type": "boolean"
        }
      }
    }
  }
}

POST /rooms/_bulk
{"_index": { "_type": "room", "_id": 233}}
{"room_num": 233}
{"_index": { "_type": "availability", "_id": "20160701", "_parent": 233}}
{"date": "2016-07-01"}
{"_index": { "_type": "availability", "_id": "20160702", "_parent": 233}}
{"date": "2016-07-02"}

POST /rooms/room/_search
{
  "query": {
    "has_child": {
      "type": "availability",
      "query": {
        "term": {
          "date": "2016-07-01"
        }
      }
    }
  }
}

POST /rooms/room/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 3,
      "should": [
        {
          "has_child": {
            "type": "availability",
            "query": {
              "term": {
                "date": "2016-07-01"
              }
            }
          }
        },
        {
          "has_child": {
            "type": "availability",
            "query": {
              "term": {
                "date": "2016-07-02"
              }
            }
          }
        },
        {
          "has_child": {
            "type": "availability",
            "query": {
              "term": {
                "date": "2016-07-03"
              }
            }
          }
        }
      ]
    }
  }
}

I am just new and start learning ES. Is the above setup the correct approach in making a search engine for available rooms ?

ciao..remco

I see you've found my solution on SO, but it is not valid anymore as of ES 6.x onwards since it will soon not be allowed anymore to create an index with several mapping types. You can modify that solution by using the new join field, though.

So it'd look something like this:

PUT /rooms
{
  "mappings": {
    "doc": {
      "properties": {
        "room_num": {
          "type": "integer"
        },
         "room_availability": { 
          "type": "join",
          "relations": {
            "room": "availability" 
          }
        },
        "date": {
          "type": "date",
          "format": "date"
        }
      }
    }
  }
}

Add some docs:

PUT rooms/doc/233
{
  "room_num": 233,
  "room_availability": "room"
}

PUT rooms/doc/233?routing=233
{
  "date": "2016-07-01",
  "room_availability": {
    "name": "availability",
    "parent": 233
  }
}

Hi Val,

Thanks! We have also diffent types of properties (villas, apartmetns, b&B) and each has own specifications like "guests", "bathrooms", "bedrooms" ect. Where must i put this in the mapping ?

thanks again . ciao..remco

Each should probably have its own index with appropriate mapping.

Also note that you can only have a single join field per index/mapping.

i was thinking about this. Is this not the right way?

PUT /rooms
{
  "mappings": {
    "room": {
      "properties": {
      	"beds": {
          "type": "integer"
        },
        "baths": {
          "type": "integer"
        },
        "room_num": {
          "type": "integer"
        }
      }
    },
    "availability": {
      "_parent": {
        "type": "room"
      },
      "properties": {
        "date": {
          "type": "date",
          "format": "date"
        },
        "available": {
          "type": "boolean"
        }
      }
    }
  }
}

No because you're including two mapping types into the same index. See the example I gave you above.

thanks. i notice that i must understand ES more to make the correct mapping. :slight_smile:

Go through the links I gave you above and I'm pretty confident that you'll nail it. Feel free to come back with questions while you're creating those mappings.

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