Reputation: 2094
I am working with elastic search i want to create a search by range of dates i am thinking of using LocalDate for this only problem is my date in the json data looks like this
{
"departure": {
"city": "\u041c\u0438\u043d\u0441\u043a",
"date": "Aug 27, 2018 12:09:00 AM"
},
I have a about 1000 of such so its not wise for me to change each date in json to yyyy-mm-dd format
my search method is in java as such.
public List<Map<String, Object>> searchDao(LocalDate departureDateFrom, LocalDate departureDateTo,
String countryOrCityOrHotel, int nights, int people, String departureCity) {
List<Map<String, Object>> search = new ArrayList<>();
QueryBuilder range = QueryBuilders.rangeQuery("date")
.from(departureDateFrom)
.to(departureDateTo)
.includeLower(true)
.includeUpper(true);
QueryBuilder cityQuery = QueryBuilders.matchQuery("departure.city", departureCity);
QueryBuilder nightsQuery = QueryBuilders.matchQuery("nights", nights);
QueryBuilder peopleQuery = QueryBuilders.matchQuery("people", people);
QueryBuilder destinationHotel = QueryBuilders.matchQuery("hotel.country", countryOrCityOrHotel);
How do i change my local date format to accepts such dates so its easier to work with data as they are.
Upvotes: 1
Views: 497
Reputation: 184
You need to use a the right mapping in your index template: { "departure.date": { "type": "date" } }
And in your java code to write this:
QueryBuilder range = QueryBuilders.rangeQuery("date")
.from(departureDateFrom)
.to(departureDateTo)
.format("MM dd yyyy hh:mm:ss")
.includeLower(true)
.includeUpper(true);
But in general its always a good idea to use epoch time when storing dates as strings are very fragile and formats tend to change all the time.
Upvotes: 2