Skip to content

Commit

Permalink
ElasticSearchClient: Add deleteById API. (apache#11305)
Browse files Browse the repository at this point in the history
  • Loading branch information
wankai123 authored Sep 5, 2023
1 parent 52d5b2e commit 8e529ee
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/en/changes/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#### OAP Server

* ElasticSearchClient: Add `deleteById` API.


#### UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ public void forceUpdate(String indexName, String id, Map<String, Object> source)
es.get().documents().update(wrapper.getRequest(), params);
}

public void deleteById(String indexName, String id) {
indexName = indexNameConverter.apply(indexName);
Map<String, Object> params = ImmutableMap.of("refresh", "true");
es.get().documents().deleteById(indexName, TYPE, id, params);
}

public IndexRequestWrapper prepareInsert(String indexName, String id,
Map<String, Object> source) {
return prepareInsert(indexName, id, Optional.empty(), source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public void documentOperate(final ElasticsearchContainer server,
.next()
.getSource()
.get("message"));

client.deleteById(indexName, id);
Assertions.assertFalse(client.existDoc(indexName, id));
client.shutdown();
server.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,26 @@ public void update(UpdateRequest request, Map<String, Object> params) {
});
future.join();
}

@SneakyThrows
public void deleteById(String index, String type, String id, Map<String, Object> params) {
final CompletableFuture<Void> future = version.thenCompose(
v -> client.execute(v.requestFactory().document().deleteById(index, type, id, params))
.aggregate().thenAccept(response -> {
final HttpStatus status = response.status();
if (status != HttpStatus.OK) {
throw new RuntimeException(response.contentUtf8());
}
}));
future.whenComplete((result, exception) -> {
if (exception != null) {
log.error("Failed to delete doc by id {} in index {}, params: {}", id, index, params, exception);
return;
}
if (log.isDebugEnabled()) {
log.debug("Succeeded delete doc by id {} in index {}, params: {}", id, params, index);
}
});
future.join();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public interface DocumentFactory {
*/
HttpRequest delete(String index, String type, Query query,
Map<String, ?> params);

/**
* Returns a request to delete documents matching the given {@code id} in {@code index}.
*/
HttpRequest deleteById(String index, String type, String id, Map<String, ?> params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,23 @@ public HttpRequest delete(String index, String type, Query query,
.content(MediaType.JSON, content)
.build();
}

@Override
public HttpRequest deleteById(final String index, final String type, final String id, Map<String, ?> params) {
checkArgument(!isNullOrEmpty(index), "index cannot be null or empty");
checkArgument(!isNullOrEmpty(type), "type cannot be null or empty");
checkArgument(!isNullOrEmpty(id), "id cannot be null or empty");

final HttpRequestBuilder builder = HttpRequest.builder();
if (params != null) {
params.forEach(builder::queryParam);
}

return builder
.delete("/{index}/{type}/{id}")
.pathParam("index", index)
.pathParam("type", type)
.pathParam("id", id)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,22 @@ public HttpRequest delete(String index, String type, Query query,
.content(MediaType.JSON, content)
.build();
}

@Override
public HttpRequest deleteById(final String index, final String type, final String id, Map<String, ?> params) {
checkArgument(!isNullOrEmpty(index), "index cannot be null or empty");
checkArgument(!isNullOrEmpty(type), "type cannot be null or empty");
checkArgument(!isNullOrEmpty(id), "id cannot be null or empty");

final HttpRequestBuilder builder = HttpRequest.builder();
if (params != null) {
params.forEach(builder::queryParam);
}

return builder
.delete("/{index}/_doc/{id}")
.pathParam("index", index)
.pathParam("id", id)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,37 @@ public void testSearch(final String ignored,

server.close();
}

@ParameterizedTest(name = "version: {0}")
@MethodSource("es")
public void testDocDeleteById(final String ignored,
final ElasticsearchContainer server) {
server.start();

final ElasticSearch client =
ElasticSearch.builder()
.endpoints(server.getHttpHostAddress())
.build();
client.connect();

final String index = "test-index-delete";
assertTrue(client.index().create(index, null, null));

final ImmutableMap<String, Object> doc = ImmutableMap.of("key", "val");
final String idWithSpace = "an id"; // UI management templates' IDs contains spaces
final String type = "type";

client.documents().index(
IndexRequest.builder()
.index(index)
.type(type)
.id(idWithSpace)
.doc(doc)
.build(), null);

assertTrue(client.documents().exists(index, type, idWithSpace));
client.documents().deleteById(index, type, idWithSpace, ImmutableMap.of("refresh", "true"));
assertFalse(client.documents().exists(index, type, idWithSpace));
server.close();
}
}

0 comments on commit 8e529ee

Please sign in to comment.