-
-
Save npvincent/aecc17a81515f56eadbf to your computer and use it in GitHub Desktop.
Attempt to choose author and best match nested book
This file contains 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
#!/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