Resthighlevelclient configuration on application.yml

Hi there,

I am trying to connect to elasticsearch with Resthighlevelclient and it is was working ok when i hard code the host, port and schema inside the code. But when I am using application.yml for defining the port, schema, hostname, and cluster name, I get an error saying Hostname may not be null.
I am using this for connecting to elasticsearch.
some of the code I am using are:

@Value ("{elasticsearch.host}") private static String HOST; //private static final String HOST = "localhost"; @Value ( "{elasticsearch.port_one}" )
private int PORT_ONE;
//private static final int PORT_ONE = 9200;
@Value ( "{elasticsearch.port_two}" ) private int PORT_TWO; //private static final int PORT_TWO = 9201; @Value ( "{elasticsearch.schema}" )
private String SCHEME ;

my application.yml looks like this:
elasticsearch:
port_one: 9200
port_two: 9201
index: "test_data"
clustername: "testing_elasticsearch"
type: "testinges"
schema: "http"
host: "localhost"

I can tell the application.yml and the code are not communicating. This might be a silly question, but I am new to elasticsearch and please bear with me.

Do I have to create a configuration class or did I miss something? If so, I do not know how to define a configuration class and I tried to find a code example, found nothing helpful.

Can anyone please give me some idea what to do or show me how to define configuration class for highrestlevelclient and use application.yml configuration?
I highly appreciate your help. Thanks.

Welcome.

Please read this about how to format your code.

Thanks, sorry for not following code format.

Here is the code snippet I have included in my question.

       @Value ("${elasticsearch.host}") 
       private static String HOST; 
       //private static final String HOST = "localhost"; 
       @Value ( "${elasticsearch.port_one}" )
       private int PORT_ONE;
       //private static final int PORT_ONE = 9200;
       @Value ( "${elasticsearch.port_two}" ) 
       private int PORT_TWO; 
       //private static final int PORT_TWO = 9201;
       @Value ( "${elasticsearch.schema}" )
       private String SCHEME ;

application.yml I have :

        elasticsearch:
             host: "localhost"
             schema: "http"
             port_one: 9200
             port_two: 9201
             index: "test_dea"
             clustername: "testing_elasticsearch"

Thanks.

Could you print what are the exact values used to create the client?
If they are incorrect then that's more a problem to solve in spring forums IMO (or whatever framework you're using).

Thanks for your prompt reply.
Here is the code:

               @Value ("${elasticsearch.host}") 
               private static String HOST; 
               //private static final String HOST = "localhost"; 
               @Value ( "${elasticsearch.port_one}" )
               private int PORT_ONE;
               //private static final int PORT_ONE = 9200;
               @Value ( "${elasticsearch.port_two}" ) 
               private int PORT_TWO; 
               //private static final int PORT_TWO = 9201;
               @Value ( "${elasticsearch.schema}" )
               private String SCHEMA ;

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost(HOST, PORT_ONE, SCHEMA),
                new HttpHost(HOST, PORT_TWO, SCHEMA)));

I am using spring boot framework on Intellij IDEA IDE. Thanks.

I meant. Could you print the values of:

HOST, PORT_ONE, PORT_TWO, SCHEMA

I have checked and they are null for SCHEMA and HOST and 0 for PORT_ONE and PORT_TWO.

Though the values are on application.yml, at run time it is not getting the values. How can I make this work using the application.yml ? Sorry if this sounds a silly question. As I told you, I am new for spring framework and Elasticsearch. Thank you very much for your help.

It's definitely a spring problem. You need to ask on spring forum.

Okay, thanks.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.