首页 > 解决方案 > PHP Ruflin/Elastica - 如何在大量数据插入时刷新索引

问题描述

我需要向 Elasticsearch 数据库中插入大约 150 万个文档。我根据这个例子(BULK 例子)通过 PHP 库 Elastica 来做

我想知道是否可以在批量插入的最后使用调用命令,以及它是否比每次批量发送后$elasticaType->getIndex()->refresh();调用更安全、更快。$elasticaType->getIndex()->refresh();我的意思是这样的:

$offset = 0;
$limit = 500;
$sum = 1500000,

while( $offset < $sum )
{        
    $documents = [];
    $rows = $sqlDatabase->getData( $offset, $limit )

    foreach( $rows as $row )
    {
        $docData = ['name' => $row->name, 'email' => $row->email]
        $documents[] = new \Elastica\Document( $data->id, $docData );
    }

    $elasticaType->addDocuments( $documents );
    $offset += 500;
    // Source example has refresh here. After every 500 items. But I wont it at the very end of the code after all 1500000 item are in the database.
    // $elasticaType->getIndex()->refresh();
}

$elasticaType->getIndex()->refresh();  // This is what I want.

是否可以将 1500000 个文档插入到 elasticsearch 然后调用$elasticaType->getIndex()->refresh();

标签: phpelasticsearchindexingelastica

解决方案


是否可以将1500000个文档插入elasticsearch,然后调用$elasticaType->getIndex()->refresh();?

肯定是的。

刷新使您的文档可用于搜索,此机制源自 Apache Lucene,以提供近实时 (NRT) 搜索功能,它使用DirectoryReader.openIfChanged重新打开索引。

通常您不必自己做,默认情况下会定期安排刷新,您可以将refresh_interval的值更改为更短的 NRT 搜索时间,或更长的性能。


推荐阅读