# Resthighlevelclient configuration on application.yml

**URL:** <https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497>\
**Category:** Elasticsearch\
**Created:** [July 21, 2019, 1:14am UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497 "2019-07-21T01:14:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Buta\_Kota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buta_kota/32/50585_2.png) [@Buta\_Kota](https://discuss.elastic.co/u/Buta_Kota)\
**Post date:** [July 21, 2019, 1:14am UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/1 "2019-07-21T01:14:41Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_changing_the_client_8217_s_initialization_code.html) 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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 21, 2019, 3:10am UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/2 "2019-07-21T03:10:30Z")

</div>

Welcome.

Please [read this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) about how to format your code.

---

<div class="post-metadata">

**Author:** ![Buta\_Kota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buta_kota/32/50585_2.png) [@Buta\_Kota](https://discuss.elastic.co/u/Buta_Kota)\
**Post date:** [July 21, 2019, 4:16pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/3 "2019-07-21T16:16:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 21, 2019, 5:35pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/4 "2019-07-21T17:35:39Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Buta\_Kota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buta_kota/32/50585_2.png) [@Buta\_Kota](https://discuss.elastic.co/u/Buta_Kota)\
**Post date:** [July 21, 2019, 6:23pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/5 "2019-07-21T18:23:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 21, 2019, 6:46pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/6 "2019-07-21T18:46:25Z")

</div>

I meant. Could you print the values of:

```
HOST, PORT_ONE, PORT_TWO, SCHEMA
```

---

<div class="post-metadata">

**Author:** ![Buta\_Kota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buta_kota/32/50585_2.png) [@Buta\_Kota](https://discuss.elastic.co/u/Buta_Kota)\
**Post date:** [July 21, 2019, 7:04pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/7 "2019-07-21T19:04:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 21, 2019, 7:24pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/8 "2019-07-21T19:24:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Buta\_Kota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buta_kota/32/50585_2.png) [@Buta\_Kota](https://discuss.elastic.co/u/Buta_Kota)\
**Post date:** [July 21, 2019, 7:27pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/9 "2019-07-21T19:27:37Z")

</div>

Okay, thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 18, 2019, 7:27pm UTC](https://discuss.elastic.co/t/resthighlevelclient-configuration-on-application-yml/191497/10 "2019-08-18T19:27:43Z")

</div>

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