Unable to connect to Elasticsearch client running locally: receiving 'connection refused' on KOTLIN

I am experiencing connection issues with my Elasticsearch client running locally. Specifically, when I try to connect using localhost in the RestClient.builder, I receive a java.net.connectException: connection refused error, and when I try to use my IP address, I get a timeout error. I have developed two different versions of my test app, one using a low-level REST client and the other using the Java API, both of which were developed using Kotlin for and android application. Despite this, I still receive the same errors. I have included the code for both versions below. Can anyone suggest a solution or help me diagnose the issue?

Here are a code snipet to better understand my situation. Code used in the low level rest client:

private fun sendFileStandard() {
        val restClient = RestClient.builder(
            HttpHost("", 9200, "http"),
            HttpHost("localhost", 9200, "http")

        val request = Request(

        var risultato = "defualt value"

        doAsync {
            val cancellable: Cancellable = restClient.performRequestAsync(request,
                object : ResponseListener {
                    override fun onSuccess(response: Response) {
                        dii("onsuccess", response)
                        risultato = "success" +response.toString()

                    override fun onFailure(exception: Exception) {
                        dii("onfailure", exception)
                        risultato = "exception" + exception.toString()
        dii("risultato", risultato)



the java api ones:

val restClient = RestClient.builder(
            HttpHost("localhost", 9200)

        val transport: ElasticsearchTransport = RestClientTransport(
            restClient, JacksonJsonpMapper()
        val client = ElasticsearchClient(transport)

        val input: Reader = StringReader(
            "{'@timestamp': '2022-04-08T13:55:32Z', 'level': 'warn', 'message': 'Some log message'}"
                .replace('\'', '"')

        val request = IndexRequest.of { i: IndexRequest.Builder<JsonData?> ->
        doAsync {
            val response: IndexResponse = client.index(request)

You should probably use "https" instead of "http" - see Connecting | Elasticsearch Java API Client [8.6] | Elastic

Note also that the Java API client uses the RestClient to manage connections, which is why you have started your Java API example by creating a RestClient :wink:

if what are you trying to say is that i should replace "http" here with "https" I tried it yet and obtained the same result

        val restClient = RestClient.builder(
            HttpHost("", 9200, "http"),
            HttpHost("localhost", 9200, "http")

Otherwise if you are trying to say is that i should configure ssl, autentication and https I don't need those level of securety and I disabled those feature. That's part of the elasticsearch.yml file

# Enable security features
xpack.security.enabled: false

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
  enabled: false
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
  enabled: false
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["DESKTOP-29F0DN7"]

# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication

# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

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