ElasticSearchClient: Add `deleteById` API. (#11305)

This commit is contained in:
Wan Kai 2023-09-05 17:20:43 +08:00 committed by GitHub
parent 52d5b2e38f
commit 8e529ee956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 106 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#### OAP Server
* ElasticSearchClient: Add `deleteById` API.
#### UI

View File

@ -346,6 +346,12 @@ public class ElasticSearchClient implements Client, HealthCheckable {
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);

View File

@ -169,7 +169,8 @@ public class ElasticSearchIT {
.next()
.getSource()
.get("message"));
client.deleteById(indexName, id);
Assertions.assertFalse(client.existDoc(indexName, id));
client.shutdown();
server.stop();
}

View File

@ -157,4 +157,26 @@ public final class DocumentClient {
});
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();
}
}

View File

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

View File

@ -198,4 +198,23 @@ final class V6DocumentFactory implements DocumentFactory {
.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();
}
}

View File

@ -191,4 +191,22 @@ final class V7DocumentFactory implements DocumentFactory {
.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();
}
}

View File

@ -389,4 +389,37 @@ public class ElasticSearchIT {
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();
}
}