Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

starting elasticsearch instance from java?

I want to manage starting and stopping of elasticsearch from Java. Is there any easy/nice way to do this?

We're trying to deploy ElasticSearch in our product and we want to maintain the instance of the ElasticSearch on our customer's machine in our own product, and right now every thing depends on having an ElasticSearch instance started from es.bat or something.

As an aside, can anyone give me an example of how to mock test some code around the Jest API for ElasticSearch, so I can unit test our stuff that calls ElasticSearch without needing to start an ElasticSearch instance?

like image 541
cdietschrun Avatar asked Jul 06 '13 01:07

cdietschrun


1 Answers

Starting an elasticsearch instance is extermely easy. You just have to use the Java API. That means you have to add the elasticsearch dependency to your project and create a node, as mentioned in the reference:

// on startup
Node node = nodeBuilder().node();
Client client = node.client();

// on shutdown
node.close();

Once you created the node it will behave exactly the same as a node started from the command line. You can interact with it using the created client object, but by default it will also open the 9200 and 9300 (or following ones if busy) ports for rest calls and inter-node communication.

like image 122
javanna Avatar answered Sep 29 '22 06:09

javanna