Sample of Java API

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

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

Regards,
Lukas

On Fri, Dec 2, 2011 at 10:29 PM, samCougars 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
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

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?