How can I set a parent / child relationship

I'm trying to create two documents, one build a child of the other. My mapping config is like this

'mappings' => [
            "roadType" => [
                "properties" => [
                ]
            ],
            "carType" => [
                "_parent" => [
                    "type" => "roadType"
                ],
                "properties" => [
                    "make" => [
                        "type" => "string"
                    ],
                    "model" => [
                        "type" => "string"
                    ]
                ]
            ]
        ]

I got this error message .
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"No handler for type [roadType] declared on fie ld [_parent]"}],"type":"mapper_parsing_exception","reason":"No handler for type [roadType] declared on field [_pare nt]"},"status":400}
Does anyone have any idea. Thank you

Hi, Can you let us know as on which version of Elasticsearch your are getting this error?

My Elasticsearch version is 5.3.0 and I am using this package https://github.com/basemkhirat/elasticsearch to communicate with Elasticsearch.

The mapping looks fine and works properly when used directly in Elasticsearch. I feel the problem is in the client which you are using to communicate with Elasticsearch. You can try creating the child type first and then the parent; see if this helps.

Also, just for your information - In the newer version of Elasticsearch type: string has been replaced with type: text or keyword.

Thank you. I have figured out why it is not work.
When create the index , the mapping of parent and child must create in same time. So this is a problem. The client which i am using to communicate with Elasticsearch will sent request to ES server one by one for each mapping type.

Do you know how to create parent/child when the parent type is already exist ?

Its not the same time; the rule is - parent should not exist before child creation, thats why i pointed that the problem is in the client.

What you can try is to change the order of your type creation in the mapping. Put child mapping before parent. It should work.

Your mapping will be like below:

'mappings' => [
        "carType" => [
            "_parent" => [
                "type" => "roadType"
            ],
            "properties" => [
                "make" => [
                    "type" => "text"
                ],
                "model" => [
                    "type" => "text"
                ]
            ]
        ],
        "roadType" => [
            "properties" => [
            ]
        ]
    ]

Thanks I missed this point. But
Do you know how to create parent/child if the parent type is already exist ? Is it possible to do it ?

See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html#_parent_child_restrictions

Mmm. Thanks . So there is no way to create child type if parent type is already exist .

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.