【问题标题】:ElasticSearch Delete by query not working in PHPElasticSearch 通过查询删除在 PHP 中不起作用
【发布时间】:2020-07-26 23:12:21
【问题描述】:

我使用的是 Elastic search 5.x,以下代码运行良好:

curl -XPOST "http://localhost:9200/test_index/test_info/_delete_by_query" -d'
{
  "query": {
    "match": {
        "category_id": "21"
    }
  }
}'

但是当我在我的 php 代码中尝试同样的方法时,它不起作用:

$client->deleteByQuery([
'index' => 'test_index',
'type'  => 'test_info',

    'query' => [
        'match' => [
                ['category_id' => 21]

        ]       
    ]

]);

【问题讨论】:

  • 您使用的是哪个版本的elasticsearch-php 库?
  • @Val 我使用的是 elasticsearch-php 2.0 版
  • 好了,你需要使用版本 5 就可以了。
  • @Val ...谢谢...如何在 2.0 版中使用它?
  • 看到这个答案:stackoverflow.com/questions/37382980/… 你至少需要 2.4 版

标签: php elasticsearch


【解决方案1】:

您需要在body 参数数组中提供您的query 数组:

$client->deleteByQuery([
    'index' => 'test_index',
    'type' => 'test_info',
    'body' => [
        'query' => [
            'match' => [
                ['category_id' => 21]
            ]
        ]
    ]
]);

【讨论】:

  • 没有类型也是可能的 (php 7.2 - ES 7.3) ['index' => 'test_index','body' => ['query' => ['match' => ['keyword ' => '123456']]],];
【解决方案2】:

这是一个老问题,以前的 cmets 在 2020 年不再工作了:

$client->deleteByQuery([
    'index' => 'test_index',
   (there were a type here)  'type' => 'test_info',
    'body' => [
        'query' => [
            'match' => [
                (there were an array here) ['category_id' => 21]
            ]
        ]
    ]
]);

所以最后的代码是:

$client->deleteByQuery([
    'index' => 'test_index',
    'body' => [
        'query' => [
            'match' => [
               'category_id' => 21
            ]
        ]
    ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2015-01-05
    • 2015-01-04
    • 2021-10-12
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多