Elasticsearch PHP批量索引性能与索引

时间:2015-05-31 13:45:10

标签: php performance elasticsearch bulk

我使用elasticsearch-php在elasticsearch上运行基准测试。 我将10 000指数逐一比较10 000与1 000份文件的大部分进行比较。

在我的vpn服务器上3核2 Gb内存,无论是否有批量索引,性能都是相同的。

我的PHP代码(受àpost启发):

<?php
set_time_limit(0);  //  no timeout
require 'vendor/autoload.php';
$es = new Elasticsearch\Client([
    'hosts'=>['127.0.0.1:9200']
]);
$max = 10000;

// ELASTICSEARCH BULK INDEX
$temps_debut = microtime(true);
for ($i = 0; $i <=  $max; $i++) {
    $params['body'][] = array(
        'index' => array(
            '_index' => 'articles',
            '_type' => 'article',
            '_id' => 'cle' . $i
        )
    );
    $params['body'][] = array(
        'my_field' => 'my_value' . $i
    );
    if ($i % 1000) {   // Every 1000 documents stop and send the bulk request
        $responses = $es->bulk($params);
        $params = array();  // erase the old bulk request    
        unset($responses); // unset  to save memory
    }
}
$temps_fin = microtime(true);
echo 'Elasticsearch bulk: ' . round($i / round($temps_fin - $temps_debut, 4)) . ' per sec <br>';

// ELASTICSEARCH WITHOUT BULK INDEX
$temps_debut = microtime(true);
        for ($i = 1; $i <= $max; $i++) {    
            $params = array();
            $params['index'] = 'my_index';
            $params['type']  = 'my_type';
            $params['id']    = "key".$i;
            $params['body']  = array('testField' => 'valeur'.$i);
            $ret = $es->index($params);
        }
$temps_fin = microtime(true);
echo 'Elasticsearch One by one : ' . round($i / round($temps_fin - $temps_debut, 4)) . 'per sec <br>';
?>

弹性搜索批量:每秒1209 Elasticsearch逐一:1197per sec

我的批量索引是否有问题以获得更好的性能?

感谢的

1 个答案:

答案 0 :(得分:4)

替换:

if ($i % 1000) {   // Every 1000 documents stop and send the bulk request

with:

if (($i + 1) % 1000 === 0) {   // Every 1000 documents stop and send the bulk request

或者您将查询每个非0值(即1000的999)... 显然,这仅在$max是1000的倍数时才有效。

另外,请更正此错误:

for ($i = 0; $i <=  $max; $i++) {

将遍历$max + 1个项目。将其替换为:

for ($i = 0; $i < $max; $i++) {

初始化$params的方式可能也有问题。你不应该在循环之外设置它,只在每个$params['body']之后清理->bulk()吗?当您使用$params = array();重置时,您将全部松开。

另外,请记住ES可能分布在群集上。然后可以将批量操作分发到甚至工作负载。因此,在单个物理节点上看不到某些性能扩展。