0

I am using elasticsearch 7.3 version, I am very new to this, all i want is to connect to elasticsearch node, i have installed ealasticsearch in one of the google cloud instance now i want to connect to elasticsearch and create index, delete index using java, how can i achieve that, i used this code but its full of error

public class ElasticConnection {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
           TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));

// on shutdown

client.close(); 
    }

} 

tell what jar should i add or what should i do, please help me out with this.

Thanks.

2 Answers 2

1
public RestHighLevelClient createESRestClient() {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUserName, esPassword));

    RestClientBuilder restClientBuilder = RestClient
            .builder(new HttpHost(esRestclientHost, 9200, "http"));
    // Use this one if your ElasticSearch server is setup to use username & password authentication
    if (esAuthentication) {
        restClientBuilder.setHttpClientConfigCallback(h -> h.setDefaultCredentialsProvider(credentialsProvider));
    }

    return new RestHighLevelClient(restClientBuilder);
}

You can use the following code to create your ElasticSearch connection. RestHighLevelClient is the new client which replaced TransportClient, I suggest that you use RestHighLevelClient.

For your reference this is listed in the ElasticSearch documentation guide: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high-getting-started-initialization.html

Sign up to request clarification or add additional context in comments.

3 Comments

yes my elasticsearch is password-protected, i used this code but esUserName says cannot find symbol, i know i am asking a dumb question, should i pass a string of user-name and password like "elastic" and "elastic" ?
yes the esUserName and esPassword are plain strings. Just pass the credentials you have configured in your ElasticSearch. Also the esRestclientHost is just the hostname of your ElasticSearch server.
You can ignore esAuthentication it was a simple boolean in my code to indicate as a switch whether i need to provide credentials or not. A switch between my Local Elastic instance vs. the Actual environment Elastic. :)
1

Don't use the TransportClient - it is deprecated. use the high level rest client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html.

Also, note that the rest port is 9200, for when using the java high level rest client.

another option that you can use and worth mentioning is Jest: https://github.com/searchbox-io/Jest/tree/master/jest

3 Comments

i added some jar file and copy the given code but it says cannot find symbol HttpHost .
I suggest you read the full documentation of the library you use. Adding jars to your project and hoping it would work is optimistic. Start with a working example. You can look at tests to understand it as well
Happy to hear. Good luck

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.