Java – Indexes cannot be created using the Java Advanced REST client

Indexes cannot be created using the Java Advanced REST client… here is a solution to the problem.

Indexes cannot be created using the Java Advanced REST client

I’m using the Java Advanced REST client to integrate ElasticSearch into my application, but I can’t create indexes

I

found somewhere that to execute the request, we need to use the index(request) method (which I commented in my code), but it indicates that the index(request) method has been deprecated from the RestHighLevelClient type.

Here is my code :

@GetMapping("/createIndex")
public boolean createIndex() throws IOException {

IndexRequest request = new IndexRequest(
            "muviuser", 
            "user",  
            "1");   
    String jsonString = "{" +
            "\"user\":\"Bran\"," +
            "\"postDate\":\"2018-01-30\"," +
            "\"message\":\"trying out Elasticsearch\"" +
            "}";
    request.source(jsonString, XContentType.JSON);

client.index(request);
}

Solution

As documentation explains how to create an index using the high-level ES API:

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.mapping("_doc", mappingJson, XContentType.JSON);
    CreateIndexResponse response = client.indices().create(request);

Note that your source document doesn’t look right because it needs to follow a specific ES request format。 With mappings, settings, and aliases. It is best to specify only mapping.

Related Problems and Solutions