| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # This file is part of ElasticSearchX-Model | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # This software is Copyright (c) 2016 by Moritz Onken. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # This is free software, licensed under: | 
| 7 |  |  |  |  |  |  | # | 
| 8 |  |  |  |  |  |  | #   The (three-clause) BSD License | 
| 9 |  |  |  |  |  |  | # | 
| 10 |  |  |  |  |  |  | package ElasticSearchX::Model::Role; | 
| 11 |  |  |  |  |  |  | $ElasticSearchX::Model::Role::VERSION = '1.0.2'; | 
| 12 | 11 |  |  | 11 |  | 40525 | use Moose::Role; | 
|  | 11 |  |  |  |  | 18 |  | 
|  | 11 |  |  |  |  | 78 |  | 
| 13 | 11 |  |  | 11 |  | 37416 | use Search::Elasticsearch; | 
|  | 11 |  |  |  |  | 20 |  | 
|  | 11 |  |  |  |  | 216 |  | 
| 14 | 11 |  |  | 11 |  | 41 | use ElasticSearchX::Model::Index; | 
|  | 11 |  |  |  |  | 12 |  | 
|  | 11 |  |  |  |  | 207 |  | 
| 15 | 11 |  |  | 11 |  | 1694 | use version; | 
|  | 11 |  |  |  |  | 5476 |  | 
|  | 11 |  |  |  |  | 69 |  | 
| 16 | 11 |  |  | 11 |  | 652 | use ElasticSearchX::Model::Document::Types qw(ES); | 
|  | 11 |  |  |  |  | 17 |  | 
|  | 11 |  |  |  |  | 83 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has es => ( is => 'rw', lazy_build => 1, coerce => 1, isa => ES ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub _build_es { | 
| 21 |  |  |  |  |  |  | Search::Elasticsearch->new( | 
| 22 | 0 |  | 0 | 0 |  | 0 | nodes => $ENV{ES} || '127.0.0.1:9200', | 
| 23 |  |  |  |  |  |  | cxn => 'HTTPTiny', | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub deploy { | 
| 28 | 0 |  |  | 0 | 0 | 0 | my ( $self, %params ) = @_; | 
| 29 | 0 |  |  |  |  | 0 | my $t = $self->es->transport; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 0 |  |  |  |  | 0 | foreach my $name ( $self->meta->get_index_list ) { | 
| 32 | 0 |  |  |  |  | 0 | my $index = $self->index($name); | 
| 33 | 0 | 0 | 0 |  |  | 0 | next if ( $index->alias_for && $name eq $index->alias_for ); | 
| 34 | 0 | 0 |  |  |  | 0 | $name = $index->alias_for if ( $index->alias_for ); | 
| 35 | 0 |  |  |  |  | 0 | local $@; | 
| 36 | 0 |  |  |  |  | 0 | eval { $self->es->indices->delete( index => $name ) } | 
| 37 | 0 | 0 |  |  |  | 0 | if ( $params{delete} ); | 
| 38 | 0 |  |  |  |  | 0 | my $dep     = $index->deployment_statement; | 
| 39 | 0 |  |  |  |  | 0 | my $mapping = delete $dep->{mappings}; | 
| 40 | 0 |  |  |  |  | 0 | eval { | 
| 41 | 0 |  |  |  |  | 0 | $t->perform_request( | 
| 42 |  |  |  |  |  |  | { | 
| 43 |  |  |  |  |  |  | method => 'PUT', | 
| 44 |  |  |  |  |  |  | path   => "/$name", | 
| 45 |  |  |  |  |  |  | body   => $dep, | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | ); | 
| 48 |  |  |  |  |  |  | }; | 
| 49 | 0 |  |  |  |  | 0 | sleep(1); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  | 0 | while ( my ( $k, $v ) = each %$mapping ) { | 
| 52 | 0 |  |  |  |  | 0 | $t->perform_request( | 
| 53 |  |  |  |  |  |  | { | 
| 54 |  |  |  |  |  |  | method => 'PUT', | 
| 55 |  |  |  |  |  |  | path   => "/$name/$k/_mapping", | 
| 56 |  |  |  |  |  |  | body   => { $k => $v }, | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | ); | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 0 | 0 |  |  |  | 0 | if ( my $alias = $index->alias_for ) { | 
| 61 |  |  |  |  |  |  | my @aliases = keys %{ | 
| 62 | 0 | 0 |  |  |  | 0 | $self->es->indices->get_aliases( | 
|  | 0 |  |  |  |  | 0 |  | 
| 63 |  |  |  |  |  |  | index  => $index->name, | 
| 64 |  |  |  |  |  |  | ignore => [404] | 
| 65 |  |  |  |  |  |  | ) | 
| 66 |  |  |  |  |  |  | || {} | 
| 67 |  |  |  |  |  |  | }; | 
| 68 |  |  |  |  |  |  | my $actions = [ | 
| 69 |  |  |  |  |  |  | ( | 
| 70 |  |  |  |  |  |  | map { | 
| 71 | 0 |  |  |  |  | 0 | { remove => { index => $_, alias => $index->name } } | 
|  | 0 |  |  |  |  | 0 |  | 
| 72 |  |  |  |  |  |  | } @aliases | 
| 73 |  |  |  |  |  |  | ), | 
| 74 |  |  |  |  |  |  | { add => { index => $alias, alias => $index->name } } | 
| 75 |  |  |  |  |  |  | ]; | 
| 76 | 0 |  |  |  |  | 0 | $self->es->indices->update_aliases( | 
| 77 |  |  |  |  |  |  | body => { actions => $actions } ); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  | } | 
| 80 | 0 |  |  |  |  | 0 | return 1; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub bulk { | 
| 84 | 1 |  |  | 1 | 0 | 181897 | my $self = shift; | 
| 85 | 1 |  |  |  |  | 42 | return ElasticSearchX::Model::Bulk->new( es => $self->es, @_ ); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub es_version { | 
| 89 | 4 |  |  | 4 | 0 | 136739 | my $self   = shift; | 
| 90 | 4 |  |  |  |  | 134 | my $string = $self->es->info->{version}->{number}; | 
| 91 | 4 |  |  |  |  | 213 | $string =~ s/RC//g; | 
| 92 | 4 |  |  |  |  | 75 | return version->parse($string)->numify; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | 1; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | __END__ | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =pod | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =encoding UTF-8 | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =head1 NAME | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | ElasticSearchX::Model::Role | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 VERSION | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | version 1.0.2 | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head1 AUTHOR | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | Moritz Onken | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | This software is Copyright (c) 2016 by Moritz Onken. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | This is free software, licensed under: | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | The (three-clause) BSD License | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =cut |