# Switch from Java to REST API and use XContent builders?

**URL:** <https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208>\
**Category:** Elasticsearch\
**Created:** [August 11, 2010, 8:32pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208 "2010-08-11T20:32:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fucema](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fucema/32/3160_2.png) [@fucema](https://discuss.elastic.co/u/fucema)\
**Post date:** [August 11, 2010, 8:32pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/1 "2010-08-11T20:32:54Z")

</div>

Is it possible to leverage the java xcontent builders to form json  
strings that can be used to interact with the REST API?

I am trying to avoid manually marshalling from java objects to json  
strings if the elastic search java api can do this already. If not I  
will most likely use the Jackson JSON tools to form the json.

Cheers,  
Seon

---

<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:** [August 11, 2010, 8:48pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/2 "2010-08-11T20:48:30Z")

</div>

Yes, at the end those builders can create either binary or string  
representation. But, they don't provide (yet) a way to marshal / unmarshal  
objects. In this case, I would suggest you use jackson to build the json and  
index it in elasticsearch.

-shay.banon

On Wed, Aug 11, 2010 at 11:32 PM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:

> Is it possible to leverage the java xcontent builders to form json  
> strings that can be used to interact with the REST API?
> 
> I am trying to avoid manually marshalling from java objects to json  
> strings if the Elasticsearch java api can do this already. If not I  
> will most likely use the Jackson JSON tools to form the json.
> 
> Cheers,  
> Seon

---

<div class="post-metadata">

**Author:** ![fucema](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fucema/32/3160_2.png) [@fucema](https://discuss.elastic.co/u/fucema)\
**Post date:** [August 11, 2010, 8:58pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/3 "2010-08-11T20:58:47Z")

</div>

Ok thanks Shay.

My use case involves integrating ElasticSearch into Hibernate running  
within Spring.

I have working code that integrates ES seamlessly with Hibernate using  
the existing hibernate search framework (which by default hooks into  
lucene via directory providers). My code leveraged the ES Java API to  
make this happen mostly pain free, but now I have a requirement to  
communicate with ES via the REST API.

Just to be clear, what you are saying is I should skip the builders  
entirely and just use Jackson to facilitate communicating with the ES  
REST API?

- Seon

---

<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:** [August 11, 2010, 9:03pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/4 "2010-08-11T21:03:30Z")

</div>

Now I am confused a bit ;). If you used the native Java API, that menas you  
built the xcontent (json) yourself from the object? If so, you can reuse the  
same code with the REST API. Not sure why you need to move and use the REST  
API, if its because the need of not using the elasticsearch jar in the  
application, then the xcontent builders will not be available... .

Is the hibernate search integration code open somewhere?

-shay.banon

On Wed, Aug 11, 2010 at 11:58 PM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:

> Ok thanks Shay.
> 
> My use case involves integrating Elasticsearch into Hibernate running  
> within Spring.
> 
> I have working code that integrates ES seamlessly with Hibernate using  
> the existing hibernate search framework (which by default hooks into  
> lucene via directory providers). My code leveraged the ES Java API to  
> make this happen mostly pain free, but now I have a requirement to  
> communicate with ES via the REST API.
> 
> Just to be clear, what you are saying is I should skip the builders  
> entirely and just use Jackson to facilitate communicating with the ES  
> REST API?
> 
> - Seon

---

<div class="post-metadata">

**Author:** ![fucema](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fucema/32/3160_2.png) [@fucema](https://discuss.elastic.co/u/fucema)\
**Post date:** [August 11, 2010, 9:25pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/5 "2010-08-11T21:25:57Z")

</div>

Sorry for the confusion. My code is not open-sourced yet, although my  
company is not against making it open.

The reason I am moving to the REST API is because I do not want to  
start an ES Node in this JVM. So I am using the XContent wherever  
possible, but communicating with ES through http (and not a java Node  
instance).

Here's some working code snippets for indexing, the commented out code  
was connecting to ES via no-data node, whereas the working code now  
uses  
a Spring rest client to speak with ES' REST api.

```
public void onAdd(final AddLuceneWork work, final String index, final

```

String type) {  
final String id = work.getIdInString();  
try {  
BinaryXContentBuilder contentBuilder = jsonBuilder().startObject();  
...  
contentBuilder.endObject();  
IndexRequest indexRequest =  
indexRequest(index).type(type).id(id).source(contentBuilder);

// node.client().index(indexRequest, new  
ActionListener() {  
// @Override  
// public void onResponse(IndexResponse indexResponse) {  
// log.info("Successful async index [" + index + "]["

- id + "]");  
// }  
//  
// @Override  
// public void onFailure(Throwable t) {  
// log.warn("Failed to index [" + index + "][" + id +  
"]", t);  
// }  
// });

contentBuilder.string(), index, type, id);

I am working on getting the search facilities working through the REST  
api. Here is some code snippet that is currently broken.  
Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
String representation of the query. This is not a big deal however,  
and I'll just form the search query body by hand probably.

```
public Page<Photo> searchPhotos(XContentQueryBuilder queryBuilder,

```

int pageNum, int pageSize) {  
// results start from index 0  
int from = pageNum \<= 0 ? 0 : (pageNum - 1) \* pageSize;

```
	String query = queryBuilder.toString();
	String response = restTemplate.getForObject(serverBaseUrl + "/

```

{index}/{type}/\_search/?q={query}&from={from}&size={size}",  
String.class, index, type, query, from, pageSize);

