Last active
December 12, 2015 04:19
-
-
Save dedico/4713754 to your computer and use it in GitHub Desktop.
Elasticsearch example - index setup using nested feature 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/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 } } } | |
] | |
} | |
} | |
} | |
} | |
}' |
Thanks a lot! Awesome!
Tiny syntax corrections:
# available hotels for two nights 16 and 17 Feb 2013
curl -XGET localhost:9200/hotels/nested_hotel/_search -d '
{
"query": {
"constant_score": {
"filter": {
"bool" : {
"must" : [
{
"nested" : {
"path" : "rooms",
"filter" : {
"bool" : {
"must" : [
{"term" : { "rooms.night" : "2013-02-16" }},
{ "range": { "rooms.allocation": { "gt": 0 } } }
]
}
}
}
},
{
"nested" : {
"path" : "rooms",
"filter" : {
"bool" : {
"must" : [
{"term" : { "rooms.night" : "2013-02-17" }},
{ "range": { "rooms.allocation": { "gt": 0 } } }
]
}
}
}
}
]
}
}
}
}
}'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this: