elasticsearch-java
elasticsearch-java copied to clipboard
7.16.2 client esClient.shutdown(); not working
- using docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html I get compile error
- ES server 7.14.2
- java 8:
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
.....
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient esClient = new ElasticsearchClient(transport);
SearchResponse<Account> search = esClient.search(s
-> s.index("account*")
.query(q -> q.term(t -> t.field("username").value(v -> v.stringValue("tmajer3")))),
Account.class);
for (Hit<Account> hit : search.hits().hits()) {
System.out.println(hit.source());
}
esClient.shutdown();
- error:
never complite
And how do you search by multiple fields, this DSL API is totally not fluent
using RestHighLevelClient work as expected, but is marked as deprecated
ElasticsearchClient naming convention is not respected it should be ElasticSearchClient
restClient .close()
Please take your time to write up an easy to understand issue, in full sentences, including expectations (especially what you would expect), code samples that can be compiled locally (including imports and maybe even a proper test case sometimes).
Also, please do not mix up several issues, this makes it super hard to discuss, as github issues does not support threading.
The RestHighLevelClient is not part of this repo - this client however lead to the deprecation. What is your concrete question behind this? It is deprecated, this client is the successor.
The naming convention is correct, as this is how Elasticsearch is spelled.
Your last comment is without any context, please add some.
The term query only allows to search for a single term, take a look at the terms query or the bool query how to write several queries. Take a look at the different types of queries in the official Elasticsearch of the query DSL at https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl.html
Also, the github issue tracker is for bugs, if you have questions about using the client, don't hesitate to ask those in the discuss forum. Thanks a lot!
Thanks @spinscale for addressing each comment individually! I'll answer the initial comment.
If you look at the signature of ElasticsearchClient.shutdown(), it returns a ShutdownClient. That object contains the node lifecycle APIs.
If you want to close the client, then you should, as you found out, call restClient.close(). So I guess all points have been resolved.
I hit this issue today and I think that there's something to do better. It's a very weird Java API. For example, the ElasticsearchClient could implement Closeable interface and call the close on the outstanding restClient instance.
I recomment putting a "close()" method directly on the "co.elastic.clients.elasticsearch.ElasticsearchClient" class, just like how the previous RestHighLevelClient class had a "close()" method. This method can simply call "this._transport.close()".
This is a common pattern - such as when chaining input-stream classes in java - you can always close the outter-most class, and it will close the child/chidren that it wraps.
An example of where a close() method would be particularly useful is when using the ElasticsearchClient as a spring bean - you can then use spring to directly close the rest client, without needing to resort to things like wrapper classes, or extension, just to have a method to close the client.
@brian-mcdonell-invitae these are valid points. However the reason we did not to add close() on ElasticsearchClient is because an application can have several client objects all using the same transport (http client). For example:
- an
ElasticsearchClientand anElasticsearchAsyncClient - child clients for API groups, e.g. an
IndicesClientobtained usingElasticsearchClient.indices() - several
ElasticsearchClientwith different options (e.g. additional headers or request parameters)
In all these cases, close()ing one of the client objects will make all others unusable. This isn't a chain like in the case of stream classes, but more a tree. This is why close() is a transport-level method, so that you are well aware that closing it will kill all the API clients using it.
That being said, I understand that shutdown() can be confusing. This is actually the name of a group of APIs related to cluster management. We will rename this group to something more meaningful and less confusing.
Also, to avoid the original issue of the JVM not exiting because the client isn't closed, this can be solved by adding a shutdown hook that closes it.
I'm reopening this issue to implement these two last points.