3

I have a php console script which processes long array in foreach cycle and send Elasticsearch bulks to database. I need to solve case if bulk insert fails on connection error like: No enabled connection. I would like to catch it and try it again. But my solution does not work. Still no enabled connection error. Here is the code:

/** @var Elastica\Client $elastic */
$elastic = new Elastica\Client(['servers' => [['host' => 'xxx.xxx.x.xx', 'port' => 9200]]]);

/** @var \Elastica\Index $searchCz */
$searchCz = $elastic->getIndex('search_cz');

$bulkLimit = 500;
$scrollIcos = [About 2mil items...];
$continue = TRUE;

while ( $continue && $bulkIcos = array_splice( $scrollIcos, 0, $bulkLimit ) )
{
    try
    {
        $elasticBulk = [];
        $icos = array_values($bulkIcos);

        foreach ($bulkIcos as $ico)  // Numeric key does not work with array_splice()
        {
            $offset++;
            $companyData = [];
            $companyData['ico'] = $ico;
            $companyData['xxx'] = 'yyy';
            ...

            $elasticBulk[] = new \Elastica\Document((string)$ico, $companyData);
        }

        if( $elasticBulk )
        {
            echo 'Bulk send start ' . PHP_EOL;
            $searchCz->getType('doc')->addDocuments($elasticBulk);
            echo '----- offset ' . $offset . ' OK' . PHP_EOL;
        }
    }
    catch( \Exception $e )
    {
        echo '------------------------' . PHP_EOL;
        echo 'Error while bulk insert. ' . $e->getMessage() . PHP_EOL . PHP_EOL;
        setIcosBackToScrollIcos($bulkIcos, $scrollIcos);
        sleep(30);
    }
}
Čamo
  • 3,863
  • 13
  • 62
  • 114

0 Answers0