0

I am using Elasticsearch 7.10 and want to delete a document inside an index using Delete By Query. I have referenced the official documentation on how to use the library but not sure what exactly i am missing. Any help is appreciated.

    String indexName = "test_delete_index";

    Map<String, String> testDoc = new HashMap<>();
    testDoc.put("user", "abc");
    testDoc.put("postDate", new Date().toString());
    testDoc.put("message", "Test the Delete Method with Version");
    testDoc.put("gender", "male");

    DeleteByQueryRequest request = new DeleteByQueryRequest(indexName);
    request.setConflicts("proceed");
    request.setRefresh(true);
    request.setQuery(new TermQueryBuilder ("gender","male"));

    BulkByScrollResponse deleteResponse = client.deleteByQuery(request,RequestOptions.DEFAULT);

    System.out.println("Delete By Query ::: " + deleteResponse.getDeleted());

The output says nothing got deleted :

Delete By Query ::: 0

Tarun Pande
  • 371
  • 1
  • 6
  • 18

1 Answers1

0

I first indexed the doc "testDoc" and deleteByquery worked.

String indexName = "test_delete_index";

    Map<String, String> testDoc = new HashMap<>();
    testDoc.put("user", "abc");
    testDoc.put("postDate", new Date().toString());
    testDoc.put("message", "Test the Delete Method with Version");
    testDoc.put("gender", "male");

    IndexRequest indexRequest = new IndexRequest(indexName);
    indexRequest.source(testDoc);

    IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);

    System.out.println("Insert Doc ::: " + response.toString());

    DeleteByQueryRequest request = new DeleteByQueryRequest(indexName);
    request.setConflicts("proceed");
    request.setRefresh(true);
    request.setQuery(new TermQueryBuilder("gender", "male"));

    BulkByScrollResponse deleteResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);

    System.out.println("Delete By Query ::: " + deleteResponse.getDeleted());
rabbitbr
  • 2,991
  • 2
  • 4
  • 17
  • I still see the document is not deleted. Delete By Query ::: 0. I forgot to mention that i have created the index after setting the map values. – Tarun Pande Jun 10 '22 at 22:53
  • Try "gender.keyword". Maybe it could be something with your mapping, if you can share it too. – rabbitbr Jun 10 '22 at 23:01
  • It doesn't work with "gender.keyword". Mapping is very simple: { "properties": { "index_name": { "type": "keyword", "store": "false", "index": "true" }, "gender": { "type": "keyword", "store": "true", "index": "true" } } } – Tarun Pande Jun 10 '22 at 23:14
  • If you run the example I posted it still doesn't delete? – rabbitbr Jun 11 '22 at 00:05
  • No, I still see the deleted count as 0. What is the mapping document are you using. I can try with that. – Tarun Pande Jun 11 '22 at 00:35