Last active
December 10, 2017 04:41
-
-
Save karpet/a4a0dae27fb5360fc24b16f6de0e9e85 to your computer and use it in GitHub Desktop.
example Dezi::Client indexer for TSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Dezi::Client; | |
use Dezi::Doc; | |
my $BATCH_SIZE = 1000; # number of docs to buffer before committing. | |
if ( $#ARGV != 0 ) { | |
die "$0 inputfile.tsv"; | |
} | |
my $client = Dezi::Client->new( server => 'http://localhost:5000' ); | |
my $inputfile = $ARGV[0]; | |
my $count = 0; | |
open( RF1, "$inputfile" ) or die "Can't open < $inputfile: $!"; | |
my $header = <RF1>; # read out header line | |
while ( my $line = <RF1> ) { | |
chomp $line; | |
$count++; | |
my @array = split( /\t/, $line ); | |
my $doc = Dezi::Doc->new( uri => $count, ); | |
$doc->set_field( 'shopid' => $array[0] ); | |
$doc->set_field( 'prodtype' => $array[1] ); | |
$doc->set_field( 'prodid' => $array[2] ); | |
$doc->set_field( 'prodname' => $array[3] ); | |
my $resp = $client->index($doc); | |
if ( !$resp->is_success ) { | |
die "Failed to add $doc to the Dezi index!"; | |
} | |
$client->commit() if $count % $BATCH_SIZE == 0; | |
} | |
close(RF1); | |
$client->commit(); # final commit to catch any lingering buffer. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment