Originates from: https://discuss.elastic.co/t/how-to-create-a-child-type-when-its-parent-type-is-already-exist-in-elasticsearch-2-x
In cases when a parent type already has child mappings we can potentially relax the constraint that forbids adding more child type.
The constraint as is today was designed to avoid the following scenario:
PUT /index
{
"mappings": {
"parent" : {
}
}
}
// some time later:
PUT /index/child1/_mapping
{
"child1" : {
"_parent": {
"type": "parent"
}
}
}
The put mapping call fails with: can't add a _parent field that points to an already existing type
The reason we can't allow is this, is that otherwise for already existing parent docs the ids wouldn't be stored in the doc values join field (which the _paren field controls)
However this constraint also prevents adding additional child types:
PUT /index
{
"mappings": {
"parent" : {
},
"child1" : {
"_parent": {
"type": "parent"
}
}
}
}
PUT /index/child2/_mapping
{
"child2" : {
"_parent": {
"type": "parent"
}
}
}
In this case adding the second child type fails. This is too strict and we can allow this, because for the parent type we already store the ids in the doc values join field.
We real hope this feature can role back to the normal logic of v1.x . In most situation, parent are main object of domain, and children can add relation to parent at anytime .
And this is the biggest obstacle for our updating from v1.6 to v2.3 .
The parent-child relationship and flexibility of adding child at a later stage was one the key feature for using elasticsearch. This restriction has taken away the advantage as in many cases it is not possible to identify all the possible child types in advance. Hope the restriction is relaxed and allow to add new child type if one child type already exists as explained by @martijnvg
What release will this change be available in? Any chance it will be backported to the 2.3.x branch? Thanks!
@turp1twin This will be available in 2.4.0 once it is released. (no scheduled date for this yet). The 2.3 branch is mainly meant for bug fixes and this is a change in behavior, so that is why this won't be back ported to the 2.3 branch.
Thanks @martijnvg! 2.4.0 will work just fine... Ship It! :-)
Any updates on a potential ship date for 2.4.0? Thanks!
Hi,
I really need to write a query for a scenario like this to have all the data in the parent and child with just a single query. It seems writing a query when we have more than one child under a single parent is not that easy or possible.
POST /test/grandparent/_search?pretty
{
"query":
{
"has_child":
{
"type": "parent",
"query":
{
"has_child":
{
"type": "child",
"query":
{
"match_all": {}
},
"inner_hits": {}
}
},
"inner_hits": {}
}
}
}
if I had a secondchild type, I can not add it beside the child in the query. I have written the whole mapping below.
PUT /test
{
"mappings": {
"grandparent": {},
"parent": {
"_parent": {
"type": "grandparent"
}
},
"child": {
"_parent": {
"type": "parent"
}
}
,
"secondchild": {
"_parent": {
"type": "parent"
}
}
}
}
Most helpful comment
The parent-child relationship and flexibility of adding child at a later stage was one the key feature for using elasticsearch. This restriction has taken away the advantage as in many cases it is not possible to identify all the possible child types in advance. Hope the restriction is relaxed and allow to add new child type if one child type already exists as explained by @martijnvg