How are child objects addressed in elasticseacrch?
Suppose we create two parents and three children. Note that there are two
children with id c2 but with different parents:
curl -XPUT localhost:9200/test/parent/p1 -d'{
"name": "Parent 1"
}'
curl -XPUT localhost:9200/test/parent/p2 -d'{
"name": "Parent 2"
}'
curl -XPOST localhost:9200/test/child/_mapping -d '{
"child":{
"_parent": {"type": "parent"}
}
}'
curl -XPOST localhost:9200/test/child/c1?parent=p1 -d '{
"child": "Parent 1 - Child 1"
}'
curl -XPOST localhost:9200/test/child/c2?parent=p1 -d '{
"child": "Parent 1 - Child 2"
}'
curl -XPOST localhost:9200/test/child/c2?parent=p2 -d '{
"child": "Parent 2 - Child 2"
}'
If we search the children, we see that there are two children with _id of c2
curl -XGET localhost:9200/test/_search
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "c1",
"_index": "test",
"_score": 1.0,
"_source": {
"child": "Parent 1 - Child 1"
},
"_type": "child"
},
{
"_id": "c2",
"_index": "test",
"_score": 1.0,
"_source": {
"child": "Parent 1 - Child 2"
},
"_type": "child"
},
{
"_id": "c2",
"_index": "test",
"_score": 1.0,
"_source": {
"child": "Parent 2 - Child 2"
},
"_type": "child"
}
],
"max_score": 1.0,
"total": 3
},
"timed_out": false,
"took": 1
}
How do I address p1/c2? Without the parent child relationship, the _id can be used access, change or delete a child object. In my case I let elasticsearch create the id of the objects.
To access the child objects, the _id is not enough:
curl -XGET localhost:9200/test/child/c2
I have to specify the parent as well:
curl -XGET localhost:9200/test/child/c2?parent=p1
In my system it is worse, some objects I can directly access without the parent and others I cannot access. (Why???)
If I delete c2 (without parent!):
curl -XDELETE http://localhost:9200/test/child/c2
both children are deleted. To delete only one child I have to use ?parent=p1
curl -XDELETE http://localhost:9200/test/child/c2?parent=p1
Here are my questions.
What is the best practices to manage the identity of child objects?
Does that mean, that I have to somehow put the parent id manually into the child object and then construct the of the object as
id?parent=parent_idWhy does elasticsearch not return the parent id?
If I let elasticseach create the id of the child objects, are they guaranteed to be unique or can it happen that two children of different parents get the same
id?