问题
I want to know: do we have to call node.close()
every time when we are done with our querying/searching process or just client.close()
is fine? Here is my code:
val node =nodeBuilder().client(true).node()
val client =node.client()
val query = QueryBuilders.matchQuery(fieldName.toString(), q).fuzziness(Fuzziness.AUTO)//user can make 2 typo mistakes
val response = client.prepareSearch("arteciatedb")
.setTypes("readOnlyAdmin")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addFields("uuid","FirstName","LastName","Email","SecondryEmail","UserStatus","_source")
.setQuery(query)
.execute()
.actionGet()
val hits = response.getHits
totalHits=hits.getTotalHits
log.info("total search result {}",)
client.close()
node.close()
I am running this code frequently; so, after node.close()
, the next time I execute this code, it starts up the node again (causing delays in search response), which is not desired. I want to know if node.close()
is the right thing to do when we need to call this searching code frequently.
回答1:
In short: no, you can reuse the same client over time without trouble. close()
should be used on shutdown only. Moreover, frequently starting and stopping one or more node clients creates unnecessary noise across the cluster.
Note that using nodeBuilder().client(true).node()
, you are creating a node, that will join the ES cluster. If you just want a client and not a node, you should use TransportClient
(https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html).
来源:https://stackoverflow.com/questions/35064511/do-we-need-to-close-elasticsearch-node-after-the-every-search-request