• 周六. 7 月 27th, 2024

    ElasticSearch-php 库批量更新ES

    root

    11 月 17, 2021

    批量更新

    $params = ['body' => []];
    
    for ($i = 1; $i <= 1234567; $i++) {
        $params['body'][] = [
            'index' => [     //注意这里的动作为index, 表示库中有就更新,没有就创建
                '_index' => 'my_index',   //注意这里的字段前缀是_
                '_id'    => $i
            ]
        ];
    
        $params['body'][] = [
            'my_field'     => 'my_value',
            'second_field' => 'some more values'
        ];
    
        // Every 1000 documents stop and send the bulk request
        if ($i % 1000 == 0) {
            $responses = $client->bulk($params);
    
            // erase the old bulk request
            $params = ['body' => []];
    
            // unset the bulk response when you are done to save memory
            unset($responses);
        }
    }
    
    // Send the last batch if it exists
    if (!empty($params['body'])) {
        $responses = $client->bulk($params);
    }

    root