// SearchResponse response =  
// node.client()  
// .search(  
// searchRequest(index)  
// .types(type)  
// .searchType(SearchType.DEFAULT)  
// .source(searchSource().query(queryBuilder).from(from).size(pageSize).explain(false))  
// ).actionGet();  
//  
// if (response.getHits().getHits().length \> 0) {  
// Collection idlist = new ArrayList();  
// for (SearchHit hit : response.getHits().getHits()) {  
// try {  
// idlist.add(Long.valueOf(hit.getId()));  
// } catch (NumberFormatException e) {  
// // Defensive try/catch ... should never reach here.  
// log.error("Cannot cast string value [" + hit.getId() + "] to a  
Long");  
// }  
// }  
// if (!idlist.isEmpty()) {  
// Query hibQuery =  
sessionFactory.getCurrentSession().createQuery("from Photo where id in  
(:idlist)").setParameterList("idlist", idlist);  
// return new GenericPage(hibQuery.list(), pageNum,  
pageSize, response.getHits().getTotalHits());  
// }  
// }  
// Default, return empty page with zero results.  
return new GenericPage(Collections.EMPTY\_LIST, pageNum,  
pageSize, 0);  
}

---

<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:** [August 11, 2010, 9:35pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/6 "2010-08-11T21:35:25Z")

</div>

In this case, you should be able to reuse the xcontent builder. But, if you  
don't want to start a (client) node within the app, you can always use the  
transport client, which is more lightweight (does not join the cluster) and  
provide the full API the node client provides (its the same Client  
interface).

-shay.banon

On Thu, Aug 12, 2010 at 12:25 AM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:

