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

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

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.

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:

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 = client.search(s -> s.index("data-*"), Session.class);

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

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.