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

亡梦爱人 提交于 2020-01-17 06:04:35

问题


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


回答1:


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




回答2:


$params = [
    "index" => "my_index",
    "type" => "my_type",
    "body" => [
        "query" => [
            "match_all" => (object)[]
        ]
    ]
];

$client->deleteByQuery($params);

According to @Val 's answer, U may experience error, so note "match_all" => (object)[] retyping. I found that here



来源:https://stackoverflow.com/questions/37382980/elasticsearch-2-3-delete-all-documents-in-a-type-by-query-using-php-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!