Deleting by query

In the Deleting a document recipe from Chapter 4, Basic Operations, we saw how to delete a document(). A document can be deleted very quickly, but it requires you to know the document ID.

ElasticSearch provides a call to delete all the documents that match a query.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script (chapter_05/populate_query.sh) available in the code bundle for this book.

How to do it...

In order to execute a DELETE by query, perform the following steps:

  1. Using the command line, execute a query, as follows:
    curl -XDELETE 'http://127.0.0.1:9200/test-index/test-type/_query?pretty=true' -d '{"query":{"match_all":{}}}'
    
  2. The following result should be returned by ElasticSearch, ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.