嗨,有人能告诉我如何通过php ruflin\elastica库在elasticsearch 2上执行滚动吗?
根据
documentation
对于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有什么问题?为什么抛出错误:没有启用的连接?希望有人知道,因为我真的不知道。