使用perl进行弹性搜索批量索引

时间:2015-06-23 12:21:06

标签: perl elasticsearch bulkinsert

我在Elasticsearch中尝试过批量API Perl客户端进行内容索引。我在Bulk Ingestion行上遇到Error。请找到以下代码:

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.

1 个答案:

答案 0 :(得分:0)

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++;