Skip to content

Instantly share code, notes, and snippets.

@dedico
Last active December 12, 2015 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dedico/4713754 to your computer and use it in GitHub Desktop.
Save dedico/4713754 to your computer and use it in GitHub Desktop.
Elasticsearch example - index setup using nested feature and sample queries
# 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 } } }
]
}
}
}
}
}'
@clintongormley
Copy link

Try this:

curl -XGET 'http://127.0.0.1:9200/hotels/nested_hotel/_search?pretty=1'  -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}}
                                ]
                             }
                          }
                       }
                    }
                 ]
              }
           }
       }
   }
}
'

@dedico
Copy link
Author

dedico commented Feb 8, 2013

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