diff --git a/vpro-shared-elasticsearch-client/src/main/java/nl/vpro/elasticsearchclient/IndexHelper.java b/vpro-shared-elasticsearch-client/src/main/java/nl/vpro/elasticsearchclient/IndexHelper.java index ebb9044a6..f07e19709 100644 --- a/vpro-shared-elasticsearch-client/src/main/java/nl/vpro/elasticsearchclient/IndexHelper.java +++ b/vpro-shared-elasticsearch-client/src/main/java/nl/vpro/elasticsearchclient/IndexHelper.java @@ -590,7 +590,10 @@ public final ObjectNode sendEntity(String method, String path, HttpEntity entity c.accept(req); } log.debug("Posting to {}", path); - return read(client().performRequest(req)); + + var response = client().performRequest(req); + + return read(response); } catch (IOException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); @@ -688,7 +691,7 @@ public ObjectNode index(String id, Object o) { public ObjectNode index(String id, byte[] o) { HttpEntity entity = entity(o); - return postEntity(indexPath(id), entity(o)); + return putEntity(indexPath(id), entity); } public ObjectNode index(String id, Object o, Consumer sourceConsumer) { @@ -701,7 +704,7 @@ public ObjectNode index(String id, Object o, Consumer sourceConsumer /** */ public ObjectNode indexWithRouting(String id, byte[] o, String routing) { - return postEntity(indexPath(id), entity(o), req -> req.addParameter(ROUTING, routing)); + return putEntity(indexPath(id), entity(o), req -> req.addParameter(ROUTING, routing)); } /** @@ -734,7 +737,7 @@ protected String indexPath(String id) { } protected String _indexPath(String type, @Nullable String id, @Nullable String parent) { - String path = getIndexName() + "/" + type + (id == null ? "" : ("/" + encode(id))); + String path = '/' + getIndexName() + "/" + type + (id == null ? "" : ("/" + encode(id))); if (parent != null) { path += "?parent=" + parent; } @@ -811,7 +814,7 @@ public Optional get(String id){ ArrayNode docs = objectNode.withArray("docs"); for (JsonNode n : docs) { - boolean found = n.get("found").asBoolean(); + boolean found = n.has("found") && n.get("found").asBoolean(); if (found) { result.add(Optional.of(n)); } else { @@ -1112,7 +1115,11 @@ protected static HttpEntity bulkEntity(Collection request) { static protected String saveToString(JsonNode jsonNode) { String value = jsonNode.toString(); return value.replaceAll("\\p{Cc}", ""); + } + @SneakyThrows + static protected byte[] saveToArray(JsonNode jsonNode) { + return LENIENT.writer().writeValueAsBytes(jsonNode); } @Override