# Connection to ElasticSearch with Groovy

**URL:** <https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458>\
**Category:** Elasticsearch\
**Created:** [August 8, 2018, 8:55am UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458 "2018-08-08T08:55:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chetansk](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chetansk](https://discuss.elastic.co/u/chetansk)\
**Post date:** [August 8, 2018, 8:55am UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/1 "2018-08-08T08:55:24Z")

</div>

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

---

<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:** [August 8, 2018, 9:09am UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/2 "2018-08-08T09:09:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chetansk](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chetansk](https://discuss.elastic.co/u/chetansk)\
**Post date:** [August 8, 2018, 10:05am UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/3 "2018-08-08T10:05:53Z")

</div>

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

---

<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:** [August 8, 2018, 11:23am UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/4 "2018-08-08T11:23:50Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![chetansk](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chetansk](https://discuss.elastic.co/u/chetansk)\
**Post date:** [August 8, 2018, 12:39pm UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/5 "2018-08-08T12:39:32Z")

</div>

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.

---

<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:** [September 5, 2018, 12:39pm UTC](https://discuss.elastic.co/t/connection-to-elasticsearch-with-groovy/143458/6 "2018-09-05T12:39:52Z")

</div>

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