Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jclosure/806a84c8671528b6231d5f7e7183c783 to your computer and use it in GitHub Desktop.
Save jclosure/806a84c8671528b6231d5f7e7183c783 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 $!;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment