Last active
April 28, 2020 08:21
-
-
Save dedico/4710731 to your computer and use it in GitHub Desktop.
Elasticsearch example - index setup and sample queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup | |
# delete index | |
curl -XDELETE 'http://localhost:9200/hotels/' | |
# create index | |
curl -XPOST 'http://localhost:9200/hotels/' | |
# create mapping | |
curl -XPOST localhost:9200/hotels/regular_hotel/_mapping -d '{ | |
"regular_hotel":{ | |
"properties":{ | |
"rooms": { | |
"type": "object" | |
} | |
} | |
} | |
}' | |
# add some initial data for first hotel | |
curl -XPUT localhost:9200/hotels/regular_hotel/1 -d'{ | |
"name": "Hotel Staromiejski", | |
"city": "Słupsk", | |
"rooms": [ | |
{ | |
"night": "2013-02-15", | |
"allocation": "4" | |
}, | |
{ | |
"night": "2013-02-16", | |
"allocation": "2" | |
}, | |
{ | |
"night": "2013-02-17", | |
"allocation": "0" | |
}, | |
{ | |
"night": "2013-02-18", | |
"allocation": "1" | |
}, | |
{ | |
"night": "2013-02-19", | |
"allocation": "5" | |
}, | |
{ | |
"night": "2013-02-20", | |
"allocation": "1" | |
} | |
] | |
}' | |
# add some initial data for second hotel | |
curl -XPUT localhost:9200/hotels/regular_hotel/2 -d'{ | |
"name": "Hotel Lubicz", | |
"city": "Ustka", | |
"rooms": [ | |
{ | |
"night": "2013-02-15", | |
"allocation": "4" | |
}, | |
{ | |
"night": "2013-02-16", | |
"allocation": "2" | |
}, | |
{ | |
"night": "2013-02-17", | |
"allocation": "3" | |
}, | |
{ | |
"night": "2013-02-18", | |
"allocation": "1" | |
}, | |
{ | |
"night": "2013-02-19", | |
"allocation": "0" | |
}, | |
{ | |
"night": "2013-02-20", | |
"allocation": "5" | |
} | |
] | |
}' | |
# queries | |
# query 1: looking for hotels which have rooms on nights 15 and 16 Feb and with allocation greater than 1 | |
# result: 2 hotels - which is correct in that case... | |
curl -XGET localhost:9200/hotels/regular_hotel/_search -d '{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ "match": { "_all": "hotel" } }, | |
{ "range": { "rooms.night": { "from": "2013-02-15", "to": "2013-02-16" } } }, | |
{ "range": { "rooms.allocation": { "from": "1", "to": "1000" } } } | |
] | |
} | |
} | |
}' | |
# query 2: looking for hotels which have rooms on nights 16 and 17 Feb and with allocation greater than 1 | |
# result: 2 hotels - which is wrong, because hotel 1 has no allocation for 17 Feb... | |
curl -XGET localhost:9200/hotels/regular_hotel/_search -d '{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ "match": { "_all": "hotel" } }, | |
{ "range": { "rooms.night": { "from": "2013-02-16", "to": "2013-02-17" } } }, | |
{ "range": { "rooms.allocation": { "from": "1", "to": "1000" } } } | |
] | |
} | |
} | |
}' | |
# query 3: looking for hotels which have rooms on nights 18, 19 and 20 Feb and with allocation greater than 1 | |
# result: 2 hotels - which is wrong, because hotel 2 has no allocation for 19 Feb... | |
curl -XGET localhost:9200/hotels/regular_hotel/_search -d '{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ "match": { "_all": "hotel" } }, | |
{ "range": { "rooms.night": { "from": "2013-02-18", "to": "2013-02-20" } } }, | |
{ "range": { "rooms.allocation": { "from": "1", "to": "1000" } } } | |
] | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am going through same problem for hotel availablity sequential date search. In my case, structure is pretty similar. There is only difference, in place of allocation i have availability flag having boolean value.
Data:
My understanding is, you are trying to apply allocation range query in result of room night range query, requirement is to club that with room night range of each date.
I just want to know how you achieved the right results. Its very helpful if you share the solution query for this.
Thanks