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.