Update of a record in elastic using elastic 8.7 javaclient

955 views Asked by At

I attempted to add a couple new fields to an existing record in elastic search. The record is being updated, however all previously indexed fields have been cleared.

Is there something am I doing wrong?

Maven dependency

<dependency>
   <groupId>co.elastic.clients</groupId>
   <artifactId>elasticsearch-java</artifactId>
   <version>8.7.1</version>
</dependency>

Me code:

RestClientBuilder builder = RestClient.builder(new HttpHost(esIP, Integer.parseInt(esPort)));

RestClient restClient = builder.build();

//jsonMap indecates one record to be indexed 

br.operations(op -> op.index(idx -> idx.index((String) jsonMap.get("index_name")).id(jsonMap.get("id").toString()).document(jsonMap)));
2

There are 2 answers

0
Mathew On BEST ANSWER

Try following, you shoud exclude meta data (like index, id) from doc param.

    RestClient restClient = RestClient.builder(
            new HttpHost("localhost", 9200)).build();

    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(
            restClient, new JacksonJsonpMapper());

    // And create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);


    HashMap jsonMap = new HashMap();
    jsonMap.put("field", "new value");

    UpdateRequest.Builder builder = new UpdateRequest.Builder();
    builder.index("index_name")
            .id("id")
            .doc(jsonMap); // exclude meta data (like index, id)

    UpdateResponse response = client.update(builder.build(), Object.class);

    System.out.println("Indexed with version " + response.version());
0
Farkhod Abdukodirov On

So you are developing low level client. And as your provided source code is not full enough to understand, I'm giving a sample Low Level Rest API over Remote Elasticsearch:

public class ElasticLowLevelRemote {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        System.out.print("enter index ");
        String indexInput = input.nextLine();
        System.out.print("enter id ");
        String id = input.nextLine();
        RestClient restClient = RestClient.builder(
                          new HttpHost("ipaddress", port, "http"))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(
                        new BasicCredentialsProvider() {{
                            setCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials("username", "password"));}})).build();
            HttpEntity entity = new NStringEntity("{\n \"query\": {\n \"match\": {\n \"_id\": \"" + id + "\"\n }\n }\n}",
                ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(
                "GET", "/"+indexInput+"/_search",
                Collections.emptyMap(),
                entity);

        String jsonResponse = EntityUtils.toString(response.getEntity());
        ObjectMapper mapper = new ObjectMapper();
        Object json = mapper.readValue(jsonResponse, Object.class);
        String formattedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

        System.out.println(formattedJson);

        restClient.close();
    }
}