Elasticsearch 2 + PHP滚动流程问题

时间:2018-10-21 13:35:33

标签: php elasticsearch scroll

嗨,有人可以告诉我如何通过PHP Ruflin \ Elastica库在Elasticsearch 2上执行滚动吗? 根据{{​​3}},对于ES2,第一个滚动请求应导致特定的索引,而下一个请求仅在具有scroll_id参数的情况下不带索引。 所以我写了这段代码:

/** @var Elastica\Client $elastic */
$elastic = $container->getService( 'elastica' );

// This first call works fine. I get the scroll_id.
$elasticScrollData = $elastic->getIndex( 'event' )->request( '_search?scroll=5m', 'GET', ['size' => 500, 'sort' => ['_doc']] )->getData();

$countAll = $elasticScrollData['hits']['total'];

saveToMongo( $elasticScrollData, $countAll, $elastic );


function saveToMongo( $scrollData, $countAll, \Elastica\Client $elastic )
{
    $documents = [];
    foreach ( $scrollData['hits']['hits'] as $item )
    {
        $doc = [];
        $doc['ico'] = (array)$item['_source']['ico'];
        ...         

        $documents[] = $doc;
    }

    try
    {
        saveDataToDb( $documents );
    }
    catch( \Exception $e )
    {
        echo '+++ insert exception: ' . $e->getMessage() . "\n";
    }

    // Here is the problem. It throws me an exception: No enabled connection
    $scrollData = $elastic->request( '_search/scroll', 'GET', ['scroll' => '5m', 'scroll_id' => $scrollData['_scroll_id']] )->getData();

    saveToMongo( $scrollData, $countAll, $offset, $elastic, $mongoCollection );
}

第二次调用Elasticsearch有什么问题?为什么会引发错误:没有启用的连接?希望有人知道我真的不知道。

1 个答案:

答案 0 :(得分:1)

默认情况下,Elastica\Client使用循环连接池来解决连接问题,如果所有连接都消失了,则会在消息ClientException 1中抛出No enabled connection。 / p>

检查PHP脚本和Elasticsearch之间的网络连接,并查看Elasticsearch的配置。