Search code examples
elasticsearchspring-bootresttemplate

How to Make a PUT request to Elastic Search with a JSON file using REST Template


I am creating a SpringBoot Application, where I need to PUT a JSON schema in Elastic Search. The JSON schema will be in my resources folder(in my classpath). How to PUT the raw JSON file using REST Template.

Any Help?" As most of the example on internet are JUST assuming that we have a POJO class to send. But here I am not aware about the JSON Schema. I need to make the request with the raw JSON file.


Solution

  • Assuming the json schema contains the mapping/settings for an index. Then you can put the mapping like as shown below :

    CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
    // CREATE MAPPING
    String mapping_json = new String(Files.readAllBytes(json_mapping_path));
    createIndexRequestBuilder.addMapping("my_mapping", mapping_json);
    CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();