Elasticsearch upgrade path from transport client to high level REST client

时间:2017-07-10 15:25:37

标签: elasticsearch elasticsearch-5

What is the upgrade path for an application using the Elasticsearch native Java client API (TransportClient) to move to using the high-level REST client for Java?

Documentation (preliminary?) seems to indicate:

The Java High Level REST Client depends on the Elasticsearch core project. It accepts the same request arguments as the TransportClient and returns the same response objects.

(Source: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.x/java-rest-high.html)

But I am not entirely clear what this means. Will I be able to switch my entire codebase over to the high level REST client without rewriting my queries, or other client-type operations? It doesn't seem like the REST client implements the Client interface. That may make sense from a decoupling point-of-view.'

What I need to know is whether I should be building my own abstraction around client operations, or whether HighLevelRestClient will be basically implementing the Client interface already.

Should I continue, for the time being, to write code against the TransportClient API or will that code all need to be rewritten when TransportClient is deprecated?

Note that I am looking at the high-level REST client, not the low-level REST client.

1 个答案:

答案 0 :(得分:3)

高级REST客户端没有实现Client接口。我刚才写的blogpost描述了这个计划。

我们也在编写文档,其中包含一个页面,其中包含有关如何从传输客户端迁移的说明。

新客户端重用来自现有传输客户端的请求和响应,但客户端对象不兼容,这意味着例如以下内容:

IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source("field", "value");
IndexResponse indexResponse = transportClient.index(indexRequest).get();

会变成:

IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source("field", "value");
IndexResponse indexResponse = restHighLevelClient.index(indexRequest);

对于异步请求,调用稍有不同(请参阅方法名称),在新客户端中,我们选择了另一种方法,其名称以" Async"结尾。后缀,你会从以下内容出发:

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
                    }
                });

以下内容:

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
                }
            });

不幸的是,Client#prepare*方法在高级客户端中不可用,所以类似于:

IndexResponse indexResponse = transportClient.prepareIndex("index", "type", "id").setSource("field", "value").get();

需要使用ActionRequest而不是ActionRequestBuilder s迁移到上面。我们正在进行此更改,因为传输客户端中的请求和构建器之间始终存在混淆,这两种方式完全相同。新客户将有一种方式提供请求。

如果您想查看当前的文档,虽然正在进行中,但它已经存在:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high.html

高级REST客户端将取代传输客户端,尽管它的第一个即将发布的版本仅支持索引,批量,获取,删除,更新,搜索,搜索滚动和清除滚动API。接下来将支持缺少的API,我们也会像往常一样对用户做出贡献。

运输客户端很快就会被弃用,因此我建议尽快转移高级REST客户端,它不应该是一个巨大的变化,它会得到回报,因为我们将改进它加班,已经通过REST是一个很大的改进。