ElasticSearch: High Level Rest Client Connection

This entry is part 1 of 4 in the series ElasticSearch High Level Rest Client
(Last Updated On: )

In this tutorial I will show you how to use the ElasticSearch high level rest client.

First you will need to add the low level rest to the pom.

  1. <properties>
  2. <elasticSearch.version>6.2.4</elasticSearch.version>
  3. </properties>
  4.  
  5. <dependency>
  6. <groupId>org.elasticsearch.client</groupId>
  7. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  8. <version>${elasticSearch.version}</version>
  9. </dependency>

Next you will need to specify the imports.

  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import org.apache.http.HttpHost;
  5. import org.elasticsearch.client.RestClient;
  6. import org.elasticsearch.client.RestClientBuilder;
  7. import org.elasticsearch.client.RestHighLevelClient;

Now you can connect to ElasticSearch.

  1. final List hosts = new ArrayList<>(Arrays.asList("localhost"));
  2. final Integer port = 9200;
  3. final String scheme = "http";
  4. final HttpHost[] httpHosts = hosts.stream().map(host -> new HttpHost(host, port, scheme)).toArray(HttpHost[]::new);
  5.  
  6. final RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
  7. final RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

Now you can do whatever you need to!

Series NavigationElasticSearch: High Level Client Post >>

2 thoughts on “ElasticSearch: High Level Rest Client Connection”

Comments are closed.