From 26a8535369daf3ba9724aeec8eeb85240e46f789 Mon Sep 17 00:00:00 2001 From: Ethan Joachim Eldridge Date: Tue, 9 Feb 2016 00:29:20 -0500 Subject: [PATCH] Add close to ElasticClient, fixed path.home bug The need for path.home to be configured is meh, it's documented in the open issue on ES here: https://github.com/elastic/elasticsearch/issues/13155 for now using UUID class to create a unique directory to store data in. --- .gitignore | 2 +- src/main/scala/ElasticClients.scala | 24 ++++++++++++++++++++++- src/main/scala/ElasticSearchService.scala | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d913617..847709f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ target - +tmp diff --git a/src/main/scala/ElasticClients.scala b/src/main/scala/ElasticClients.scala index bf0354f..a8102bf 100644 --- a/src/main/scala/ElasticClients.scala +++ b/src/main/scala/ElasticClients.scala @@ -5,6 +5,7 @@ import org.elasticsearch.client.transport._ import org.elasticsearch.common.settings.Settings import org.elasticsearch.common.transport._ import java.net.{ InetAddress, InetSocketAddress } +import java.util.UUID /** This is to illustrate how you can construct a Transport Client in ES 2.2.0 * @@ -19,6 +20,10 @@ trait TcpClient extends ElasticClient { val client: Client = new TransportClient.Builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)) + + def close { + client.close() + } } import org.elasticsearch.node._ @@ -33,9 +38,18 @@ import org.elasticsearch.node.NodeBuilder._ trait NodeClient extends ElasticClient { val node: Node = nodeBuilder() .clusterName("elasticsearch") + .settings(Settings.settingsBuilder() + .put("http.enabled", false) + .put("path.home", "tmp/" + UUID.randomUUID()) + .put("index.store.type", "memory")) .client(true) .node(); val client: Client = node.client(); + + def close { + client.close() + node.close() + } } /** This is to illustrate how to start a local node and supply a client @@ -48,8 +62,16 @@ trait NodeClient extends ElasticClient { trait LocalNodeClient extends ElasticClient { val node: Node = nodeBuilder() .clusterName("elasticsearch") - .settings(Settings.settingsBuilder().put("http.enabled", false)) + .settings(Settings.settingsBuilder() + .put("http.enabled", false) + .put("path.home", "tmp/" + UUID.randomUUID()) + .put("index.store.type", "memory")) .local(true) .node(); val client: Client = node.client(); + + def close { + client.close() + node.close() + } } \ No newline at end of file diff --git a/src/main/scala/ElasticSearchService.scala b/src/main/scala/ElasticSearchService.scala index fd3d21a..fe211bc 100644 --- a/src/main/scala/ElasticSearchService.scala +++ b/src/main/scala/ElasticSearchService.scala @@ -6,6 +6,7 @@ import org.elasticsearch.index.query._ /** Extend this trait and provide a configured client, then mix it into ElasticSearchService */ trait ElasticClient { val client: Client + def close: Unit } /** Helper Class to make our service interface a little bit easier */