Search code examples
phpelasticsearchelasticsearch-plugin

Elasticsearch 2.3 Delete all documents in a type by query using PHP LIbrary


I want to be able to delete documents by query using the elasticsearch php library. I actually ideally want to delete all documents in my type. I can achieve this using a normal curl XDELETE request in sense, however I cant get it to work using elastic search's PHP library.

Yes I have installed the delete-by-query plugin hence why raw request works.

my current code:

$params = [
    'index' => 'sanctions_lists',
    'type' => $listName,
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ]
];
return $this->delete($params);

results in

InvalidArgumentException in Client.php line 1440: id cannot be null.

From my error message (ID cannot be null) it appears delete by query could be a limitation of the php library Only allowing deletes by id.

Would be a bit annoying if I have to execute raw HTTP request for this function inside my app when the php library has been really good for my other queries in the app.

Question Summary

Is there a workaround to delete by query using the elasticsearch php library without touching library code?

Goes without saying but thanks to anyone who takes any time to view and help with my question. Cheers.

Edit

Incase it helps im using:

elasticsearch php library version v2.1.5


Solution

  • You need to use the deleteByQuery() function, like this:

    $params = [
        'index' => 'sanctions_lists',
        'type' => $listName,
        'body' => [
            'query' => [
                'match_all' => []
            ]
        ]
    ];
    return $this->deleteByQuery($params);
    

    If you want to have a look the source code is available here