简体   繁体   中英

Guzzle - Elasticsearch - Bulk API

I'm trying to use Elasticsearch Bulk API with guzzle but I don't know what is the correct format for the body.

When I do it with curl, I don't have any problem.

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

When I do it with guzzle, I always get this error

 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...)    

This is the php code

        $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,
        ]);

I've tried with a normal array without json_encode and without the string transformation, but I always get the same error.

EDIT : The final working code

        $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",
        ]);

You're on the right path, nice effort so far!

All you need to do now is to add a newline character at the end of each line including the last one , like this:

    $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
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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