Created
June 29, 2012 05:01
-
-
Save nickhoffman/3015881 to your computer and use it in GitHub Desktop.
ElasticSearch: Parent/Child: Find children based on parent properties
This file contains hidden or 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
#!/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