Minimal example of connection from Java and search for every document on index


I want to use Java to do basic queries in a remote elastic cluster.
It seems I successfully am connected to it, but querying does not return any result, whereas I have documents on my elastic cluster, on the requested index "data".

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                new UsernamePasswordCredentials("###", "###"));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost("###", ###, "http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder

        // Create the low-level client
        RestClient restClient =;

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        ElasticsearchClient client = new ElasticsearchClient(transport);

        SearchResponse<Session> search = null;
        try {
            search = -> s
                            .query(q -> q
                                    .term(t -> t
                                            .value(v -> v.stringValue("Name"))
        } catch (IOException e) {

        for (Hit<Session> hit: search.hits().hits()) {

Is there somewhere on the ES documentation a simple example of this? I've been through the "Installing" and "Connecting" docs, but I don't find them this useful for people who are laying their eyes for the first time on the API.

The Session object is not yet producing any errors, but I'm guessing it will at some point because I'm not sure how it should be built. Should it be inheriting from some es API Java class? What should the constructor look like?

Thanks a lot in advance.

First try with this:

    SearchResponse<Session> search = null;
    try {
        search = -> s.index("data"), Session.class);
    } catch (IOException e) {

This should give some results.

If not, please run this in Kibana Dev Console:

GET /_cat/indices/data?v
GET /data/_search

Also make sure you are running that in the context of an integration test. If you are, please tell :wink:

1 Like

The GET /data-*/_search request seems to work correctly.

But an exception is thrown when running this line :

search = -> s.index("data-*"), Session.class);

Exception in thread "main" Jackson exception: Unrecognized token 'Client': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (ByteArrayInputStream); line: 1, column: 8]
	at co.elastic.clients.json.jackson.JacksonJsonpParser.convertException(
	at co.elastic.clients.json.jackson.JacksonJsonpParser.fetchNextToken(
	at co.elastic.clients.json.JsonpDeserializer.deserialize(
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(

This probably comes from the second parameter Session.class. How should it be built? Should it inherit from a Java class in the es API? What should the constructor look like?

@Malo_Maisonneuve , please mention the solution if you have resolved this.

The Java client calls Jackson to deserialize the documents returned by the search query as instances of Session.

You can test this independently from the Java client. Also, the original Jackson exception should follow the one you pasted (Caused by) and provide more information on where Jackson is failing.

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