Skip to content

Instantly share code, notes, and snippets.

@npvincent
Created April 29, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npvincent/aecc17a81515f56eadbf to your computer and use it in GitHub Desktop.
Save npvincent/aecc17a81515f56eadbf to your computer and use it in GitHub Desktop.
Attempt to choose author and best match nested book
#!/usr/bin/env bash
printf "\nDelete old index "
curl -X DELETE 'http://localhost:9200/authors'
printf "\nCreate index "
curl -X PUT 'http://localhost:9200/authors'
printf "\nUpload mapping "
curl -X POST 'http://localhost:9200/authors/_mapping/author' -d '{
"properties": {
"name": {
"type": "string"
},
"books": {
"type": "nested",
"properties": {
"title": {
"type": "string"
},
"pages": {
"type": "integer"
}
}
}
}
}'
printf "\nAdd data "
curl -X POST 'http://localhost:9200/authors/author' -d '{
"name": "tolkien",
"books": [
{"title":"Return of the King", "pages":431},
{"title":"The Fellowship of the Ring", "pages":390},
{"title":"The Two Towers", "pages":376},
{"title":"The Hobbit", "pages":192 }
]
}'
sleep 1
printf "\nFind author and most relevant book "
curl -X POST 'http://localhost:9200/authors/author/_search' -d '{
"query": {
"bool": {
"must": [
{"term": {"name":"tolkien"}}],
"should": [
{"nested": {
"path": "books",
"query": {
"term": {"pages": 376}
}
}}
]
}
},
"aggs": {
"results": {
"terms": { "field": "name" },
"aggs": {
"books": {
"nested": {
"path": "books"
},
"aggs": {
"top_book": { "top_hits": { "size": 1 } }
}
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment