ElasticSearch/Java Api: how to initialize node for aggregations

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

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"));

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'

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");
1 Like

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