Guzzle - Elasticsearch - 批量 API
Guzzle - Elasticsearch - Bulk API
我正在尝试将 Elasticsearch Bulk API 与 guzzle 一起使用,但我不知道正文的正确格式是什么。
当我用curl做的时候,没有任何问题。
curl -X POST "http://localhost:9200/hakuna/matata/_bulk" -H 'Content-Type: application/json' -d'
{"index": {}}
{"title": "Abc", "category": "Alphabet", "tags": ["premier", "alphabet"], "duration": 40}
{"index": {}}
{"title": "Def", "category": "Alphabet", "tags": ["second", "alphabet"], "duration": 50}
{"index": {}}
{"title": "Ghi", "category": "Alphabet", "tags": ["troisieme", "alphabet"], "duration": 60}
'
当我用 guzzle 做的时候,我总是得到这个错误
Client error: `POST http://localhost:9200/hakuna/matata/_bulk` resul
ted in a `400 Bad Request` response:
{"error":{"root_cause":[{"type":"action_request_validation_exception","reas
on":"Validation Failed: 1: no requests added; (truncated...)
这是php代码
$data = [
json_encode(['index' => []]),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []]),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []]),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'json' => $data,
]);
我试过没有 json_encode 和字符串转换的普通数组,但我总是得到同样的错误。
编辑:最终工作代码
$data = [
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'body' => $data."\n",
]);
您走在正确的道路上,到目前为止,您的努力很好!
你现在需要做的就是在每一行的末尾添加一个换行符包括最后一行,像这样:
$data = [
json_encode(['index' => []]),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []]),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []]),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'body' => $data . '\n'; <--- change this line
]);
我正在尝试将 Elasticsearch Bulk API 与 guzzle 一起使用,但我不知道正文的正确格式是什么。
当我用curl做的时候,没有任何问题。
curl -X POST "http://localhost:9200/hakuna/matata/_bulk" -H 'Content-Type: application/json' -d'
{"index": {}}
{"title": "Abc", "category": "Alphabet", "tags": ["premier", "alphabet"], "duration": 40}
{"index": {}}
{"title": "Def", "category": "Alphabet", "tags": ["second", "alphabet"], "duration": 50}
{"index": {}}
{"title": "Ghi", "category": "Alphabet", "tags": ["troisieme", "alphabet"], "duration": 60}
'
当我用 guzzle 做的时候,我总是得到这个错误
Client error: `POST http://localhost:9200/hakuna/matata/_bulk` resul
ted in a `400 Bad Request` response:
{"error":{"root_cause":[{"type":"action_request_validation_exception","reas
on":"Validation Failed: 1: no requests added; (truncated...)
这是php代码
$data = [
json_encode(['index' => []]),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []]),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []]),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'json' => $data,
]);
我试过没有 json_encode 和字符串转换的普通数组,但我总是得到同样的错误。
编辑:最终工作代码
$data = [
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []], JSON_FORCE_OBJECT),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'body' => $data."\n",
]);
您走在正确的道路上,到目前为止,您的努力很好!
你现在需要做的就是在每一行的末尾添加一个换行符包括最后一行,像这样:
$data = [
json_encode(['index' => []]),
json_encode(['title' => 'Abc', 'category' => 'Alphabet', 'tags' => ['premier', 'alphabet'], 'duration' => 40]),
json_encode(['index' => []]),
json_encode(['title' => 'Def', 'category' => 'Alphabet', 'tags' => ['second', 'alphabet'], 'duration' => 50]),
json_encode(['index' => []]),
json_encode(['title' => 'Ghi', 'category' => 'Alphabet', 'tags' => ['troisieme', 'alphabet'], 'duration' => 60]),
];
$data = join("\n", $data);
$response = $client->post('hakuna/matata/_bulk', [
'headers' => ['Content-Type' => 'application/json'],
'body' => $data . '\n'; <--- change this line
]);