【问题标题】:ElasticSearch - Bulk Indexing with refreshElasticSearch - 批量索引刷新
【发布时间】:2019-03-29 22:40:17
【问题描述】:

我需要尽快索引大约 1000 个文档。我决定使用比我原来的解决方案快 10 倍的批量功能。我需要在索引结束后立即创建refresh 以使文档可搜索。在其他情况下,我会使用刷新参数 'refresh' => true, 但我无法使其在 PHP 中批量处理。我用code from official documentation。

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 批量函数中使用刷新的正确方法是什么?

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    我在批量更新后使用了假更新操作

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

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

    【讨论】:

    • 所以不是每次都调用刷新,而是在所有批量数据都被索引后调用
    【解决方案2】:

    只需添加

    $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);
    

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2016-04-06
      • 1970-01-01
      • 2017-08-28
      • 2016-01-25
      • 1970-01-01
      相关资源
      最近更新 更多