# Using ES with Java API

**URL:** <https://discuss.elastic.co/t/using-es-with-java-api/4444>\
**Category:** Elasticsearch\
**Created:** [May 20, 2011, 10:32am UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444 "2011-05-20T10:32:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![greekscala](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@greekscala](https://discuss.elastic.co/u/greekscala)\
**Post date:** [May 20, 2011, 10:32am UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/1 "2011-05-20T10:32:28Z")

</div>

Hello ES community,

I am new to ES and try to build my head around it 🙂  
I think I am going to use ES for my project, so I am going  
to do some unittests for me.

ES looks on one hand easy to use but on the other hand  
I do not know where to get startet.

Since I do not need now to have a seperate search server  
I would like to use ES from within my Scala app.

I have placed a elasticsearch.json file in my "resources" dir,  
so it will be on the classpath.

Is it sufficient to just get a  
NodeBuilder.nodeBuilder().node().client()  
and work with it? Or is there another step that I am missing?  
Here is an info [http://www.elasticsearch.org/guide/reference/java-api/client.html](http://www.elasticsearch.org/guide/reference/java-api/client.html)  
that for unittest I need to set a node as "local" so that it can  
connect to a local node.  
That confused me a bit.

best regards

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 20, 2011, 11:51am UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/2 "2011-05-20T11:51:51Z")

</div>

you would want to keep the Node returned around as well (and keep it at the application score). And then, when the application stops / closes, call close on it. Other than that, looks good.  
On Friday, May 20, 2011 at 1:32 PM, greekscala wrote:  
Hello ES community,

> I am new to ES and try to build my head around it 🙂  
> I think I am going to use ES for my project, so I am going  
> to do some unittests for me.
> 
> ES looks on one hand easy to use but on the other hand  
> I do not know where to get startet.
> 
> Since I do not need now to have a seperate search server  
> I would like to use ES from within my Scala app.
> 
> I have placed a elasticsearch.json file in my "resources" dir,  
> so it will be on the classpath.
> 
> Is it sufficient to just get a  
> NodeBuilder.nodeBuilder().node().client()  
> and work with it? Or is there another step that I am missing?  
> Here is an info [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/client.html)  
> that for unittest I need to set a node as "local" so that it can  
> connect to a local node.  
> That confused me a bit.
> 
> best regards

---

<div class="post-metadata">

**Author:** ![greekscala](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@greekscala](https://discuss.elastic.co/u/greekscala)\
**Post date:** [May 20, 2011, 1:52pm UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/3 "2011-05-20T13:52:15Z")

</div>

Hello Shay,

thank you for answering. So I should use a singleton Node, once a Node  
is obtained.

thanks.

best regards

On 20 Mai, 13:51, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> you would want to keep the Node returned around as well (and keep it at the application score). And then, when the application stops / closes, call close on it. Other than that, looks good.On Friday, May 20, 2011 at 1:32 PM, greekscala wrote:
> 
> Hello ES community,
> 
> > I am new to ES and try to build my head around it 🙂  
> > I think I am going to use ES for my project, so I am going  
> > to do some unittests for me.
> 
> > ES looks on one hand easy to use but on the other hand  
> > I do not know where to get startet.
> 
> > Since I do not need now to have a seperate search server  
> > I would like to use ES from within my Scala app.
> 
> > I have placed a elasticsearch.json file in my "resources" dir,  
> > so it will be on the classpath.
> 
> > Is it sufficient to just get a  
> > NodeBuilder.nodeBuilder().node().client()  
> > and work with it? Or is there another step that I am missing?  
> > Here is an infohttp://www.elasticsearch.org/guide/reference/java-api/client.html  
> > that for unittest I need to set a node as "local" so that it can  
> > connect to a local node.  
> > That confused me a bit.
> 
> > best regards

---

<div class="post-metadata">

**Author:** ![greekscala](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@greekscala](https://discuss.elastic.co/u/greekscala)\
**Post date:** [May 20, 2011, 1:56pm UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/4 "2011-05-20T13:56:56Z")

</div>

Just found this:  
[http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/a618d990927c6756/1896faf0e22771d9?lnk=gst&q=spring#1896faf0e22771d9](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/a618d990927c6756/1896faf0e22771d9?lnk=gst&q=spring#1896faf0e22771d9)

Think will take the Spring approach, too.

best reagards

On 20 Mai, 15:52, greekscala [hellectro...@gmail.com](mailto:hellectro...@gmail.com) wrote:

> Hello Shay,
> 
> thank you for answering. So I should use a singleton Node, once a Node  
> is obtained.
> 
> thanks.
> 
> best regards
> 
> On 20 Mai, 13:51, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > you would want to keep the Node returned around as well (and keep it at the application score). And then, when the application stops / closes, call close on it. Other than that, looks good.On Friday, May 20, 2011 at 1:32 PM, greekscala wrote:
> 
> > Hello ES community,
> 
> > > I am new to ES and try to build my head around it 🙂  
> > > I think I am going to use ES for my project, so I am going  
> > > to do some unittests for me.
> 
> > > ES looks on one hand easy to use but on the other hand  
> > > I do not know where to get startet.
> 
> > > Since I do not need now to have a seperate search server  
> > > I would like to use ES from within my Scala app.
> 
> > > I have placed a elasticsearch.json file in my "resources" dir,  
> > > so it will be on the classpath.
> 
> > > Is it sufficient to just get a  
> > > NodeBuilder.nodeBuilder().node().client()  
> > > and work with it? Or is there another step that I am missing?  
> > > Here is an infohttp://www.elasticsearch.org/guide/reference/java-api/client.html  
> > > that for unittest I need to set a node as "local" so that it can  
> > > connect to a local node.  
> > > That confused me a bit.
> 
> > > best regards

---

<div class="post-metadata">

**Author:** ![Barsk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barsk/32/13734_2.png) [@Barsk](https://discuss.elastic.co/u/Barsk)\
**Post date:** [May 24, 2011, 7:10am UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/5 "2011-05-24T07:10:50Z")

</div>

greekscala skrev 2011-05-20 15:56:

> Just found this:  
> [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/a618d990927c6756/1896faf0e22771d9?lnk=gst&q=spring#1896faf0e22771d9](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/a618d990927c6756/1896faf0e22771d9?lnk=gst&q=spring#1896faf0e22771d9)
> 
> Think will take the Spring approach, too.
> 
> best reagards

I found that having ES run as a service has many benefits. Scaling is  
one since clustrering support is kind of built-in. And having many  
different indexes running on the same server etc.  
Running tests and queries is also easier on a standalons server.  
But for a small application an embedded solution might work.

When it comes to coding the Java API I found the simplest solution was  
to download the source and look at the plethora of unit tests that are  
in there. They are the true bluerprint of what the API can do. The docs  
where (at the time I started coding) a bit lacking...

Regards  
Kristian

---

<div class="post-metadata">

**Author:** ![greekscala](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@greekscala](https://discuss.elastic.co/u/greekscala)\
**Post date:** [May 31, 2011, 9:29pm UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/6 "2011-05-31T21:29:24Z")

</div>

Hello Kristian,

thank you for your comment. I think that I will need an ES-Server  
in the future. But for now I think that I will try to use ES as  
embedded.

The idea with the unit tests is a good one. Will clone the repository  
and take a look.

Just to be complete. The link above to the spring solution helped.  
I just created a bean with postConstruct and preDestroy annotations:  
@Service  
class SearchServiceESImpl extends SearchService {

private var node: Node = \_

@PostConstruct  
def start() {  
node = NodeBuilder.nodeBuilder.node  
}

@PreDestroy  
def stop() {  
node.close  
}

def client() = node.client  
}

with best regards

On 24 Mai, 09:10, Kristian Jörg [k...@devo.se](mailto:k...@devo.se) wrote:

> greekscala skrev 2011-05-20 15:56:
> 
> > Just found this:  
> > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...
> 
> > Think will take the Spring approach, too.
> 
> > best reagards
> 
> I found that having ES run as a service has many benefits. Scaling is  
> one since clustrering support is kind of built-in. And having many  
> different indexes running on the same server etc.  
> Running tests and queries is also easier on a standalons server.  
> But for a small application an embedded solution might work.
> 
> When it comes to coding the Java API I found the simplest solution was  
> to download the source and look at the plethora of unit tests that are  
> in there. They are the true bluerprint of what the API can do. The docs  
> where (at the time I started coding) a bit lacking...
> 
> Regards  
> Kristian

---

<div class="post-metadata">

**Author:** ![Barsk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barsk/32/13734_2.png) [@Barsk](https://discuss.elastic.co/u/Barsk)\
**Post date:** [June 9, 2011, 5:09pm UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/7 "2011-06-09T17:09:53Z")

</div>

Hello again.

Yes, that Spring solution is what I use myself. In a simple servlet  
there are the standard init and destroy methods as well that can be  
used in similar manner.

/Kristian

greekscala skrev 2011-05-31 23:29:

> Hello Kristian,
> 
> thank you for your comment. I think that I will need an ES-Server  
> in the future. But for now I think that I will try to use ES as  
> embedded.
> 
> The idea with the unit tests is a good one. Will clone the repository  
> and take a look.
> 
> Just to be complete. The link above to the spring solution helped.  
> I just created a bean with postConstruct and preDestroy annotations:  
> @Service  
> class SearchServiceESImpl extends SearchService {
> 
> private var node: Node = \_
> 
> @PostConstruct  
> def start() {  
> node = NodeBuilder.nodeBuilder.node  
> }
> 
> @PreDestroy  
> def stop() {  
> node.close  
> }
> 
> def client() = node.client  
> }
> 
> with best regards
> 
> On 24 Mai, 09:10, Kristian JÃ¶rg[k...@devo.se](mailto:k...@devo.se) wrote:
> 
> > greekscala skrev 2011-05-20 15:56:
> > 
> > > Just found this:  
> > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...  
> > > Think will take the Spring approach, too.  
> > > best reagards  
> > > I found that having ES run as a service has many benefits. Scaling is  
> > > one since clustrering support is kind of built-in. And having many  
> > > different indexes running on the same server etc.  
> > > Running tests and queries is also easier on a standalons server.  
> > > But for a small application an embedded solution might work.
> > 
> > When it comes to coding the Java API I found the simplest solution was  
> > to download the source and look at the plethora of unit tests that are  
> > in there. They are the true bluerprint of what the API can do. The docs  
> > where (at the time I started coding) a bit lacking...
> > 
> > Regards  
> > Kristian

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:04am UTC](https://discuss.elastic.co/t/using-es-with-java-api/4444/8 "2017-07-06T04:04:16Z")

</div>


