# Migrating from the Java API to the REST API

**URL:** <https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359>\
**Category:** Elasticsearch\
**Created:** [April 16, 2012, 8:18pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359 "2012-04-16T20:18:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [April 16, 2012, 8:18pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/1 "2012-04-16T20:18:21Z")

</div>

Is there a simple way to reuse code that was written using  
elasticsearch's Java API to use the REST API instead, or do I need to  
start from scratch?

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [April 16, 2012, 9:23pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/2 "2012-04-16T21:23:11Z")

</div>

Hi,

you probably need to be more specific. For example:

- are you going to use it directly or via some existing client? (Perl,  
Ruby, ... etc)
- what are you doing with the Java API? Are you doing more then just  
querying and indexing? For example, do you perform any admin API calls?

Without knowing more details I would say that you probably need to rewrite  
your queries into query DSL and you may need to check cluster nodes stats  
yourself regularly to learn how many nodes are active and which of them are  
HTTP enabled and load balance between them (probably some REST clients can  
do this for you). You may also need to think about deploying some proxy  
server for security reasons.

Regards,  
Lukas

On Mon, Apr 16, 2012 at 10:18 PM, Eric Jain [eric.jain@gmail.com](mailto:eric.jain@gmail.com) wrote:

> Is there a simple way to reuse code that was written using  
> elasticsearch's Java API to use the REST API instead, or do I need to  
> start from scratch?

---

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [April 16, 2012, 9:43pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/3 "2012-04-16T21:43:47Z")

</div>

On Mon, Apr 16, 2012 at 14:23, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> you probably need to be more specific. For example:
> 
> - are you going to use it directly or via some existing client? (Perl, Ruby,  
> ... etc)

I'm using (and will continue to use) elasticsearch from a Java application only.

> - what are you doing with the Java API? Are you doing more then just  
> querying and indexing? For example, do you perform any admin API calls?

I use quite a bit of the API, including client.prepareXYZ and  
client.admin().indices().prepareXYZ.

I was hoping there would be a simple way to convert e.g. a  
PutMappingRequest object to json that can be sent to the REST API. But  
it looks like I'll need to write my own json request builder code?

> Without knowing more details I would say that you probably need to rewrite  
> your queries into query DSL and you may need to check cluster nodes stats  
> yourself regularly to learn how many nodes are active and which of them are  
> HTTP enabled and load balance between them (probably some REST clients can  
> You may also need to think about deploying some proxy  
> server for security reasons.

Yes, I'll have a password-protected load balancer.

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [April 16, 2012, 11:19pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/4 "2012-04-16T23:19:44Z")

</div>

On Mon, Apr 16, 2012 at 11:43 PM, Eric Jain [eric.jain@gmail.com](mailto:eric.jain@gmail.com) wrote:

> On Mon, Apr 16, 2012 at 14:23, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:
> 
> > you probably need to be more specific. For example:
> > 
> > - are you going to use it directly or via some existing client? (Perl,  
> > Ruby,  
> > ... etc)
> 
> I'm using (and will continue to use) elasticsearch from a Java application  
> only.
> 
> > - what are you doing with the Java API? Are you doing more then just  
> > querying and indexing? For example, do you perform any admin API calls?
> 
> I use quite a bit of the API, including client.prepareXYZ and  
> client.admin().indices().prepareXYZ.
> 
> I was hoping there would be a simple way to convert e.g. a  
> PutMappingRequest object to json that can be sent to the REST API. But  
> it looks like I'll need to write my own json request builder code?

I am not sure if that is possible for every Java API request but it is  
possible at least for some of them. For example, if you can  
get org.elasticsearch.search.builder.SearchSourceBuilder then you can call  
toString() on it which will give you JSON string that you are looking for.

HTH

> > Without knowing more details I would say that you probably need to  
> > rewrite  
> > your queries into query DSL and you may need to check cluster nodes stats  
> > yourself regularly to learn how many nodes are active and which of them  
> > are  
> > HTTP enabled and load balance between them (probably some REST clients  
> > can  
> > You may also need to think about deploying some proxy  
> > server for security reasons.
> 
> Yes, I'll have a password-protected load balancer.

---

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [April 17, 2012, 12:15am UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/5 "2012-04-17T00:15:18Z")

</div>

On Mon, Apr 16, 2012 at 16:19, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> I am not sure if that is possible for every Java API request but it is  
> possible at least for some of them. For example, if you can  
> get org.elasticsearch.search.builder.SearchSourceBuilder then you can call  
> toString() on it which will give you JSON string that you are looking for.

Too bad this doesn't work for SearchRequest.toString() as well...

---

<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:** [April 17, 2012, 1:27pm UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/6 "2012-04-17T13:27:13Z")

</div>

Using the client libs will be meaningless in this case, you will need to  
write something that generates the correct HTTP request and response.

On Tue, Apr 17, 2012 at 3:15 AM, Eric Jain [eric.jain@gmail.com](mailto:eric.jain@gmail.com) wrote:

> On Mon, Apr 16, 2012 at 16:19, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:
> 
> > I am not sure if that is possible for every Java API request but it is  
> > possible at least for some of them. For example, if you can  
> > get org.elasticsearch.search.builder.SearchSourceBuilder then you can  
> > call  
> > toString() on it which will give you JSON string that you are looking  
> > for.
> 
> Too bad this doesn't work for SearchRequest.toString() as well...

---

<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, 3:32am UTC](https://discuss.elastic.co/t/migrating-from-the-java-api-to-the-rest-api/7359/7 "2017-07-06T03:32:16Z")

</div>


