Hi,
I am trying out the high level java client for elasticsearch.
I managed on a simple test to query some records, but I do not get the aggregations running.
As I understand I need to put a query and the aggregation to the same SearchSourceBuilder which is put into the SearchRequest object. Is that correct so far?
My test application is throwing the following exception when firing
searchResponse = client.search(searchRequest);
Exception in thread "main" ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:573)
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:549)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:456)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:429)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:368)
at com.tsystems.elasticsearchclientlts.Test.testAggregation(Test.java:89)
at com.tsystems.elasticsearchclientlts.Test.main(Test.java:122)
Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9999], URI [/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 500 Internal Server Error]
{"error":{"root_cause":[{"type":"illegal_state_exception","reason":"value source config is invalid; must have either a field context or a script or marked as unwrapped"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana-6","node":"qaspBNsmRdaXVI8Lq5qj8w","reason":{"type":"illegal_state_exception","reason":"value source config is invalid; must have either a field context or a script or marked as unwrapped"}}]},"status":500}
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:346)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
at org.apache.http.impl.nio.client.InternalRequestExecutor.inputReady(InternalRequestExecutor.java:83)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
at java.lang.Thread.run(Thread.java:748)
Here is the code snippet of my application:
private static void testAggregation(RestHighLevelClient client, String myQueryString) throws IOException
{
// test of aggregation
// building the search (query + aggregations)
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// setting the query which is used for the aggregation
searchSourceBuilder.query(QueryBuilders.queryStringQuery(myQueryString));
// build the aggregation
TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_msisdn")
.field("msisdn.keyword");
aggregation.subAggregation(AggregationBuilders.count("count"));
searchSourceBuilder.aggregation(aggregation);
// create the request
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
// fire the request and get the response
SearchResponse searchResponse = new SearchResponse();
searchResponse = client.search(searchRequest);
// get aggregations:
Aggregations aggregations = searchResponse.getAggregations();
Terms byMsisdn = aggregations.get("by_msisdn");
}
/**
* @param args
*/
public static void main(String[] args)
{
String server = args[0];
int port = Integer.parseInt(args[1]);
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(server, port, "http")));
// define the complete query string, like you do it in kibana or timelion
String myQueryString = new String("logType: ttp OR type: ttp");
try
{
// call query test procedure
testQuerySearch(client, myQueryString);
// call the aggregation test procedure
//testAggregation(client, myQueryString);
client.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks, Andreas