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).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);
}