Skip to content

Instantly share code, notes, and snippets.

@eranid
Created April 3, 2013 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eranid/5299628 to your computer and use it in GitHub Desktop.
Save eranid/5299628 to your computer and use it in GitHub Desktop.
Elasticsearch Parent-Child-Grandchild problem
curl -XPOST 'http://localhost:9200/index1' -d '{
"settings" : {
"number_of_shards" : 5,
"number_of_replicas" : 1
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : {
"type" : "string"
}
}
},
"type2" : {
"_parent" : {
"type" : "type1"
},
"properties" : {
"field2" : {
"type" : "string"
}
}
},
"type3" : {
"_parent" : {
"type" : "type2"
},
"properties" : {
"field3" : {
"type" : "string"
}
}
}
}
}'
#####
# index 3 level 1 docs.
curl -XPOST 'http://localhost:9200/index1/type1/' -d '{"field1":"one"}'
{"ok":true,"_index":"index1","_type":"type1","_id":"P6pC-Vp_S6GlzzYHdugeHw","_version":1}
curl -XPOST 'http://localhost:9200/index1/type1/' -d '{"field1":"two"}'
{"ok":true,"_index":"index1","_type":"type1","_id":"Fj9E3BSBT-yr8ELDwntMlw","_version":1}
curl -XPOST 'http://localhost:9200/index1/type1/' -d '{"field1":"three"}'
{"ok":true,"_index":"index1","_type":"type1","_id":"RfFUdumPT1mrlYcsm01viQ","_version":1}
#####
# sub index two level 2 docs for each level 1 doc
curl -XPOST 'http://localhost:9200/index1/type2/?parent=P6pC-Vp_S6GlzzYHdugeHw' -d '{"field2":"a"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"6K2VV9vaRYqy0jYj7_fUhg","_version":1}
curl -XPOST 'http://localhost:9200/index1/type2/?parent=P6pC-Vp_S6GlzzYHdugeHw' -d '{"field2":"b"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"Jx-kK-o6QweEZHo5j4rnyQ","_version":1}
curl -XPOST 'http://localhost:9200/index1/type2/?parent=P6pC-Vp_S6GlzzYHdugeHw' -d '{"field2":"c"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"-7FCJKpnRqqRnilmq4nSUQ","_version":1}
curl -XPOST 'http://localhost:9200/index1/type2/?parent=Fj9E3BSBT-yr8ELDwntMlw' -d '{"field2":"aa"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"Y3-swLFCQge2ZTY7s3dIxQ","_version":1}
curl -XPOST 'http://localhost:9200/index1/type2/?parent=Fj9E3BSBT-yr8ELDwntMlw' -d '{"field2":"bb"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"D1SkieKlRqS8ArRk7i55jw","_version":1}
curl -XPOST 'http://localhost:9200/index1/type2/?parent=Fj9E3BSBT-yr8ELDwntMlw' -d '{"field2":"cc"}'
{"ok":true,"_index":"index1","_type":"type2","_id":"cgke5Sk-RE-RK9KAE6sbEw","_version":1}
#####
# sub index a level 3 docs for each level 2 doc
curl -XPOST 'http://localhost:9200/index1/type3/?parent=6K2VV9vaRYqy0jYj7_fUhg' -d '{"field3":"1"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"Xvc-EtbaT-iBrvPPilyfkw","_version":1}
curl -XPOST 'http://localhost:9200/index1/type3/?parent=Jx-kK-o6QweEZHo5j4rnyQ' -d '{"field3":"2"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"NN9u94NqQ0ahGfQxum4EMw","_version":1}
curl -XPOST 'http://localhost:9200/index1/type3/?parent=-7FCJKpnRqqRnilmq4nSUQ' -d '{"field3":"3"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"f4J9LgpRSO--WwSNc5CwfA","_version":1}
curl -XPOST 'http://localhost:9200/index1/type3/?parent=Y3-swLFCQge2ZTY7s3dIxQ' -d '{"field3":"11"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"EOPzM5MbQ62kiH4iuXU-tA","_version":1}
curl -XPOST 'http://localhost:9200/index1/type3/?parent=D1SkieKlRqS8ArRk7i55jw' -d '{"field3":"22"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"rROD8R75QiO2ycyUSpN-Ag","_version":1}
curl -XPOST 'http://localhost:9200/index1/type3/?parent=cgke5Sk-RE-RK9KAE6sbEw' -d '{"field3":"33"}'
{"ok":true,"_index":"index1","_type":"type3","_id":"RQYkVWgzTRuwxEzMFoO-SA","_version":1}
# Now let's see how many level 3 documents we have (we are expecting 6):
curl -XPOST 'http://localhost:9200/index1/type3/_search?pretty=true' -d '{
"query": {
"match_all": {}
}
}'
ANSWER:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 6,
"max_score" : 1.0,
"hits" : [ {
... ETC
##########
# when using "has child" query, we only get two
curl -XPOST 'http://localhost:9200/index1/type3/_search?pretty=true' -d '{
"query": {
"bool": {
"must": [
{
"has_parent": {
"parent_type": "type2",
"query": {
"match_all": {}
}
}
}
]
}
}
}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "index1",
"_type" : "type3",
"_id" : "EOPzM5MbQ62kiH4iuXU-tA",
"_score" : 1.0, "_source" : {"field3":"11"}
}, {
"_index" : "index1",
"_type" : "type3",
"_id" : "Xvc-EtbaT-iBrvPPilyfkw",
"_score" : 1.0, "_source" : {"field3":"1"}
} ]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment