简体   繁体   English

Elasticsearch 2.3通过使用PHP库进行查询来删除类型中的所有文档

[英]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. 我希望能够使用elasticsearch php库通过查询删除文档。 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. 我可以使用普通的curl XDELETE请求来实现此目的,但是我无法使用弹性搜索的PHP库来使其正常工作。

Yes I have installed the delete-by-query plugin hence why raw request works. 是的,我已经安装了delete-by-query插件,因此为什么原始请求有效。

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. Client.php第1440行中的InvalidArgumentException:id不能为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. 从我的错误消息(ID不能为空)中,似乎通过查询删除可能是php库的限制。仅允许通过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. 如果我必须在应用程序内部执行此函数的原始HTTP请求,而php库对于应用程序中的其他查询确实非常好,那将有点烦人。

Question Summary 问题总结

Is there a workaround to delete by query using the elasticsearch php library without touching library code? 有没有变通办法,可以通过使用Elasticsearch php库通过查询删除而不触及库代码?

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 v2.1.5 php库版本v2.1.5

You need to use the deleteByQuery() function , like this: 您需要使用deleteByQuery()函数 ,如下所示:

$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 如果您想看一下源代码,请点击这里

$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. 根据@Val的答案,U可能会遇到错误,因此请注意“ match_all” =>(object)[]重新键入。 I found that here 我发现在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM