protected InputStream execute(Request request) { return execute(request, true).body(); }
private long countBeforeES5X(String indexAndType, String shardId, QueryBuilder query) { StringBuilder uri = new StringBuilder(indexAndType); uri.append("/_count"); if (StringUtils.hasLength(shardId)) { uri.append("?preference=_shards:"); uri.append(shardId); } Response response = execute(GET, uri.toString(), searchRequest(query)); Number count = (Number) parseContent(response.body(), "count"); return (count != null ? count.longValue() : -1); }
Map<String, List<Map<String, Object>>> hits = parseContent(execute(GET, endpoint + "/_search", new BytesArray(sb.toString())).body(), "hits"); List<Map<String, Object>> docs = hits.get("hits"); if (docs == null || docs.isEmpty()) {
Map<String, Object> content = parseContent(response.body(), "hits");
public String postDocument(Resource resource, BytesArray document) throws IOException { // If untyped, the type() method returns '_doc' Request request = new SimpleRequest(Method.POST, null, resource.index() + "/" + resource.type(), null, document); Response response = execute(request, true); Object id = parseContent(response.body(), "_id"); if (id == null || !StringUtils.hasText(id.toString())) { throw new EsHadoopInvalidRequest( String.format("Could not determine successful write operation. Request[%s > %s] Response[%s]", request.method(), request.path(), IOUtils.asString(response.body()) ) ); } return id.toString(); }
Scroll scroll(String query, BytesArray body, ScrollReader reader) throws IOException { InputStream scroll = client.execute(POST, query, body).body(); try { return reader.read(scroll); } finally { if (scroll instanceof StatsAware) { stats.aggregate(((StatsAware) scroll).stats()); } } }
public List<List<Map<String, Object>>> targetShards(String index, String routing) { List<List<Map<String, Object>>> shardsJson = null; // https://github.com/elasticsearch/elasticsearch/issues/2726 String target = index + "/_search_shards"; if (routing != null) { target += "?routing=" + HttpEncodingTools.encode(routing); } if (indexReadMissingAsEmpty) { Request req = new SimpleRequest(GET, null, target); Response res = executeNotFoundAllowed(req); if (res.status() == HttpStatus.OK) { shardsJson = parseContent(res.body(), "shards"); } else { shardsJson = Collections.emptyList(); } } else { shardsJson = get(target, "shards"); } return shardsJson; }
@SuppressWarnings("rawtypes") Iterator<Map> parseBulkActionResponse(Response response) { InputStream content = response.body(); // Check for failed writes try { ObjectReader r = JsonFactory.objectReader(mapper, Map.class); JsonParser parser = mapper.getJsonFactory().createJsonParser(content); try { if (ParsingUtils.seek(new JacksonJsonParser(parser), "items") == null) { return Collections.<Map>emptyList().iterator(); } else { return r.readValues(parser); } } finally { countStreamStats(content); } } catch (IOException ex) { throw new EsHadoopParsingException(ex); } }
public boolean touch(String index) { if (!indexExists(index)) { Response response = execute(PUT, index, false); if (response.hasFailed()) { String msg = null; // try to parse the answer try { msg = parseContent(response.body(), "error"); } catch (Exception ex) { // can't parse message, move on } if (StringUtils.hasText(msg) && !msg.contains("IndexAlreadyExistsException")) { throw new EsHadoopIllegalStateException(msg); } } return response.hasSucceeded(); } return false; }
public InputStream scroll(String scrollId) { // NB: dynamically get the stats since the transport can change long start = network.transportStats().netTotalTime; try { BytesArray body; if (internalVersion.onOrAfter(EsMajorVersion.V_2_X)) { body = new BytesArray("{\"scroll_id\":\"" + scrollId + "\"}"); } else { body = new BytesArray(scrollId); } // use post instead of get to avoid some weird encoding issues (caused by the long URL) InputStream is = execute(POST, "_search/scroll?scroll=" + scrollKeepAlive.toString(), body).body(); stats.scrollTotal++; return is; } finally { stats.scrollTotalTime += network.transportStats().netTotalTime - start; } }
private void checkResponse(Request request, Response response) { if (response.hasFailed()) { // check error first String msg = null; // try to parse the answer try { EsHadoopException ex = errorExtractor.extractError(this.<Map> parseContent(response.body(), null)); msg = (ex != null) ? ex.toString() : null; if (response.isClientError()) { msg = msg + "\n" + request.body(); } else { msg = errorExtractor.prettify(msg, request.body()); } } catch (Exception ex) { // can't parse message, move on } if (!StringUtils.hasText(msg)) { msg = String.format("[%s] on [%s] failed; server[%s] returned [%s|%s:%s]", request.method().name(), request.path(), response.uri(), response.status(), response.statusDescription(), IOUtils.asStringAlways(response.body())); } throw new EsHadoopInvalidRequest(msg); } }
public String[] scan(String query, BytesArray body) { Map<String, Object> scan = parseContent(execute(POST, query, body).body(), null); String[] data = new String[2]; data[0] = scan.get("_scroll_id").toString(); data[1] = ((Map<?, ?>) scan.get("hits")).get("total").toString(); return data; }
public long count(String indexAndType, ByteSequence query) { Response response = execute(GET, indexAndType + "/_count", query); Number count = (Number) parseContent(response.body(), "count"); return (count != null ? count.longValue() : -1); }
private long countBeforeES5X(String indexAndType, String shardId, QueryBuilder query) { StringBuilder uri = new StringBuilder(indexAndType); uri.append("/_count"); if (StringUtils.hasLength(shardId)) { uri.append("?preference=_shards:"); uri.append(shardId); } Response response = execute(GET, uri.toString(), searchRequest(query)); Number count = (Number) parseContent(response.body(), "count"); return (count != null ? count.longValue() : -1); }
private long countBeforeES5X(String indexAndType, String shardId, QueryBuilder query) { StringBuilder uri = new StringBuilder(indexAndType); uri.append("/_count"); if (StringUtils.hasLength(shardId)) { uri.append("?preference=_shards:"); uri.append(shardId); } Response response = execute(GET, uri.toString(), searchRequest(query)); Number count = (Number) parseContent(response.body(), "count"); return (count != null ? count.longValue() : -1); }
private long countBeforeES5X(String indexAndType, String shardId, QueryBuilder query) { StringBuilder uri = new StringBuilder(indexAndType); uri.append("/_count"); if (StringUtils.hasLength(shardId)) { uri.append("?preference=_shards:"); uri.append(shardId); } Response response = execute(GET, uri.toString(), searchRequest(query)); Number count = (Number) parseContent(response.body(), "count"); return (count != null ? count.longValue() : -1); }
public String postDocument(Resource resource, BytesArray document) throws IOException { Request request = new SimpleRequest(Method.POST, null, resource.index() + "/" + resource.type(), null, document); Response response = execute(request, true); Object id = parseContent(response.body(), "_id"); if (id == null || !StringUtils.hasText(id.toString())) { throw new EsHadoopInvalidRequest( String.format("Could not determine successful write operation. Request[%s > %s] Response[%s]", request.method(), request.path(), IOUtils.asString(response.body()) ) ); } return id.toString(); }
Scroll scroll(String query, BytesArray body, ScrollReader reader) throws IOException { InputStream scroll = client.execute(POST, query, body).body(); try { return reader.read(scroll); } finally { if (scroll instanceof StatsAware) { stats.aggregate(((StatsAware) scroll).stats()); } } }
Scroll scroll(String query, BytesArray body, ScrollReader reader) throws IOException { InputStream scroll = client.execute(POST, query, body).body(); try { return reader.read(scroll); } finally { if (scroll instanceof StatsAware) { stats.aggregate(((StatsAware) scroll).stats()); } } }
Scroll scroll(String query, BytesArray body, ScrollReader reader) throws IOException { InputStream scroll = client.execute(POST, query, body).body(); try { return reader.read(scroll); } finally { if (scroll instanceof StatsAware) { stats.aggregate(((StatsAware) scroll).stats()); } } }