Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created March 16, 2012 10:50
Show Gist options
  • Save clintongormley/2049562 to your computer and use it in GitHub Desktop.
Save clintongormley/2049562 to your computer and use it in GitHub Desktop.
Elasticsearch to CSV
#!/usr/bin/perl
use ElasticSearch;
use Text::CSV_XS;
my $csv_file = 'output.csv';
open my $fh, '>:encoding(utf8)', $csv_file or die $!;
my $csv = Text::CSV_XS->new;
my $e = ElasticSearch->new(servers => '127.0.0.1:9200');
my $s = $e->scrolled_search(
index => 'foo',
type => 'bar',
query => {....}
);
my @field_names = qw(title name foo bar);
while (my $doc = $s->next) {
my @cols = map {$doc->{$_} @field_names;
$csv->print($fh, \@cols);
}
close $fh or die $!;
@hrchu
Copy link

hrchu commented Jun 4, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment