Update operation does not work with BinaryData object.

Open
#615 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
java, json
Domain
api, search

Research direction

Start with the testBinaryDataUpdateIngestion reproducer and trace the UpdateRequest.doc(BinaryData) entry point through the client’s request serialization. Run the reproducer against Elasticsearch and confirm that the created document contains {"foo":"bar"} and that the existing response assertions pass.

Written by the indexing model from the issue text.

Description

Category: Enhancement
Java API client version

7.17.10

Java version

11

Elasticsearch Version

7.17.9

Problem description

If you try and perform an update when using the BinaryData object the document will be created but it will be empty.

I have created a test for a reproducer.

public void testBinaryDataUpdateIngestion() throws Exception {
        String index = "binary-ingestion-test";
        String id = "foo-bar";

        BinaryData data = BinaryData.of("{\"foo\":\"bar\"}".getBytes(), ContentType.APPLICATION_JSON);

        UpdateResponse<BinaryData> response = esAsyncClient.update(UpdateRequest.of(u -> u
                .index(index)
                .id(id)
                .doc(data)
                .docAsUpsert(true)
                .refresh(Refresh.True)), BinaryData.class).get();

        Assertions.assertEquals(response.result().toString(), "Created");

        GetResponse<BinaryData> getResponse =
                esAsyncClient.get(g -> g.index(index)
                                        .id(id)
                        ,BinaryData.class
                ).get();

        Assertions.assertEquals(id, getResponse.id());
        Assertions.assertEquals(
                "{\"foo\":\"bar\"}",
                new String(getResponse.source().asByteBuffer().array(), StandardCharsets.UTF_8)
        );
    }

I was able to get this to work using a similar object and implementing JsonpSerializable like so.

 @Override
    public void serialize(jakarta.json.stream.JsonGenerator generator, JsonpMapper mapper) {
        if(generator instanceof JacksonJsonpGenerator){
            // data is a byte[] containing raw JSON data
            String json = new String(data, StandardCharsets.UTF_8);
            try {
                ((JacksonJsonpGenerator) generator).jacksonGenerator().writeRawValue(json);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to write raw json", e);
            }
        }else{
            throw new UnsupportedOperationException("Only JacksonJsonpGenerator is supported");
        }
    }
Dominant language
Java
Stars
524
Forks
300
Avg merge
1d 11h
Merged PRs (30d)
16

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from elastic/elasticsearch-java

All issues in elastic/elasticsearch-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.