Elasticsearch upgrade path from transport client to advanced REST client
What is the upgrade path for an application that uses the Elasticsearch native Java client API ( TransportClient
) to migrate to an advanced REST client that uses Java ?
Documentation (preliminary?) seems to indicate:
The Java advanced REST client depends on the Elasticsearch core project. It accepts the same request parameters as TransportClient and returns the same response object.
( Source : https://www.elastic.co/guide/en-US/elasticsearch/client/java-rest/5.x/java-rest-high.html )
But I don't know what that means. Can I switch my entire codebase to an advanced REST client without rewriting queries or other client type operations? The REST client doesn't seem to implement that interface. This might make sense from a decoupling perspective. "Client
What I need to know is whether I should build my own abstraction around the client operations or should I already basically implement the interface.HighLevelRestClient
Client
Should I continue writing code against the TransportClient API for now, or do I need to rewrite all this code when TransportClient is deprecated?
Note that I'm looking at a high-level REST client, not a low-level REST client.
Advanced REST clients do not implement this Client
interface. I described the plan in this blog post I wrote a while back .
We are also in the process of writing the documentation, which will include a page with instructions on how to migrate from the transport client.
The new client reuses the requests and responses of the existing transport client, but the client objects are not compatible, which means for example the following:
IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source("field", "value");
IndexResponse indexResponse = transportClient.index(indexRequest).get();
will become this:
IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source("field", "value");
IndexResponse indexResponse = restHighLevelClient.index(indexRequest);
For asynchronous requests, the calls are slightly different (see method names), in the new client we use other methods with names ending with "Async" suffix, you can do it from:
transportClient.index(indexRequest, new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
// called when the operation is successfully completed
}
@Override
public void onFailure(Exception e) {
// called on failure
}
});
to the following:
restHighLevelClient.indexAsync(indexRequest, new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
// called when the operation is successfully completed
}
@Override
public void onFailure(Exception e) {
// called on failure
}
});
Unfortunately, these Client#prepare*
methods won't work in the advanced client, so look like this:
IndexResponse indexResponse = transportClient.prepareIndex("index", "type", "id").setSource("field", "value").get();
Need to use ActionRequest
instead of ActionRequestBuilder
s to migrate to the above version . We're making this change because there's always been a confusion between requests and builders in the transport client, which are two ways of doing the exact same thing. New clients will have only one way to provide requests.
If you want to see the current documentation, it's already there, although it's in progress : https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high.html .
The high-level REST client will replace the transport client, although its first upcoming release only supports the index, bulk, get, delete, update, search, search scroll and clear scroll APIs. The missing API will be supported next, and we will also open it up to users as usual.
The shipping client will be deprecated soon so I recommend moving the advanced REST client asap, this shouldn't be a huge change, it will pay off as we'll be addressing it by incrementally improving it REST is a big one progress.