# Search for available dates?

**URL:** <https://discuss.elastic.co/t/search-for-available-dates/127317>\
**Category:** Elasticsearch\
**Created:** [April 9, 2018, 11:52am UTC](https://discuss.elastic.co/t/search-for-available-dates/127317 "2018-04-09T11:52:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [April 9, 2018, 11:52am UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/1 "2018-04-09T11:52:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [April 9, 2018, 12:06pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/2 "2018-04-09T12:06:30Z")

</div>

I see you've found my [solution on SO](https://stackoverflow.com/questions/37824365/how-to-store-date-range-data-in-elastic-search-aws-and-search-for-a-range/37827757#37827757), 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](https://www.elastic.co/blog/removal-of-mapping-types-elasticsearch). You can modify that solution by using the new [`join` field](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/parent-join.html), 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
  }
}
```

---

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [April 9, 2018, 12:14pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/3 "2018-04-09T12:14:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [April 9, 2018, 12:16pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/4 "2018-04-09T12:16:45Z")

</div>

Each should probably have its own index with appropriate mapping.

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

---

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [April 9, 2018, 12:29pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/5 "2018-04-09T12:29:38Z")

</div>

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"
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [April 9, 2018, 12:31pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/6 "2018-04-09T12:31:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [April 9, 2018, 12:44pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/7 "2018-04-09T12:44:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [April 9, 2018, 12:48pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/8 "2018-04-09T12:48:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 7, 2018, 12:49pm UTC](https://discuss.elastic.co/t/search-for-available-dates/127317/9 "2018-05-07T12:49:08Z")

</div>

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