Skip to content

Instantly share code, notes, and snippets.

@hivefans
Forked from dedico/nested_example.bash
Last active March 17, 2020 02:04
Show Gist options
  • Select an option

  • Save hivefans/28f7ca5c29d10f9ea428 to your computer and use it in GitHub Desktop.

Select an option

Save hivefans/28f7ca5c29d10f9ea428 to your computer and use it in GitHub Desktop.
|-|{"files":{"nested_example.bash":{"env":"plain"}},"tag":"bigdata"}
# setup
# delete index
curl -XDELETE 'http://localhost:9200/hotels/'
# create index
curl -XPOST 'http://localhost:9200/hotels/'
# create mapping
curl -XPOST localhost:9200/hotels/nested_hotel/_mapping -d '{
"nested_hotel":{
"properties":{
"rooms": {
"type": "nested"
}
}
}
}'
curl -XPUT localhost:9200/hotels/nested_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/nested_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"
}
]
}'
curl -XGET localhost:9200/hotels/nested_hotel/_search -d '{
"query": {
"nested": {
"path" : "rooms",
"query": {
"bool": {
"must": [
{ "range": { "rooms.night": { "from": "2013-02-15", "to": "2013-02-16" } } },
{ "range": { "rooms.allocation": { "gt": 0 } } }
]
}
}
}
}
}'
curl -XGET localhost:9200/hotels/nested_hotel/_search -d '{
"query": {
"nested": {
"path" : "rooms",
"query": {
"bool": {
"must": [
{ "range": { "rooms.night": { "from": "2013-02-16", "to": "2013-02-17" } } },
{ "range": { "rooms.allocation": { "gt": 0 } } }
]
}
}
}
}
}'
curl -XGET localhost:9200/hotels/nested_hotel/_search -d '{
"query": {
"nested": {
"path" : "rooms",
"query": {
"bool": {
"must": [
{ "terms": { "rooms.night": ["2013-02-16", "2013-02-17"], "minimum_match": 2 } },
{ "range": { "rooms.allocation": { "gt": 0 } } }
]
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment