my doc in elsticsearch is like below
"hits" : [
{
"_index" : "MyIndex",
"_type" : "_doc",
"_id" : "Lxh7THMBycWRIeJFdwbZ",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-07-14T13:10:26.087+0430",
"message" : "elk: customer inserted: id=5",
"traceId" : "ABC",
"severity" : "INFO",
"thread" : "http-nio-8095-exec-2"
}
}
]
I want to delete All docs in one index that "traceId" : "ABC" with java code. I use Elasticsearch 7.8 and my client is RestHighLevelClient. please guide me.
The below sample code should help. You can refer to the links below to go through the API and understand what needs to be done in steps.
Basically every request sent via Java would have below two parts.
As for your example, note that I'm assuming that traceId.keyword
is the field of type keyword
and hence I'm using TermBuilder
i.e. Term Query. If it is of type text
I've mentioned Match Query
in the comment, you would have to use that.
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
public class DeleteBasedOnQuery {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
deleteQueryUsingMatch(client);
client.close();
}
private static void deleteQueryUsingMatch(RestHighLevelClient client) throws IOException {
//Mention index name
DeleteByQueryRequest request = new DeleteByQueryRequest("my_delete_index");
request.setConflicts("proceed");
request.setBatchSize(100);
request.setRefresh(true);
//Term Query to Delete the document. Note the field name and value in below code
request.setQuery(new TermQueryBuilder("traceId.keyword", "ABC"));
//Match Query to Delete the Document
//request.setQuery(QueryBuilders.matchQuery("traceID", "abc"));
//Execute the request to delete based on above details
BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
//By this time your delete query got executed and you have the response with you.
long totalDocs = bulkResponse.getTotal();
long deletedDocs = bulkResponse.getDeleted();
//Print the response details
System.out.println("Total Docs Processed :: " + totalDocs);
System.out.println("Total Docs Deleted :: " + deletedDocs);
}
}