问题
I am trying to make a search engine like Airbnb (for cars) where when you search, you need to filter the distance
and also check for the availability of the item (if it has been reserved on the dates I am searching for, it should not be a hit). This means that the searched date range needs to check against each future reservation date ranges.
In the MongoDB database, I am currently storing my reservation dates for the items as an array of date objects:
// Future reservations:
[{startDate: d1, endDate: d2}, {startDate: d3, endDate: d4}, ...]
I am not sure how to map this in ElasticSearch and how can I filter out documents whose date ranges are falling in the searched date range.
This query object is all I have got so far:
{
"filtered" : {
"query" : {
"multi_match" : {
"query" : "white toyota",
"fields" : [ "title^2", "description" ]
}
},
"filter" : {
"geo_distance" : {
"distance" : "50km",
"location" : [-122.3050, 37.9174]
}
} // * Add another filter to check start and end dates not fall on future reservation dates
}
};
(Please note that I do not have much experience with ElasticSearch)
EDIT1:
Here is a sample data:
{
title: "Toyota Camry",
description: "Lorem ipsum dolor sit amet, wisi option nam an. Eam ad vulputate expetendis, equidem omnesque efficiantur mei at. Us"
pricePerDay: 90,
location: [-126.3050, 37.9174],
upcomingReservations: [
{startDate: "05/03/2015", endDate: "05/10/2015"},
{startDate: "05/15/2015", endDate: "05/18/2015"}
]
}
I think I will have a separate Reservations colection in Mongo but I will denormalize the dates when transforming data into ElasticSearch (as shown above with upcomingReservations
).
Now, If I search for a "Toyota", for dates "04/30/2015" to "05/02/2015" within the distance, I should get a hit. However, if I select a date range of "05/17/2015" - "05/21/2015", it should not be a hit because one of the future reservations fall in that range.
PS. The data format can be changed. I just thought having future reservations as array of dateRange object would be useful.
来源:https://stackoverflow.com/questions/29820179/elasticsearch-filter-out-multiple-ranges-of-dates