Just started off with ElasticSearch and JAVA Client APIs today itself and trying with a very simple Hello World program. However when i am trying to search the document for a particular keyword, i am not getting any results.
Need some help here -
Node node = nodeBuilder().clusterName("elasticsearch").node();
Client client = node.client();
client.prepareIndex("kodcucom", "article", "1")
.setSource(putJsonDocument("Anurag",
"ElasticSeach provides Java API, thus it executes all operations "
+ "asynchronously by using client object..",
new Date(), new String[] { "elasticsearch" }, "Anurag Jain"))
.execute().actionGet();
client.prepareIndex("kodcucom", "article", "2")
.setSource(putJsonDocument("Java Web Application and ElasticSearch (Video)",
"Today, here I am for exemplifying the usage of ElasticSearch which is an open source, distributed "
+ "and scalable full text search engine and a data analysis tool in a Java web application.",
new Date(), new String[] { "elasticsearch" }, "Saanchi Jain"))
.execute().actionGet();
PutJsonDocument method -
public static Map<String, Object> putJsonDocument(String title, String content, Date postDate, String[] tags,
String author) {
Map<String, Object> jsonDocument = new HashMap<String, Object>();
jsonDocument.put("title", title);
jsonDocument.put("content", content);
jsonDocument.put("postDate", postDate);
jsonDocument.put("tags", tags);
jsonDocument.put("author", author);
return jsonDocument;
}
This is putting the documents in ES as the getDocuments are giving me the results.
However searching the documents is not working for me -
public static void searchDocument(Client client, String index, String type, String field, String value) {
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DEFAULT)
.setQuery(QueryBuilders.termQuery(field, value))
.setFrom(0).setSize(60)
.setExplain(true)
.execute()
.actionGet();
SearchHit[] results = response.getHits().getHits();
System.out.println("Current results: " + results.length);
for (SearchHit hit : results) {
System.out.println("------------------------------");
Map<String, Object> result = hit.getSource();
System.out.println(result);
}
}
This is how its being called -
searchDocument(client, "kodcucom", "article", "title", "Anurag");
Output is -
Current results: 0
Now my goal is simple that i just want to search the title field for a keyword say "Anurag" and return the corresponding document. Any advice/pointers?
BR,
Anurag