elastic/elasticsearch-java

How to create parent-child documents according to Java API?

it-ll opened this issue · 3 comments

Description

No response

Tasks

No tasks being tracked yet.

Hello! If you're referring to the Join field type, here is how to define the index using the java client:

esClient.indices().create(cr -> cr
    .index("my-index-000001")
    .mappings(mp -> mp
        .properties("my_id",pr -> pr
             .keyword(k -> k))
        .properties("my_join_field",pr -> pr
              .join(j -> j
                  .relations("question",List.of("answer"))))));

As for the documents, the java classes will have to follow the same structure as the one defined in the mapping, so for example for the Question class will have to be something like:

public record Question(String my_id, String text, String my_join_field){ }

which then can be simply created and indexed like so:

Question question = new Question("1","This is a question","question");

esClient.index(ix -> ix
    .index("my-index-000001")
    .id("1")
    .document(question));

Let me know if this is what you meant, and if it helped :)

ok,very nice,How to write an example of a java api for a subdocument? Thank you

Glad to be of help! I'm closing this issue as this is not really the place for usage support, you can ask this question and more on our discuss forum. I would also like to encourage you to try out the java client yourself, autocompletion is our friend :)