gpt4 book ai didi

php - uzz-Elasticsearch-批量API

转载 作者:行者123 更新时间:2023-12-02 22:16:31 25 4
gpt4 key购买 nike

我正在尝试使用带有 flex 的Elasticsearch Bulk API,但我不知道该主体的正确格式是什么。

当我 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}
'

当我大吃一惊时,我总是会收到此错误
 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
]);

关于php - uzz-Elasticsearch-批量API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com