Elasticsearch-刷新批量索引



我需要尽快索引约1000个文档。我决定使用比原始解决方案快10倍的散装功能。索引结束后,我需要立即进行刷新,以使文档可搜索。在其他情况下,我会使用刷新参数'refresh'=> true,,但我不能在PHP中与批量合作。我使用官方文档中的代码。

for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
            '_type' => 'my_type',
        ]
    ];
    $params['body'][] = [
        'my_field' => 'my_value',
        'second_field' => 'some more values'
    ];
}
$responses = $client->bulk($params);

在PHP批量功能中使用刷新的正确方法是什么?

我在散装之后使用了假更新操作与刷新

$params = [
    'index' => 'my_index',
    'type' => 'refresh',
    'id' => 'refresh',
    'refresh' => true,             // REFRESH
    'body' => []
];
$client->index($params);

这不是最好的方法,而是唯一对我有用的方法。

只需添加

$params['refresh'] = true;

您的循环和批量插入之前。

因此,最终代码将是

for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
            '_type' => 'my_type',
        ]
    ];
    $params['body'][] = [
        'my_field' => 'my_value',
        'second_field' => 'some more values'
    ];
}
$params['refresh'] = true;
$responses = $client->bulk($params);

最新更新