# Translate REST call to Java API

**URL:** <https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765>\
**Category:** Elasticsearch\
**Created:** [November 4, 2011, 4:25am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765 "2011-11-04T04:25:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)\
**Post date:** [November 4, 2011, 4:25am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/1 "2011-11-04T04:25:29Z")

</div>

Hi,

I'm having a hard time converting a REST call to the JAVA equivalent,  
the exact REST call is:

curl -XGET [http://192.168.0.100:9200/inventory/\_search?pretty=true](http://192.168.0.100:9200/inventory/_search?pretty=true) -d  
'  
{  
"query" : {  
"query\_string" : {  
"fields" : ["part\_number", "category",  
"description"],  
"query" : "Ac Adapter"  
}  
}

}  
'

but I would really love to "learn" how to do it myself.

Thanks

Diego

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 4, 2011, 8:50am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/2 "2011-11-04T08:50:28Z")

</div>

First you need the querybuilder:

QueryStringQueryBuilder qsqb = QueryBuilders.queryString("Ac  
Adapter").field("part\_number", 1).field("category",  
1).field("description", 1);

Then you need to set the query:  
SearchRequestBuilder srb =  
client.prepareSearch("inventory").setQuery(qsqb);

Now you can add facets or set the filter:  
srb.addFacet();srb.setFilter();

As a last step you need to execute the query and fetch it / or add a  
listener to it ...  
srb.execute().actionGet();srb.execute().addListener();

Regards,  
Peter.

On 4 Nov., 05:25, fmpwizard [fmpwiz...@gmail.com](mailto:fmpwiz...@gmail.com) wrote:

> Hi,
> 
> I'm having a hard time converting a REST call to the JAVA equivalent,  
> the exact REST call is:
> 
> curl -XGEThttp://192.168.0.100:9200/inventory/\_search?pretty=true-d  
> '  
> {  
> "query" : {  
> "query\_string" : {  
> "fields" : ["part\_number", "category",  
> "description"],  
> "query" : "Ac Adapter"  
> }  
> }
> 
> }
> 
> '
> 
> but I would really love to "learn" how to do it myself.
> 
> Thanks
> 
> Diego

---

<div class="post-metadata">

**Author:** ![Diego\_Medina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diego_medina/32/2675_2.png) [@Diego\_Medina](https://discuss.elastic.co/u/Diego_Medina)\
**Post date:** [November 4, 2011, 10:20am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/3 "2011-11-04T10:20:06Z")

</div>

works great, thank you very much!

Diego

On Fri, Nov 4, 2011 at 4:50 AM, Karussell [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com) wrote:

> First you need the querybuilder:
> 
> QueryStringQueryBuilder qsqb = QueryBuilders.queryString("Ac  
> Adapter").field("part\_number", 1).field("category",  
> 1).field("description", 1);
> 
> Then you need to set the query:  
> SearchRequestBuilder srb =  
> client.prepareSearch("inventory").setQuery(qsqb);
> 
> Now you can add facets or set the filter:  
> srb.addFacet();srb.setFilter();
> 
> As a last step you need to execute the query and fetch it / or add a  
> listener to it ...  
> srb.execute().actionGet();srb.execute().addListener();
> 
> Regards,  
> Peter.
> 
> On 4 Nov., 05:25, fmpwizard [fmpwiz...@gmail.com](mailto:fmpwiz...@gmail.com) wrote:
> 
> > Hi,
> > 
> > I'm having a hard time converting a REST call to the JAVA equivalent,  
> > the exact REST call is:
> > 
> > curl -XGEThttp://192.168.0.100:9200/inventory/\_search?pretty=true-d  
> > '  
> > {  
> > "query" : {  
> > "query\_string" : {  
> > "fields" : ["part\_number", "category",  
> > "description"],  
> > "query" : "Ac Adapter"  
> > }  
> > }
> > 
> > }
> > 
> > '
> > 
> > but I would really love to "learn" how to do it myself.
> > 
> > Thanks
> > 
> > Diego

--  
Diego Medina  
Web Developer  
[diego@fmpwizard.com](mailto:diego@fmpwizard.com)  
[http://www.fmpwizard.com](http://www.fmpwizard.com)

---

<div class="post-metadata">

**Author:** ![Srikanth\_gone](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Srikanth\_gone](https://discuss.elastic.co/u/Srikanth_gone)\
**Post date:** [March 11, 2013, 6:37am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/4 "2013-03-11T06:37:59Z")

</div>

Hi All,

How to search specific text using elastic search in java

this is my code..

Settings settings = ImmutableSettings.settingsBuilder()  
.put("http.enabled", "false")  
.put("transport.tcp.port", "9300-9400")  
.put("discovery.zen.ping.multicast.enabled", "false")  
.put("discovery.zen.ping.unicast.hosts", "localhost").build();

```
      Node node = 

```

NodeBuilder.nodeBuilder().client(true).settings(settings).clusterName("elasticsearch").node();  
client = node.client();

```
    QueryBuilder queryBuilder = QueryBuilders.termQuery("userName", 

```

"SampleUser");  
SearchRequestBuilder searchRequestBuilder =  
client.prepareSearch("\_river");  
searchRequestBuilder.setTypes("dc\_user\_river");  
searchRequestBuilder.setSearchType(SearchType.DFS\_QUERY\_AND\_FETCH);  
searchRequestBuilder.setQuery(queryBuilder);  
searchRequestBuilder.setFrom(0).setSize(60).setExplain(true);  
SearchResponse resp = searchRequestBuilder.execute().actionGet();  
System.out.println(resp);  
for (SearchHit hit : resp.getHits())  
System.out.println("Hit ID: "+hit.getId());  
node.close();

output :  
{  
"took" : 32,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}  
}

i am getting 0 records.. is there any thing missing..

how to get records..

please any one help me.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Srikanth\_gone](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Srikanth\_gone](https://discuss.elastic.co/u/Srikanth_gone)\
**Post date:** [March 11, 2013, 6:41am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/5 "2013-03-11T06:41:42Z")

</div>

On Monday, March 11, 2013 12:07:59 PM UTC+5:30, srikanth gone wrote:

> Hi All,
> 
> How to search specific text using Elasticsearch in java
> 
> this is my code..
> 
> Settings settings = ImmutableSettings.settingsBuilder()  
> .put("http.enabled", "false")  
> .put("transport.tcp.port", "9300-9400")  
> .put("discovery.zen.ping.multicast.enabled", "false")  
> .put("discovery.zen.ping.unicast.hosts", "localhost").build();
> 
> ```
> Node node = 
> 
> ```
> 
> NodeBuilder.nodeBuilder().client(true).settings(settings).clusterName("elasticsearch").node();  
> client = node.client();
> 
> ```
> QueryBuilder queryBuilder = QueryBuilders.termQuery("userName", 
> 
> ```
> 
> "SampleUser");  
> SearchRequestBuilder searchRequestBuilder =  
> client.prepareSearch("\_river");  
> searchRequestBuilder.setTypes("dc\_user\_river");  
> searchRequestBuilder.setSearchType(SearchType.DFS\_QUERY\_AND\_FETCH);  
> searchRequestBuilder.setQuery(queryBuilder);  
> searchRequestBuilder.setFrom(0).setSize(60).setExplain(true);  
> SearchResponse resp = searchRequestBuilder.execute().actionGet();  
> System.out.println(resp);  
> for (SearchHit hit : resp.getHits())  
> System.out.println("Hit ID: "+hit.getId());  
> node.close();
> 
> output :  
> {  
> "took" : 32,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 1,  
> "successful" : 1,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }  
> }
> 
> i am getting 0 records.. is there any thing missing..

