public SearchQuery(Class<?> invClass) { try { inventoryClass = invClass; uri = new URI(String.format("%s/%s/%s/_search", mgr.getElasticSearchBaseUrl(), invClass.getSimpleName().toLowerCase(), invClass.getSimpleName()) .replaceAll("(?<!:)//", "/")); qb = new QueryBuilder(); } catch (URISyntaxException e) { throw new CloudRuntimeException(e); } }
private String callElasticSearch(final String url) { try { HttpGet get = new HttpGet(url); ResponseHandler<String> rspHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse rsp) throws ClientProtocolException, IOException { String res = null; if (rsp.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { logger.trace(String.format("failed to call %s, 404 not found, the entity queryed doesn't exist", url)); } else if (rsp.getStatusLine().getStatusCode() != HttpStatus.SC_OK && rsp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { String err = String.format("Failed to call , because: \nstatus line: %s\nbody: %s\n", url, rsp.getStatusLine(), EntityUtils.toString(rsp.getEntity())); logger.warn(err); throw new IOException(err); } else { res = EntityUtils.toString(rsp.getEntity()); logger.trace(String.format("Successfully call %s, %s", url, res)); } return res; } }; String res = mgr.getHttpClient().execute(get, rspHandler); return res; } catch (Exception e) { throw new CloudRuntimeException(e.getMessage(), e); } }
private String callElasticSearch() { try { build(); HttpPost post = new HttpPost(uri); final String requestBody = qb.build(); logger.trace(String.format("executing elasticsearch query as:\n%s", requestBody)); StringEntity body = new StringEntity(requestBody); body.setChunked(false); post.setEntity(body); ResponseHandler<String> rspHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse rsp) throws ClientProtocolException, IOException { String res; if (rsp.getStatusLine().getStatusCode() != HttpStatus.SC_OK && rsp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { logger.warn(String.format("Failed to search index[%s] , because: \nstatus line: %s\nbody: %s\nrequest body: %s", inventoryClass.getSimpleName(), rsp.getStatusLine(), EntityUtils.toString(rsp.getEntity()), requestBody)); throw new IOException(String.format("Failed to search index[%s] because %s", inventoryClass.getSimpleName(), rsp.getStatusLine())); } else { res = EntityUtils.toString(rsp.getEntity()); logger.trace(String.format("Successfully search index[%s], %s", inventoryClass.getSimpleName(), res)); } return res; } }; String res = mgr.getHttpClient().execute(post, rspHandler); return res; } catch (Exception e) { throw new CloudRuntimeException(e.getMessage(), e); } }
public <T> String getAsString(String uuid, Class<T> retClass) { String url = String.format("%s/%s/%s/%s", mgr.getElasticSearchBaseUrl(), retClass.getSimpleName().toLowerCase(), retClass.getSimpleName(), uuid); String res = callElasticSearch(url); if (res == null) { return res; } JSONObject jo; try { jo = new JSONObject(res); return jo.getString("_source"); } catch (JSONException e) { throw new CloudRuntimeException(e); } }