从传输客户端到高级 REST 客户端的 Elasticsearch 升级路径

Elasticsearch upgrade path from transport client to high level REST client

将使用 Elasticsearch 本机 Java 客户端 API (TransportClient) 的应用程序升级到使用 [=32= 的高级 REST 客户端的升级路径是什么? ]?

文档(初步?)似乎表明:

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.

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

但是我不是很清楚这是什么意思。我是否能够在不重写查询或其他客户端类型操作的情况下将我的整个代码库切换到高级 REST 客户端? REST 客户端似乎没有实现 Client 接口。从解耦的角度来看,这可能是有道理的。'

我需要知道的是我是否应该围绕客户端操作构建自己的抽象,或者 HighLevelRestClient 是否已经基本上实现了 Client 接口。

我应该暂时继续编写针对 TransportClient 的代码 API 还是当 TransportClient 被弃用时是否需要重写这些代码?

请注意,我正在查看高级 REST 客户端,而不是低级 REST 客户端。

高级 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();

需要使用 ActionRequests 而不是 ActionRequestBuilders 迁移到上面。我们正在进行此更改,因为传输客户端中的请求和构建器之间总是存在混淆,这两种方法做完全相同的事情。新客户端将通过单一方式提供请求。

如果您想查看当前的文档,它已经上线了,尽管还在进行中:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high.html .

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

传输客户端将很快被弃用,因此我建议尽快转移到高级 REST 客户端,这不会是一个巨大的变化,并且随着我们对其的改进它会得到回报加班,已经通过 REST 是一个很大的进步。