> Sorry for the confusion. My code is not open-sourced yet, although my  
> company is not against making it open.
> 
> The reason I am moving to the REST API is because I do not want to  
> start an ES Node in this JVM. So I am using the XContent wherever  
> possible, but communicating with ES through http (and not a java Node  
> instance).
> 
> Here's some working code snippets for indexing, the commented out code  
> was connecting to ES via no-data node, whereas the working code now  
> uses  
> a Spring rest client to speak with ES' REST api.
> 
> ```
> public void onAdd(final AddLuceneWork work, final String index,
> 
> ```
> 
> final  
> String type) {  
> final String id = work.getIdInString();  
> try {  
> BinaryXContentBuilder contentBuilder =  
> jsonBuilder().startObject();  
> ...  
> contentBuilder.endObject();  
> IndexRequest indexRequest =  
> indexRequest(index).type(type).id(id).source(contentBuilder);
> 
> // node.client().index(indexRequest, new  
> ActionListener() {  
> // @Override  
> // public void onResponse(IndexResponse  
> indexResponse) {  
> // log.info("Successful async index [" +  
> index + "]["
> 
> - id + "]");  
> // }  
> //  
> // @Override  
> // public void onFailure(Throwable t) {  
> // log.warn("Failed to index [" + index + "]["
> 
> - id +  
> "]", t);  
> // }  
> // });
> 
> contentBuilder.string(), index, type, id);
> 
> I am working on getting the search facilities working through the REST  
> api. Here is some code snippet that is currently broken.  
> Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
> String representation of the query. This is not a big deal however,  
> and I'll just form the search query body by hand probably.
> 
> ```
> public Page<Photo> searchPhotos(XContentQueryBuilder queryBuilder,
> 
> ```
> 
> int pageNum, int pageSize) {  
> // results start from index 0  
> int from = pageNum \<= 0 ? 0 : (pageNum - 1) \* pageSize;
> 
> ```
> String query = queryBuilder.toString();
> String response = restTemplate.getForObject(serverBaseUrl +
> 
> ```
> 
> "/  
> {index}/{type}/\_search/?q={query}&from={from}&size={size}",  
> String.class, index, type, query, from, pageSize);
> 
> // SearchResponse response =  
> // node.client()  
> // .search(  
> // searchRequest(index)  
> // .types(type)  
> //  
> .searchType(SearchType.DEFAULT)  
> //  
> .source(searchSource().query(queryBuilder).from(from).size(pageSize).explain(false))  
> // ).actionGet();  
> //  
> // if (response.getHits().getHits().length \> 0) {  
> // Collection idlist = new ArrayList();  
> // for (SearchHit hit : response.getHits().getHits())  
> {  
> // try {  
> //  
> idlist.add(Long.valueOf(hit.getId()));  
> // } catch (NumberFormatException e) {  
> // // Defensive try/catch ... should  
> never reach here.  
> // log.error("Cannot cast string value  
> [" + hit.getId() + "] to a  
> Long");  
> // }  
> // }  
> // if (!idlist.isEmpty()) {  
> // Query hibQuery =  
> sessionFactory.getCurrentSession().createQuery("from Photo where id in  
> (:idlist)").setParameterList("idlist", idlist);  
> // return new  
> GenericPage(hibQuery.list(), pageNum,  
> pageSize, response.getHits().getTotalHits());  
> // }  
> // }  
> // Default, return empty page with zero results.  
> return new GenericPage(Collections.EMPTY\_LIST,  
> pageNum,  
> pageSize, 0);  
> }

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [August 12, 2010, 2:01pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/7 "2010-08-12T14:01:22Z")

</div>

Is it necessary to seed the transport client with addresses, or is there a  
way for it to auto-detect?

I am using a non-data node now, but I would opt for a transport client if it  
is lighter and/or faster.

On Wed, Aug 11, 2010 at 5:35 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> In this case, you should be able to reuse the xcontent builder. But, if you  
> don't want to start a (client) node within the app, you can always use the  
> transport client, which is more lightweight (does not join the cluster) and  
> provide the full API the node client provides (its the same Client  
> interface).
> 
> -shay.banon
> 
> On Thu, Aug 12, 2010 at 12:25 AM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:
> 
> > Sorry for the confusion. My code is not open-sourced yet, although my  
> > company is not against making it open.
> > 
> > The reason I am moving to the REST API is because I do not want to  
> > start an ES Node in this JVM. So I am using the XContent wherever  
> > possible, but communicating with ES through http (and not a java Node  
> > instance).
> > 
> > Here's some working code snippets for indexing, the commented out code  
> > was connecting to ES via no-data node, whereas the working code now  
> > uses  
> > a Spring rest client to speak with ES' REST api.
> > 
> > ```
> > public void onAdd(final AddLuceneWork work, final String index,
> > 
> > ```
> > 
> > final  
> > String type) {  
> > final String id = work.getIdInString();  
> > try {  
> > BinaryXContentBuilder contentBuilder =  
> > jsonBuilder().startObject();  
> > ...  
> > contentBuilder.endObject();  
> > IndexRequest indexRequest =  
> > indexRequest(index).type(type).id(id).source(contentBuilder);
> > 
> > // node.client().index(indexRequest, new  
> > ActionListener() {  
> > // @Override  
> > // public void onResponse(IndexResponse  
> > indexResponse) {  
> > // log.info("Successful async index [" +  
> > index + "]["
> > 
> > - id + "]");  
> > // }  
> > //  
> > // @Override  
> > // public void onFailure(Throwable t) {  
> > // log.warn("Failed to index [" + index +  
> > "][" + id +  
> > "]", t);  
> > // }  
> > // });
> > 
> > contentBuilder.string(), index, type, id);
> > 
> > I am working on getting the search facilities working through the REST  
> > api. Here is some code snippet that is currently broken.  
> > Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
> > String representation of the query. This is not a big deal however,  
> > and I'll just form the search query body by hand probably.
> > 
> > ```
> > public Page<Photo> searchPhotos(XContentQueryBuilder queryBuilder,
> > 
> > ```
> > 
> > int pageNum, int pageSize) {  
> > // results start from index 0  
> > int from = pageNum \<= 0 ? 0 : (pageNum - 1) \* pageSize;
> > 
> > ```
> > String query = queryBuilder.toString();
> > String response = restTemplate.getForObject(serverBaseUrl +
> > 
> > ```
> > 
> > "/  
> > {index}/{type}/\_search/?q={query}&from={from}&size={size}",  
> > String.class, index, type, query, from, pageSize);
> > 
> > // SearchResponse response =  
> > // node.client()  
> > // .search(  
> > // searchRequest(index)  
> > // .types(type)  
> > //  
> > .searchType(SearchType.DEFAULT)  
> > //  
> > .source(searchSource().query(queryBuilder).from(from).size(pageSize).explain(false))  
> > // ).actionGet();  
> > //  
> > // if (response.getHits().getHits().length \> 0) {  
> > // Collection idlist = new ArrayList();  
> > // for (SearchHit hit : response.getHits().getHits())  
> > {  
> > // try {  
> > //  
> > idlist.add(Long.valueOf(hit.getId()));  
> > // } catch (NumberFormatException e) {  
> > // // Defensive try/catch ... should  
> > never reach here.  
> > // log.error("Cannot cast string  
> > value [" + hit.getId() + "] to a  
> > Long");  
> > // }  
> > // }  
> > // if (!idlist.isEmpty()) {  
> > // Query hibQuery =  
> > sessionFactory.getCurrentSession().createQuery("from Photo where id in  
> > (:idlist)").setParameterList("idlist", idlist);  
> > // return new  
> > GenericPage(hibQuery.list(), pageNum,  
> > pageSize, response.getHits().getTotalHits());  
> > // }  
> > // }  
> > // Default, return empty page with zero results.  
> > return new GenericPage(Collections.EMPTY\_LIST,  
> > pageNum,  
> > pageSize, 0);  
> > }

---

<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:** [August 12, 2010, 2:24pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/8 "2010-08-12T14:24:21Z")

</div>

Auto detect comes with the node client (and when you create a node client,  
set the client to true, it causes it not be a data node, but add other  
aspects to it (like not becoming a master)).

The idea of the transport client is to connect to one or more nodes, extract  
from them the list of all the cluster members, and then perform operations  
on the cluster. It will be slower than the node client since it will  
probably need to do two hops when indexing. This logic is the same one you  
would have had to implement with your REST client (give it a list of  
addresses to connect to).

-shay.banon

On Thu, Aug 12, 2010 at 5:01 PM, James Cook [jcook@tracermedia.com](mailto:jcook@tracermedia.com) wrote:

> Is it necessary to seed the transport client with addresses, or is there a  
> way for it to auto-detect?
> 
> I am using a non-data node now, but I would opt for a transport client if  
> it is lighter and/or faster.
> 
> On Wed, Aug 11, 2010 at 5:35 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> > In this case, you should be able to reuse the xcontent builder. But, if  
> > you don't want to start a (client) node within the app, you can always use  
> > the transport client, which is more lightweight (does not join the cluster)  
> > and provide the full API the node client provides (its the same Client  
> > interface).
> > 
> > -shay.banon
> > 
> > On Thu, Aug 12, 2010 at 12:25 AM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:
> > 
> > > Sorry for the confusion. My code is not open-sourced yet, although my  
> > > company is not against making it open.
> > > 
> > > The reason I am moving to the REST API is because I do not want to  
> > > start an ES Node in this JVM. So I am using the XContent wherever  
> > > possible, but communicating with ES through http (and not a java Node  
> > > instance).
> > > 
> > > Here's some working code snippets for indexing, the commented out code  
> > > was connecting to ES via no-data node, whereas the working code now  
> > > uses  
> > > a Spring rest client to speak with ES' REST api.
> > > 
> > > ```
> > > public void onAdd(final AddLuceneWork work, final String index,
> > > 
> > > ```
> > > 
> > > final  
> > > String type) {  
> > > final String id = work.getIdInString();  
> > > try {  
> > > BinaryXContentBuilder contentBuilder =  
> > > jsonBuilder().startObject();  
> > > ...  
> > > contentBuilder.endObject();  
> > > IndexRequest indexRequest =  
> > > indexRequest(index).type(type).id(id).source(contentBuilder);
> > > 
> > > // node.client().index(indexRequest, new  
> > > ActionListener() {  
> > > // @Override  
> > > // public void onResponse(IndexResponse  
> > > indexResponse) {  
> > > // log.info("Successful async index [" +  
> > > index + "]["
> > > 
> > > - id + "]");  
> > > // }  
> > > //  
> > > // @Override  
> > > // public void onFailure(Throwable t) {  
> > > // log.warn("Failed to index [" + index +  
> > > "][" + id +  
> > > "]", t);  
> > > // }  
> > > // });
> > > 
> > > contentBuilder.string(), index, type, id);
> > > 
> > > I am working on getting the search facilities working through the REST  
> > > api. Here is some code snippet that is currently broken.  
> > > Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
> > > String representation of the query. This is not a big deal however,  
> > > and I'll just form the search query body by hand probably.
> > > 
> > > ```
> > > public Page<Photo> searchPhotos(XContentQueryBuilder queryBuilder,
> > > 
> > > ```
> > > 
> > > int pageNum, int pageSize) {  
> > > // results start from index 0  
> > > int from = pageNum \<= 0 ? 0 : (pageNum - 1) \* pageSize;
> > > 
> > > ```
> > > String query = queryBuilder.toString();
> > > String response = restTemplate.getForObject(serverBaseUrl
> > > 
> > > ```
> > > 
> > > - "/  
> > > {index}/{type}/\_search/?q={query}&from={from}&size={size}",  
> > > String.class, index, type, query, from, pageSize);
> > > 
> > > // SearchResponse response =  
> > > // node.client()  
> > > // .search(  
> > > // searchRequest(index)  
> > > // .types(type)  
> > > //  
> > > .searchType(SearchType.DEFAULT)  
> > > //  
> > > .source(searchSource().query(queryBuilder).from(from).size(pageSize).explain(false))  
> > > // ).actionGet();  
> > > //  
> > > // if (response.getHits().getHits().length \> 0) {  
> > > // Collection idlist = new ArrayList();  
> > > // for (SearchHit hit :  
> > > response.getHits().getHits()) {  
> > > // try {  
> > > //  
> > > idlist.add(Long.valueOf(hit.getId()));  
> > > // } catch (NumberFormatException e) {  
> > > // // Defensive try/catch ... should  
> > > never reach here.  
> > > // log.error("Cannot cast string  
> > > value [" + hit.getId() + "] to a  
> > > Long");  
> > > // }  
> > > // }  
> > > // if (!idlist.isEmpty()) {  
> > > // Query hibQuery =  
> > > sessionFactory.getCurrentSession().createQuery("from Photo where id in  
> > > (:idlist)").setParameterList("idlist", idlist);  
> > > // return new  
> > > GenericPage(hibQuery.list(), pageNum,  
> > > pageSize, response.getHits().getTotalHits());  
> > > // }  
> > > // }  
> > > // Default, return empty page with zero results.  
> > > return new GenericPage(Collections.EMPTY\_LIST,  
> > > pageNum,  
> > > pageSize, 0);  
> > > }

---

<div class="post-metadata">

**Author:** ![fucema](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fucema/32/3160_2.png) [@fucema](https://discuss.elastic.co/u/fucema)\
**Post date:** [August 12, 2010, 4:51pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/9 "2010-08-12T16:51:22Z")

</div>

The transport client is an intriguing alternative for my use case. The  
reason I am not using the Node is because of the multicast chattiness  
on startup (it does join the cluster and send out multicast udp,  
right?).

Does the transport client use strictly TCP only to communicate with  
the nodes in the cluster?

I'll look into the transport client, as I really rather stick to the  
Java API if possible. Thanks Shay.

- Seon

On Aug 12, 10:24 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Auto detect comes with the node client (and when you create a node client,  
> set the client to true, it causes it not be a data node, but add other  
> aspects to it (like not becoming a master)).
> 
> The idea of the transport client is to connect to one or more nodes, extract  
> from them the list of all the cluster members, and then perform operations  
> on the cluster. It will be slower than the node client since it will  
> probably need to do two hops when indexing. This logic is the same one you  
> would have had to implement with your REST client (give it a list of  
> addresses to connect to).
> 
> -shay.banon
> 
> On Thu, Aug 12, 2010 at 5:01 PM, James Cook [jc...@tracermedia.com](mailto:jc...@tracermedia.com) wrote:
> 
> > Is it necessary to seed the transport client with addresses, or is there a  
> > way for it to auto-detect?
> 
> > I am using a non-data node now, but I would opt for a transport client if  
> > it is lighter and/or faster.
> 
> > On Wed, Aug 11, 2010 at 5:35 PM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > > In this case, you should be able to reuse the xcontent builder. But, if  
> > > you don't want to start a (client) node within the app, you can always use  
> > > the transport client, which is more lightweight (does not join the cluster)  
> > > and provide the full API the node client provides (its the same Client  
> > > interface).
> 
> > > -shay.banon
> 
> > > On Thu, Aug 12, 2010 at 12:25 AM, fucema [fuc...@gmail.com](mailto:fuc...@gmail.com) wrote:
> 
> > > > Sorry for the confusion. My code is not open-sourced yet, although my  
> > > > company is not against making it open.
> 
> > > > The reason I am moving to the REST API is because I do not want to  
> > > > start an ES Node in this JVM. So I am using the XContent wherever  
> > > > possible, but communicating with ES through http (and not a java Node  
> > > > instance).
> 
> > > > Here's some working code snippets for indexing, the commented out code  
> > > > was connecting to ES via no-data node, whereas the working code now  
> > > > uses  
> > > > a Spring rest client to speak with ES' REST api.
> 
> > > > ```
> > > > public void onAdd(final AddLuceneWork work, final String index,
> > > > 
> > > > ```
> > > > 
> > > > final  
> > > > String type) {  
> > > > final String id = work.getIdInString();  
> > > > try {  
> > > > BinaryXContentBuilder contentBuilder =  
> > > > jsonBuilder().startObject();  
> > > > ...  
> > > > contentBuilder.endObject();  
> > > > IndexRequest indexRequest =  
> > > > indexRequest(index).type(type).id(id).source(contentBuilder);
> 
> > > > // node.client().index(indexRequest, new  
> > > > ActionListener() {  
> > > > // @Override  
> > > > // public void onResponse(IndexResponse  
> > > > indexResponse) {  
> > > > // log.info("Successful async index [" +  
> > > > index + "]["
> > > > 
> > > > - id + "]");  
> > > > // }  
> > > > //  
> > > > // @Override  
> > > > // public void onFailure(Throwable t) {  
> > > > // log.warn("Failed to index [" + index +  
> > > > "][" + id +  
> > > > "]", t);  
> > > > // }  
> > > > // });
> 
> > > > ```
> > > > restTemplate.put(serverBaseUrl + INDEX_URI,
> > > > 
> > > > ```
> > > > 
> > > > contentBuilder.string(), index, type, id);
> 
> > > > I am working on getting the search facilities working through the REST  
> > > > api. Here is some code snippet that is currently broken.  
> > > > Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
> > > > String representation of the query. This is not a big deal however,  
> > > > and I'll just form the search query body by hand probably.
> 
> > > > ```
> > > > public Page<Photo> searchPhotos(XContentQueryBuilder queryBuilder,
> > > > 
> > > > ```
> > > > 
> > > > int pageNum, int pageSize) {  
> > > > // results start from index 0  
> > > > int from = pageNum \<= 0 ? 0 : (pageNum - 1) \* pageSize;
> 
> > > > ```
> > > > String query = queryBuilder.toString();
> > > > String response = restTemplate.getForObject(serverBaseUrl
> > > > 
> > > > ```
> > > > 
> > > > - "/  
> > > > {index}/{type}/\_search/?q={query}&from={from}&size={size}",  
> > > > String.class, index, type, query, from, pageSize);
> 
> > > > // SearchResponse response =  
> > > > // node.client()  
> > > > // .search(  
> > > > // searchRequest(index)  
> > > > // .types(type)  
> > > > //  
> > > > .searchType(SearchType.DEFAULT)  
> > > > //  
> > > > .source(searchSource().query(queryBuilder).from(from).size(pageSize).explai n(false))  
> > > > // ).actionGet();  
> > > > //  
> > > > // if (response.getHits().getHits().length \> 0) {  
> > > > // Collection idlist = new ArrayList();  
> > > > // for (SearchHit hit :  
> > > > response.getHits().getHits()) {  
> > > > // try {  
> > > > //  
> > > > idlist.add(Long.valueOf(hit.getId()));  
> > > > // } catch (NumberFormatException e) {  
> > > > // // Defensive try/catch ... should  
> > > > never reach here.  
> > > > // log.error("Cannot cast string  
> > > > value [" + hit.getId() + "] to a  
> > > > Long");  
> > > > // }  
> > > > // }  
> > > > // if (!idlist.isEmpty()) {  
> > > > // Query hibQuery =  
> > > > sessionFactory.getCurrentSession().createQuery("from Photo where id in  
> > > > (:idlist)").setParameterList("idlist", idlist);  
> > > > // return new  
> > > > GenericPage(hibQuery.list(), pageNum,  
> > > > pageSize, response.getHits().getTotalHits());  
> > > > // }  
> > > > // }  
> > > > // Default, return empty page with zero results.  
> > > > return new GenericPage(Collections.EMPTY\_LIST,  
> > > > pageNum,  
> > > > pageSize, 0);  
> > > > }

---

<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:** [August 12, 2010, 4:53pm UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/10 "2010-08-12T16:53:04Z")

</div>

Yes, the transport client does strict TCP. Note, you can also configure the  
node client to use unicast discovery to discover the nodes, it does not have  
to be multicast.

-shay.banon

On Thu, Aug 12, 2010 at 7:51 PM, fucema [fucema@gmail.com](mailto:fucema@gmail.com) wrote:

> The transport client is an intriguing alternative for my use case. The  
> reason I am not using the Node is because of the multicast chattiness  
> on startup (it does join the cluster and send out multicast udp,  
> right?).
> 
> Does the transport client use strictly TCP only to communicate with  
> the nodes in the cluster?
> 
> I'll look into the transport client, as I really rather stick to the  
> Java API if possible. Thanks Shay.
> 
> - Seon
> 
> On Aug 12, 10:24 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Auto detect comes with the node client (and when you create a node  
> > client,  
> > set the client to true, it causes it not be a data node, but add other  
> > aspects to it (like not becoming a master)).
> > 
> > The idea of the transport client is to connect to one or more nodes,  
> > extract  
> > from them the list of all the cluster members, and then perform  
> > operations  
> > on the cluster. It will be slower than the node client since it will  
> > probably need to do two hops when indexing. This logic is the same one  
> > you  
> > would have had to implement with your REST client (give it a list of  
> > addresses to connect to).
> > 
> > -shay.banon
> > 
> > On Thu, Aug 12, 2010 at 5:01 PM, James Cook [jc...@tracermedia.com](mailto:jc...@tracermedia.com)  
> > wrote:
> > 
> > > Is it necessary to seed the transport client with addresses, or is  
> > > there a  
> > > way for it to auto-detect?
> > 
> > > I am using a non-data node now, but I would opt for a transport client  
> > > if  
> > > it is lighter and/or faster.
> > 
> > > On Wed, Aug 11, 2010 at 5:35 PM, Shay Banon \<  
> > > [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)\>wrote:
> > 
> > > > In this case, you should be able to reuse the xcontent builder. But,  
> > > > if  
> > > > you don't want to start a (client) node within the app, you can always  
> > > > use  
> > > > the transport client, which is more lightweight (does not join the  
> > > > cluster)  
> > > > and provide the full API the node client provides (its the same Client  
> > > > interface).
> > 
> > > > -shay.banon
> > 
> > > > On Thu, Aug 12, 2010 at 12:25 AM, fucema [fuc...@gmail.com](mailto:fuc...@gmail.com) wrote:
> > 
> > > > > Sorry for the confusion. My code is not open-sourced yet, although my  
> > > > > company is not against making it open.
> > 
> > > > > The reason I am moving to the REST API is because I do not want to  
> > > > > start an ES Node in this JVM. So I am using the XContent wherever  
> > > > > possible, but communicating with ES through http (and not a java Node  
> > > > > instance).
> > 
> > > > > Here's some working code snippets for indexing, the commented out  
> > > > > code  
> > > > > was connecting to ES via no-data node, whereas the working code now  
> > > > > uses  
> > > > > a Spring rest client to speak with ES' REST api.
> > 
> > > > > ```
> > > > > public void onAdd(final AddLuceneWork work, final String
> > > > > 
> > > > > ```
> 
> index,
> 
> > > > > final  
> > > > > String type) {  
> > > > > final String id = work.getIdInString();  
> > > > > try {  
> > > > > BinaryXContentBuilder contentBuilder =  
> > > > > jsonBuilder().startObject();  
> > > > > ...  
> > > > > contentBuilder.endObject();  
> > > > > IndexRequest indexRequest =  
> > > > > indexRequest(index).type(type).id(id).source(contentBuilder);
> > 
> > > > > // node.client().index(indexRequest, new  
> > > > > ActionListener() {  
> > > > > // @Override  
> > > > > // public void onResponse(IndexResponse  
> > > > > indexResponse) {  
> > > > > // log.info("Successful async index ["
> 
> - 
> 
> > > > > index + "]["
> > > > > 
> > > > > - id + "]");  
> > > > > // }  
> > > > > //  
> > > > > // @Override  
> > > > > // public void onFailure(Throwable t) {  
> > > > > // log.warn("Failed to index [" + index
> 
> - 
> 
> > > > > "][" + id +  
> > > > > "]", t);  
> > > > > // }  
> > > > > // });
> > 
> > > > > ```
> > > > > restTemplate.put(serverBaseUrl + INDEX_URI,
> > > > > 
> > > > > ```
> > > > > 
> > > > > contentBuilder.string(), index, type, id);
> > 
> > > > > I am working on getting the search facilities working through the  
> > > > > REST  
> > > > > api. Here is some code snippet that is currently broken.  
> > > > > Unfortunately, I cannot use an XContentQueryBuilder instance to get a  
> > > > > String representation of the query. This is not a big deal however,  
> > > > > and I'll just form the search query body by hand probably.
> > 
> > > > > ```
> > > > > public Page<Photo> searchPhotos(XContentQueryBuilder
> > > > > 
> > > > > ```
> 
> queryBuilder,
> 
> > > > > int pageNum, int pageSize) {  
> > > > > // results start from index 0  
> > > > > int from = pageNum \<= 0 ? 0 : (pageNum - 1) \*  
> > > > > pageSize;
> > 
> > > > > ```
> > > > > String query = queryBuilder.toString();
> > > > > String response =
> > > > > 
> > > > > ```
> 
> restTemplate.getForObject(serverBaseUrl
> 
> > > > > - "/  
> > > > > {index}/{type}/\_search/?q={query}&from={from}&size={size}",  
> > > > > String.class, index, type, query, from, pageSize);
> > 
> > > > > // SearchResponse response =  
> > > > > // node.client()  
> > > > > // .search(  
> > > > > // searchRequest(index)  
> > > > > // .types(type)  
> > > > > //  
> > > > > .searchType(SearchType.DEFAULT)  
> > > > > //
> 
> .source(searchSource().query(queryBuilder).from(from).size(pageSize).explai  
> n(false))
> 
> > > > > // ).actionGet();  
> > > > > //  
> > > > > // if (response.getHits().getHits().length \> 0) {  
> > > > > // Collection idlist = new  
> > > > > ArrayList();  
> > > > > // for (SearchHit hit :  
> > > > > response.getHits().getHits()) {  
> > > > > // try {  
> > > > > //  
> > > > > idlist.add(Long.valueOf(hit.getId()));  
> > > > > // } catch (NumberFormatException e) {  
> > > > > // // Defensive try/catch ...  
> > > > > should  
> > > > > never reach here.  
> > > > > // log.error("Cannot cast string  
> > > > > value [" + hit.getId() + "] to a  
> > > > > Long");  
> > > > > // }  
> > > > > // }  
> > > > > // if (!idlist.isEmpty()) {  
> > > > > // Query hibQuery =  
> > > > > sessionFactory.getCurrentSession().createQuery("from Photo where id  
> > > > > in  
> > > > > (:idlist)").setParameterList("idlist", idlist);  
> > > > > // return new  
> > > > > GenericPage(hibQuery.list(), pageNum,  
> > > > > pageSize, response.getHits().getTotalHits());  
> > > > > // }  
> > > > > // }  
> > > > > // Default, return empty page with zero results.  
> > > > > return new GenericPage(Collections.EMPTY\_LIST,  
> > > > > pageNum,  
> > > > > pageSize, 0);  
> > > > > }

---

<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:20am UTC](https://discuss.elastic.co/t/switch-from-java-to-rest-api-and-use-xcontent-builders/3208/11 "2017-07-06T04:20:52Z")

</div>


