Create parent/child index with java api

Hi,

I'm trying to create an index with a parent/child relationship but I'm getting an error while creating the child: "can't specify parent if no parent field has been configured". I don't know how I am supposed to create the mapping between parent and child.

I'm using the 6.0 API.

Code:

    // create parent
    Map<String, Object> parent = new HashMap<String, Object>();
    parent.put("name","Parent1");
    parent.put("age", 40);
    UUID parentId = UUID.randomUUID();
    IndexRequest indexRequest = new IndexRequest(INDEX_JOIN_DATATYPE_TEST, "parent", parentId.toString())
            .source(parent);

    IndexResponse indexResponse = client.index(indexRequest);

    // create children
    Map<String, Object> child1 = new HashMap<String, Object>();
    child1.put("name","Child1");
    child1.put("age", 20);
    child1.put("parentId", parentId.toString());
    indexRequest = new IndexRequest(INDEX_JOIN_DATATYPE_TEST, "child", UUID.randomUUID().toString())
            .source(child1);
    indexRequest.parent("parent");
    indexResponse = client.index(indexRequest);

Can anyone tell me how to use the api correctly? The documentation is not sufficient for me ...

Greetings

did you create your index with the corresponding mapping for parent child? Also if you are on 6.x you should use the join field instead.

No, this is all the code I used. I would like to create the index and its mapping on the fly, without creating it first via the dev tools. Is this possible using the 6.0 java api? If yes, then HOW do I do it?
Thanks in advance! :slight_smile:

you have to put some mapping for the index in order to use the join field. you can use index templates to add some defaults that you want all indices to use.

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