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

**URL:** <https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 1, 2022, 2:26pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334 "2022-04-01T14:26:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malo\_Maisonneuve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/malo_maisonneuve/32/101718_2.png) [@Malo\_Maisonneuve](https://discuss.elastic.co/u/Malo_Maisonneuve)\
**Post date:** [April 1, 2022, 2:26pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/1 "2022-04-01T14:26:47Z")

</div>

Hello,

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

```auto
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("###", "###"));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost("###", ###, "http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        // Create the low-level client
        RestClient restClient = builder.build();

        // 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 = client.search(s -> s
                            .index("data")
                            .query(q -> q
                                    .term(t -> t
                                            .field("timestamp")
                                            .value(v -> v.stringValue("Name"))
                                    )),
                    Session.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (Hit<Session> hit: search.hits().hits()) {
            System.out.println(hit.source());
        }
    }

```

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 1, 2022, 3:40pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/2 "2022-04-01T15:40:23Z")

</div>

First try with this:

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

```

This should give some results.

If not, please run this in Kibana Dev Console:

```auto
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 😉

---

<div class="post-metadata">

**Author:** ![Alexis\_Vie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexis_vie/32/100548_2.png) [@Alexis\_Vie](https://discuss.elastic.co/u/Alexis_Vie)\
**Post date:** [April 8, 2022, 11:13am UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/3 "2022-04-08T11:13:12Z")

</div>

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

But an exception is thrown when running this line :

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

````auto
Exception in thread "main" jakarta.json.stream.JsonParsingException: 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(JacksonJsonpParser.java:84)
	at co.elastic.clients.json.jackson.JacksonJsonpParser.fetchNextToken(JacksonJsonpParser.java:91)
	at co.elastic.clients.json.jackson.JacksonJsonpParser.next(JacksonJsonpParser.java:118)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:69)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:43)
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:279)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1526)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1543)```
````

---

<div class="post-metadata">

**Author:** ![Alexis\_Vie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexis_vie/32/100548_2.png) [@Alexis\_Vie](https://discuss.elastic.co/u/Alexis_Vie)\
**Post date:** [April 8, 2022, 11:17am UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/4 "2022-04-08T11:17:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Naveen\_Vangala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/naveen_vangala/32/103116_2.png) [@Naveen\_Vangala](https://discuss.elastic.co/u/Naveen_Vangala)\
**Post date:** [April 28, 2022, 7:21pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/5 "2022-04-28T19:21:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [April 28, 2022, 9:55pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/6 "2022-04-28T21:55:06Z")

</div>

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.

---

<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:** [May 26, 2022, 9:55pm UTC](https://discuss.elastic.co/t/minimal-example-of-connection-from-java-and-search-for-every-document-on-index/301334/7 "2022-05-26T21:55:46Z")

</div>

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