I am trying to use elastic search java client here. I tried running this following piece of code using shakespeare example data
final Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
final SearchRequestBuilder searchRequestBuilder = client.prepareSearch("shakespeare")
.setTypes("line")
.setSource("{\n" +
" \"size\": 5,\n" +
" \"aggs\": {\n" +
" \"group_by_state\": {\n" +
" \"terms\": {\n" +
" \"field\": \"state\"\n" +
" },\n" +
" \"aggs\": {\n" +
" \"average_balance\": {\n" +
" \"avg\": {\n" +
" \"field\": \"balance\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}");
searchRequestBuilder.
execute(new ActionListener<SearchResponse>() {
public void onResponse(SearchResponse searchResponse) {
final Terms terms = searchResponse.getAggregations().get("group_by_state");
System.out.println(terms.getBuckets().size());
client.close();
}
public void onFailure(Throwable throwable) {
client.close();
}
});
The result I get is
!!skipped initial part!!
}, {
"_index" : "shakespeare",
"_type" : "line",
"_id" : "92972",
"_score" : 1.0,
"_source":{"line_id":92973,"play_name":"The Tempest","speech_number":4,"line_number":"3.3.17","speaker":"SEBASTIAN","text_entry":"Will we take throughly."}
} ]
},
"aggregations" : {
"group_by_state" : {
"buckets" : [ ]
}
}
}
But If i run the same query in command line using
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 5,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'
And the result -
"aggregations" : {
"group_by_state" : {
"buckets" : [ {
"key" : "al",
"doc_count" : 21,
"average_balance" : {
"value" : 25377.571428571428
}
}, {
"key" : "tx",
"doc_count" : 17,
"average_balance" : {
!!SKIPPING!!
I am not sure what the problem is.. I have also tried looking at the test cases here but no luck.
I think it is wrong index you are looking at (using java api),
Your java api is like
final SearchRequestBuilder searchRequestBuilder = client.prepareSearch("shakespeare")
.setTypes("line")
.setSource("{\n" +
and curl request is like
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
So, search in shakespeare in java api and bank in curl, and ??
Or, Everything is ok!!