ElasticSearch通过查询删除不在PHP中工作

时间:2017-06-19 05:06:19

标签: php elasticsearch

我正在使用弹性搜索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]

        ]       
    ]

]);

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

这是一个古老的问题,以前的评论在2020年不再起作用:

// You get ds Date param as string
let ds = "2020,2,28,3,44";

// To pass it in Date object, you must
// convert it to array by splitting string
ds = ds.split(',');

// Then pass array to Date object with
// spread operator
const d = new Date(...ds);

// Log
console.log(d);

所以最终的代码是:

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