Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ElasticSearch: Parent/Child: Find children based on parent properties
#!/bin/bash
# Reset the index.
curl -X DELETE "localhost:9200/test/?pretty=true"
curl -X PUT "localhost:9200/test/?pretty=true"
# Create the parent mapping.
echo
curl -X PUT "localhost:9200/test/user/_mapping?pretty=true" -d '{
"user": {
"properties": {
"firstname": { "type": "string" },
"lastname": { "type": "string" }
}
}
}'
# Create the child mapping.
echo
curl -X PUT "localhost:9200/test/message/_mapping?pretty=true" -d '{
"message": {
"_parent": { "type": "user" },
"properties": {
"content": { "type": "string" }
}
}
}'
# Index some parents.
echo
curl -X POST "localhost:9200/test/user/1" -d '{
"firstname": "Nick",
"lastname": "Hoffman"
}'
echo
curl -X PUT "localhost:9200/test/user/2" -d '{
"firstname": "Nick",
"lastname": "Smith"
}'
# Index some parents
echo
curl -X PUT "localhost:9200/test/message/3?parent=1" -d '{
"content": "A message by Nick."
}'
echo
curl -X PUT "localhost:9200/test/message/4?parent=2" -d '{
"content": "A message by John."
}'
# Search for messages by people whose firstname is "Nick".
#
# *** This returns no results. ***
#
echo
curl -X GET "localhost:9200/test/message/_search?pretty=1" -d '{
fields: ["_parent", "content"],
query: {
term: { "_parent.firstname": "Nick" }
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment