使用 perl 的弹性搜索批量索引

Elastic Search bulk indexing using perl

我已经尝试过批量 API Perl 客户端在 Elasticsearch 中进行内容索引。我在 Bulk Ingestion 行收到错误。请找到以下代码:

my $ifileid=0;
my $dir = '/home/bala/input_files/output';
opendir(DIR, $dir) or die $!;
my @arfiles = readdir (DIR);
closedir(DIR);
print scalar @arfiles." Total files\n";
foreach(@arfiles)
{
    my $file = $_;
    if ($ifileid>1)
    {
    $doc = {index => 'my_index', type => 'blog_post', id => $ifileid, body => {filename => $file, content => 'bala'}}; 
    push @docs, { create => $doc };
    if ($ibulkid==100)
        {       
            # bulk index docs
           my $res = $e->bulk(\@docs);      
           if ( $res->{errors} ) 
           {
            die "Bulk index had issues: " . $json->encode( $res->{errors} );
           }
           $ibulkid=0;      
        }
        $ibulkid++;
    }
    $ifileid++;
}

我收到以下错误:

Error => Not a HASH reference at /usr/local/share/perl5/Search/Elasticsearch/Role/Client/Direct.pm line 15.

上面bulk api的用法是错误的。 bulk 将 hashref 作为输入,其中 body 是对操作和文档数组的引用

例如,这些行中的内容应该有效:

$action = {index => {_index => 'my_index', _type => 'blog_post', _id => $ifileid}};
$doc =  {filename => $file, content => 'bala'};
push @docs, $action;
push @docs,$doc
if ($ibulkid==100)
    {       
        # bulk index docs
        my $res = $e->bulk(body => \@docs);      
       if ( $res->{errors} ) 
       {
        die "Bulk index had issues: " . $json->encode( $res->{errors} );
       }
       $ibulkid=0;      
    }
    $ibulkid++;
}
$ifileid++;