# ElasticSearch/Java Api: how to initialize node for aggregations

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769>\
**Category:** Elasticsearch\
**Created:** [June 3, 2016, 8:15am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769 "2016-06-03T08:15:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [June 3, 2016, 8:15am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/1 "2016-06-03T08:15:49Z")

</div>

Hello everyone,

I am trying to use Java api within Eclipse in order to aggregate my data in Elasticsearch.  
I successfully created a client to access my elasticsearch. Now, I want to do aggregations, that's why i followed this tutorial [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-aggs.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-aggs.html)  
SearchResponse sr = node.client().prepareSearch()  
.setQuery( /\* your query _/ )  
.addAggregation( /_ add an aggregation \*/ )  
.execute().actionGet();

However, it does not indicate how to initialize the variable 'node'. I put:  
Node node = NodeBuilder.nodeBuilder().node();  
SearchResponse sr = node.client().prepareSearch()  
.setQuery( /\* your query _/ )  
.addAggregation( /_ add an aggregation \*/ )  
.execute().actionGet();

But, I get the error: The method prepareSearch(String[]) in the type Client is not applicable for the  
arguments ()

So, do you know how I have to initialize node ?

Thank you for your attention and your help.

S

---

<div class="post-metadata">

**Author:** ![Nicolas\_Guyomar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_guyomar/32/10124_2.png) [@Nicolas\_Guyomar](https://discuss.elastic.co/u/Nicolas_Guyomar)\
**Post date:** [June 3, 2016, 8:27am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/2 "2016-06-03T08:27:17Z")

</div>

Hi,

For my client initialization I use the following snippet :

```
final Settings settings =ImmutableSettings.settingsBuilder().put("cluster.name", "myELCluster").build();
client = new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress("node1HostName", "9300"));
```

---

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [June 3, 2016, 8:31am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/3 "2016-06-03T08:31:25Z")

</div>

Actually, i have no problem to create my client.. I used the following code:  
Client client = TransportClient.builder().build()  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverIpAddr), 9300));  
//.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverIpAddr), 9300));

```
//get response
GetResponse response = client.prepareGet("computationper", "json", "AVUBeMAx5lIvHQq4Iemu")
        .setOperationThreaded(false)
        .get();
System.out.println("ttt " + response.getVersion());

```

Here I just want to add the part for aggregation and I need to initialize the variable 'node'

---

<div class="post-metadata">

**Author:** ![Nicolas\_Guyomar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_guyomar/32/10124_2.png) [@Nicolas\_Guyomar](https://discuss.elastic.co/u/Nicolas_Guyomar)\
**Post date:** [June 3, 2016, 8:36am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/4 "2016-06-03T08:36:23Z")

</div>

I'm not sure I get what you are trying to achieve, but for my aggregation I use ;

```
final SearchRequestBuilder requestBuilder = client.prepareSearch();
requestBuilder.setQuery(/*MyQuery*/).addAggregation(/*MyAggregation*/);
response = requestBuilder.execute().actionGet();

```

An aggregation could be :

```
final TermsBuilder myAggregation =
            AggregationBuilders.terms("httpMethodAggregation").field("httpmethod.raw");
```

---

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [June 3, 2016, 8:57am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/5 "2016-06-03T08:57:22Z")

</div>

Thank you Nicolas, I used what you gave me:  
here is what I did for those who will encounter the same problem.  
String[] index= new String[1];  
index[0]="myIndex";  
//aggregate  
SearchResponse response1 = (SearchResponse) client.prepareSearch(myIndex)  
.execute()  
.actionGet();  
System.out.println(response1);

---

<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:** [July 5, 2017, 10:46pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-how-to-initialize-node-for-aggregations/51769/6 "2017-07-05T22:46:29Z")

</div>


