Skip to content

Instantly share code, notes, and snippets.

@tallpsmith
Created October 13, 2011 02:36
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 tallpsmith/1283195 to your computer and use it in GitHub Desktop.
Save tallpsmith/1283195 to your computer and use it in GitHub Desktop.
Parent/Child Mapping example of a Document meta-data record and it's child file content
export ES_HOST=http://localhost:9200
# delete & recreate index with defaults
curl -XDELETE $ES_HOST/projects
curl -XPUT $ES_HOST/projects
# configure the Parent mapping type, a 'doc' with a document number and title field to simulate common meta data
curl -XPUT $ES_HOST/projects/doc/_mapping -d'
{
"doc": {
"properties": {
"docno": {"type": "string"},
"title": {"type": "string"}
}
}
}
'
# now create the Child mapping, a type called 'filecontent' to express the physical content of a file that is attached to a Meta-Data record (the parent)
curl -XPUT $ES_HOST/projects/filecontent/_mapping -d'
{
"filecontent": {
"_parent" : { "type": "doc"},
"properties" : {
"content" : {"type": "string"}
}
}
}
'
# Index a single document meta-data record
curl -X PUT $ES_HOST/projects/doc/1 -d '
{
"docno" : "ABC-123",
"title" : "Secret Nuclear Power Plant Control Systems"
}'
# Index a single file's content, attaching it to it's parent type by the parent ID (at least that's what I'm after/interpretting it as ...)
curl -X PUT $ES_HOST/projects/filecontent/1?parent=1 -d '
{
"content" : "When you press the red button, things go bang!"
}'
# I would like to be able to search on the 'filecontent' type but get results
# of the parent, this query is cut/paste from the top_children example, but I'm getting an error
# I'm almost sure I've got the idea of the query wrong, but I'm hoping
# this gist explains the intent I'm trying to do
curl -X GET $ES_HOST/projects/doc/_search?pretty=true -d '
{
"query": {
"top_children" : {
"type": "filecontent",
"query" : {
"term" : {
"content" : "red"
}
}
"score" : "max",
"factor" : 5,
"incremental_factor" : 2
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment