Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to create index using Java high level Rest client

I am using java high level rest client for integrating elasticsearch in my application,But not able to create an index

somewhere I found that to execute the request we need to use index(request) method (which I have commented in my code) but it is shwing that index(request) method is deprecated from the type RestHighLevelClient.

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);
}
like image 350
Xwin Avatar asked May 29 '26 12:05

Xwin


1 Answers

As the documentation explains, here's 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 looks wrong, as it needs to follow the specific ES request format with mappings, settings and aliases. Better to specify just the mapping instead.

like image 122
rustyx Avatar answered Jun 01 '26 02:06

rustyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!