if i am using browser

[http://192.168.10.2:9200/\_river/dc\_user\_river/\_search?pretty=true](http://192.168.10.2:9200/_river/dc_user_river/_search?pretty=true)

output:

{  
"took" : 16,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 2,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "\_river",  
"\_type" : "dc\_user\_river",  
"\_id" : "\_status",  
"\_score" : 1.0, "\_source" : {"ok":true,"node":{"id":"5jzC7AsUTCKdHCUcob6TsA","name":"Volstagg","transport\_address":"inet[/192.168.10.2:9300]"}}  
}, {  
"\_index" : "\_river",  
"\_type" : "dc\_user\_river",  
"\_id" : "\_meta",  
"\_score" : 1.0, "\_source" : { "type": "mongodb", "mongodb": { "db": "data-collection", "collection": "DC\_USER" }, "index": { "name": "dc\_user\_idx", "type": "DC\_USER" }}  
} ]  
}  
}

> how to get records using java Elasticsearch..
> 
> please any one help me.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [March 11, 2013, 7:58am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/6 "2013-03-11T07:58:45Z")

</div>

When starting a new discussion on a mailing list, please do not reply to  
an existing message, instead start a fresh email. Even if you change the  
subject line of your email, other mail headers still track which thread  
you replied to and your question is "hidden" in that thread and gets less  
attention. It makes following discussions in the mailing list archives  
particularly difficult

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:47am UTC](https://discuss.elastic.co/t/translate-rest-call-to-java-api/5765/7 "2017-07-06T02:47:18Z")

</div>


