Connection to ElasticSearch with Groovy

Hi,

I am quite beginner in ElasticSearch and Groovy. I have to perform CRUD (Create,Retrieve,Update and Delete) operations in ElasticSearch using Groovy.I am using IntelliJ for Groovy. I have found few lines of codes but getting multiple issues in code.So,couldn't proceed further.
Looking for the suggestions and help.
Thanks.

import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.action.ListenableActionFuture

TransportClient client = TransportClient.builder().settings(Settings.settingsBuilder {
client.transport.sniff = true
cluster.name = "clustername"
}).build()

client.addTransportAddress( (new TransportAddress(InetAddress.getByName("localhost"), 9200) ))

String userId = "1"

ListenableActionFuture future = client.searchAsync {
indices "index"
types "type"
source {
query {
match {
_id = userId
}
}
}
}
SearchResponse response = future.actionGet()
println response

Transport Client uses Transport Port (9300) and not the Rest Port (9200).

@dadoonet Thank you so much for the reply. I tried changing the port to 9300 but still not working. It gives the following error:
Error (10,29) Groovyc:unable to resolve class TransportAddress.

Not sure how all that plays with Groovy. May be @pickypg knows?

If possible anyway, you should use the REST client instead.

While going through some documentation, i found Groovy Elasticsearch Client and tried few lines of code as well. It didn't work either. Could you suggest me something regarding it @pickypg? Thanks.

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