elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
408 stars 237 forks source link

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

Closed it-ll closed 6 months ago

it-ll commented 6 months ago

Description

No response

### Tasks
l-trotta commented 6 months ago

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 :)

it-ll commented 6 months ago

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

l-trotta commented 6 months ago

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 :)