Elasticsearch Clients(ES客户端 java)

This chapter illustrates(1) configuration and usage of supported Elasticsearch client implementations.
(本章将演示支持的Elasticsearch客户端实现的配置和使用。) Spring Data Elasticsearch operates upon an Elasticsearch client that is connected to a single Elasticsearch node or a cluster.
(Spring Data Elasticsearch在一个连接到单个Elasticsearch节点或集群的Elasticsearch客户端上操作。(这是个that引导的定语从句,翻译的时候要先翻译从句))
Although the Elasticsearch Client can be used to work with the cluster,
applications using Spring Data Elasticsearch normally use the higher level abstractions of Elasticsearch Operations and Elasticsearch Repositories.
(虽然Elasticsearch Client可以用于与集群一起工作,但是使用Spring Data Elasticsearch的应用程序通常使用更高层次的Elasticsearch Operations和Elasticsearch Repositories抽象。)

单词:(1)
illustrate 英 /ˈɪləstreɪts/ vt. 阐明,举例说明;图解 vi. 举例

5.1. Transport Client

The TransportClient is deprecated(1) as of Elasticsearch 7 and will be removed in Elasticsearch 8. (see the Elasticsearch documentation). 
(TransportClient在Elasticsearch 7中已经弃用,将在Elasticsearch 8中移除。)
Spring Data Elasticsearch will support the TransportClient as long as it is available in the used Elasticsearch version but has deprecated the classes using it since version 4.0.
(Spring Data Elasticsearch将支持TransportClient,只要它在原来的Elasticsearch版本中可用,但是从4.0版本开始就已经弃用了使用它的类。)
单词:(1)deprecate 英 /ˈdeprəkeɪt/ vt. 反对;抨击;轻视;声明不赞成

We strongly recommend to use the High Level REST Client instead of the TransportClient.(我们强烈建议使用高级REST客户端而不是TransportClient。)

例子 52. Transport Client

@Configuration
public class TransportClientConfig extends ElasticsearchConfigurationSupport {

    @Bean
    public Client elasticsearchClient() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();        
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 
        return client;
    }

    @Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {

        ElasticsearchTemplate template = new ElasticsearchTemplate(elasticsearchClient, elasticsearchConverter);
        template.setRefreshPolicy(refreshPolicy());                                                 

        return template;
    }
}

// ...

IndexRequest request = new IndexRequest("spring-data")
 .id(randomID())
 .source(someObject);

IndexResponse response = client.index(request);

 
(1)The TransportClient must be configured with the cluster name.(必须配置集群名称)

(2)The host and port to connect the client to.(要连接客户端到的主机和端口)

(3)the RefreshPolicy must be set in the ElasticsearchTemplate (override refreshPolicy() to not use the default)(RefreshPolicy必须在ElasticsearchTemplate中设置(覆盖RefreshPolicy()以不使用默认))

 

5.2. High Level REST Client

The Java High Level REST Client is the default client of Elasticsearch, it provides a straight(1) forward replacement for the TransportClient as it accepts and returns the very same request/response objects and therefore(2)depends on the Elasticsearch core project. Asynchronous(3) calls are operated upon a client managed thread pool and require a callback to be notified when the request is done.

单词:(1)straight 英 /streɪt/  adj. 直的;连续的;笔直的;正直的;整齐的;异性恋的 adv. 直接地;不断地;立即;坦率地

(2)therefore 因此

(3)Asynchronous 异步

5.2。高级REST客户端 

Java高级REST客户端是Elasticsearch的默认客户端,它提供了TransportClient的直接替代,因为它接受并返回相同的请求/响应对象,因此依赖于Elasticsearch的核心项目。异步调用在客户端托管的线程池上操作,并要求在请求完成时通知回调。

Example 53. High Level REST Client

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()  
            .connectedTo("localhost:9200")
            .build();

        return RestClients.create(clientConfiguration).rest();                         
    }
}

// ...

  @Autowired
  RestHighLevelClient highLevelClient;

  RestClient lowLevelClient = highLevelClient.lowLevelClient();                        

// ...

IndexRequest request = new IndexRequest("spring-data")
  .id(randomID())
  .source(singletonMap("feature", "high-level-rest-client"))
  .setRefreshPolicy(IMMEDIATE);

IndexResponse response = highLevelClient.index(request,RequestOptions.DEFAULT);

 

 

posted @ 2021-07-01 19:08  姚春辉  阅读(1026)  评论(0编辑  收藏  举报