# Sample of Java API

**URL:** <https://discuss.elastic.co/t/sample-of-java-api/6060>\
**Category:** Elasticsearch\
**Created:** [December 2, 2011, 9:29pm UTC](https://discuss.elastic.co/t/sample-of-java-api/6060 "2011-12-02T21:29:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samCougars](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@samCougars](https://discuss.elastic.co/u/samCougars)\
**Post date:** [December 2, 2011, 9:29pm UTC](https://discuss.elastic.co/t/sample-of-java-api/6060/1 "2011-12-02T21:29:57Z")

</div>

Hope this helps to understand the elasticSearch Concept:  
String jsonString1 = "{" +  
""room":"livingroom","+  
""color":"red" "+  
"}";  
String jsonString2 = "{" +  
""room":"familyroom","+  
""color":"white" "+  
"}";  
String jsonString3 = "{" +  
""room":"kitchen","+  
""color":"blue" "+  
"}";

```
  String jsonString4 = "{" +
    "\"room\":\"bathroom\","+
    "\"color\":\"white\" "+
    "}";

  String jsonString5 = "{" +
    "\"room\":\"garage\","+
    "\"color\":\"blue\" "+
    "}";

  Node node = null;
  node = NodeBuilder.nodeBuilder().node();

  Client client = node.client();
  client.prepareIndex("house", "room", String.valueOf("1")).setSource(jsonString1).execute().actionGet();
  client.prepareIndex("house", "room", String.valueOf("2")).setSource(jsonString2).execute().actionGet();
  client.prepareIndex("house", "room", String.valueOf("3")).setSource(jsonString3).execute().actionGet();
  client.prepareIndex("house", "room", String.valueOf("4")).setSource(jsonString4).execute().actionGet();
  client.prepareIndex("house", "room", String.valueOf("5")).setSource(jsonString5).execute().actionGet();

  QueryBuilder queryBuilder = QueryBuilders.termQuery("color", "white");
  SearchRequestBuilder searchRequestBuilder = client.prepareSearch("house");
  searchRequestBuilder.setTypes("room");
  searchRequestBuilder.setSearchType(SearchType.DEFAULT);
  searchRequestBuilder.setQuery(queryBuilder);
  searchRequestBuilder.setFrom(0).setSize(60).setExplain(true);
  SearchResponse resp = searchRequestBuilder.execute().actionGet();
  for (SearchHit hit : resp.getHits())
    System.out.println("Hit ID: "+hit.getId());
  node.close();
}
catch (Exception e)
{
  e.printStackTrace();  
}

```

Output of this hits :  
Hit ID: 4  
Hit ID: 2

---

<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:** [December 3, 2011, 10:24am UTC](https://discuss.elastic.co/t/sample-of-java-api/6060/2 "2011-12-03T10:24:36Z")

</div>

Hi,

thanks for the effort!

Couple of comments about the code:

- in many cases it is better to use jsonBuilder instead of creating json  
String manually
- if you do search right after the data is indexed it is better to call  
refresh to make sure all the data will be visible to the search

I took your idea and turned it into a little more polished code (I also  
added some syntax sugar and some init and cleanup code), it can be found  
here:  
[https://github.com/lukas-vlcek/elasticsearch.demo/blob/master/src/test/java/org/elasticsearch/demo/BasicTest.java](https://github.com/lukas-vlcek/elasticsearch.demo/blob/master/src/test/java/org/elasticsearch/demo/BasicTest.java)

Regards,  
Lukas

On Fri, Dec 2, 2011 at 10:29 PM, samCougars [sbaniya@gmail.com](mailto:sbaniya@gmail.com) wrote:

> Hope this helps to understand the elasticSearch Concept:  
> String jsonString1 = "{" +  
> ""room":"livingroom","+  
> ""color":"red" "+  
> "}";  
> String jsonString2 = "{" +  
> ""room":"familyroom","+  
> ""color":"white" "+  
> "}";  
> String jsonString3 = "{" +  
> ""room":"kitchen","+  
> ""color":"blue" "+  
> "}";
> 
> ```
> String jsonString4 = "{" +
> "\"room\":\"bathroom\","+
> "\"color\":\"white\" "+
> "}";
> 
> String jsonString5 = "{" +
> "\"room\":\"garage\","+
> "\"color\":\"blue\" "+
> "}";
> 
> Node node = null;
> node = NodeBuilder.nodeBuilder().node();
> 
> Client client = node.client();
> client.prepareIndex("house", "room",
> 
> ```
> 
> String.valueOf("1")).setSource(jsonString1).execute().actionGet();  
> client.prepareIndex("house", "room",  
> String.valueOf("2")).setSource(jsonString2).execute().actionGet();  
> client.prepareIndex("house", "room",  
> String.valueOf("3")).setSource(jsonString3).execute().actionGet();  
> client.prepareIndex("house", "room",  
> String.valueOf("4")).setSource(jsonString4).execute().actionGet();  
> client.prepareIndex("house", "room",  
> String.valueOf("5")).setSource(jsonString5).execute().actionGet();
> 
> ```
> QueryBuilder queryBuilder = QueryBuilders.termQuery("color", "white");
> SearchRequestBuilder searchRequestBuilder =
> 
> ```
> 
> client.prepareSearch("house");  
> searchRequestBuilder.setTypes("room");  
> searchRequestBuilder.setSearchType(SearchType.DEFAULT);  
> searchRequestBuilder.setQuery(queryBuilder);  
> searchRequestBuilder.setFrom(0).setSize(60).setExplain(true);  
> SearchResponse resp = searchRequestBuilder.execute().actionGet();  
> for (SearchHit hit : resp.getHits())  
> System.out.println("Hit ID: "+hit.getId());  
> node.close();  
> }  
> catch (Exception e)  
> {  
> e.printStackTrace();  
> }
> 
> Output of this hits :  
> Hit ID: 4  
> Hit ID: 2
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Sample-of-Java-API-tp3555729p3555729.html](http://elasticsearch-users.115913.n3.nabble.com/Sample-of-Java-API-tp3555729p3555729.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![samCougars](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@samCougars](https://discuss.elastic.co/u/samCougars)\
**Post date:** [December 5, 2011, 11:27pm UTC](https://discuss.elastic.co/t/sample-of-java-api/6060/3 "2011-12-05T23:27:07Z")

</div>

Thank you for your code sample. Now, I get time out error after creating an index ( right after this code)  
client.prepareIndex("house", "room", String.valueOf("1")).setSource(jsonString1).execute().actionGet();

org.elasticsearch.action.UnavailableShardsException: [house][0] [2] shardIt, [0] active : Timeout waiting for [1m], request: index {[house][room][4], source[{"room":"bathroom","color":"white" }]}

Any suggestions?

---

<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:46am UTC](https://discuss.elastic.co/t/sample-of-java-api/6060/4 "2017-07-06T03:46:21Z")

</div>


