Skip to content

Instantly share code, notes, and snippets.

@rauanmayemir
Created June 20, 2012 07:42
Show Gist options
  • Save rauanmayemir/2958662 to your computer and use it in GitHub Desktop.
Save rauanmayemir/2958662 to your computer and use it in GitHub Desktop.
Multi level nested docs
curl -XDELETE 'http://localhost:9200/nestedtest' && echo
curl -XPUT 'http://localhost:9200/nestedtest' -d '{}'
curl -XPUT 'http://localhost:9200/nestedtest/authors/_mapping' -d '{
"authors": {
"properties": {
"name": {"type": "string"},
"books": {
"type": "nested", "index": "not_analyzed",
"properties": {
"title": {"type": "string"},
"published_year": {"type": "integer", "index": "not_analyzed"},
"options": {"type": "string", "index": "not_analyzed"},
"ordered": {"type": "integer", "index": "not_analyzed"},
"stores": {
"type": "nested", "index": "not_analyzed",
"properties": {
"store": {"type": "integer", "index": "not_analyzed"},
"price": {"type": "float", "index": "not_analyzed"},
"stock": {"type": "integer", "index": "not_analyzed"}
}
}
}
}
}
}
}' && echo
curl -XPUT 'http://localhost:9200/nestedtest/authors/1' -d '{
"name": "John Steinbeck",
"books": [
{
"title": "The Grapes of Wrath", "published_year": 1939,
"options": ["genre#fiction", "country#american", "pulitzer#yes"],
"ordered": 4,
"stores": [
{"store": 76, "price": 15.5, "stock": 16},
{"store": 18, "price": 14.3, "stock": 54}
]
}
]
}' && echo
curl -XPUT 'http://localhost:9200/nestedtest/authors/2' -d '{
"name": "Philip Roth",
"books": [
{
"title": "American Pastoral", "published_year": 1997,
"options": ["genre#fiction", "country#american"],
"ordered": 2,
"stores": [
{"store": 26, "price": 23.4, "stock": 65},
{"store": 73, "price": 20.3, "stock": 45}
]
}
]
}' && echo
curl -XPOST 'http://localhost:9200/nestedtest/_refresh' && echo
curl -s 'http://localhost:9200/nestedtest/authors/_search?pretty=true' -d '{
"filter": {
"nested": {
"_scope": "latest",
"path": "books",
"filter": {
"bool": {
"must": [
{"range": {"books.published_year": {"gt": 1900}}}
]
}
}
}
},
"facets": {"options": {
"terms_stats": {
"key_field": "options",
"value_field": "ordered"
},
"scope": "latest"
}
}}' && echo
#Search by all books published after 1900 that are cheaper $20
curl -s 'http://localhost:9200/nestedtest/authors/_search?pretty=true' -d '{
"filter": {
"nested": {
"_scope": "latest",
"path": "books",
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{"range": {"books.published_year": {"gt": 1900}}}
]
}
},
"filter": {
"nested": {
"_scope": "available_in_stock",
"path": "books.stores",
"query": {
"bool": {
"must": [
{"range": {"books.stores.price": {"lt": 20.0}}}
]
}
}
}
}
}
}
}
},
"facets": {"options": {
"terms_stats": {
"key_field": "options",
"value_field": "ordered"
},
"scope": "latest"
}
}}' && echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment