| 1 | package Search::Elasticsearch::Client::6_0::Role::Bulk; | ||
| 2 | $Search::Elasticsearch::Client::6_0::Role::Bulk::VERSION = '6.80'; | ||
| 3 | use Moo::Role; | ||
| 4 | requires 'add_action', 'flush'; | ||
| 5 | |||
| 6 | use Search::Elasticsearch::Util qw(parse_params throw); | ||
| 7 | use namespace::clean; | ||
| 8 | |||
| 9 | has 'es' => ( is => 'ro', required => 1 ); | ||
| 10 | has 'max_count' => ( is => 'rw', default => 1_000 ); | ||
| 11 | has 'max_size' => ( is => 'rw', default => 1_000_000 ); | ||
| 12 | has 'max_time' => ( is => 'rw', default => 0 ); | ||
| 13 | |||
| 14 | has 'on_success' => ( is => 'ro', default => 0 ); | ||
| 15 | has 'on_error' => ( is => 'lazy' ); | ||
| 16 | has 'on_conflict' => ( is => 'ro', default => 0 ); | ||
| 17 | has 'verbose' => ( is => 'rw' ); | ||
| 18 | |||
| 19 | has '_buffer' => ( is => 'ro', default => sub { [] } ); | ||
| 20 | has '_buffer_size' => ( is => 'rw', default => 0 ); | ||
| 21 | has '_buffer_count' => ( is => 'rw', default => 0 ); | ||
| 22 | has '_serializer' => ( is => 'lazy' ); | ||
| 23 | has '_bulk_args' => ( is => 'ro' ); | ||
| 24 | has '_last_flush' => ( is => 'rw', default => sub {time} ); | ||
| 25 | has '_metadata_params' => ( is => 'ro' ); | ||
| 26 | has '_update_params' => ( is => 'ro' ); | ||
| 27 | has '_required_params' => ( is => 'ro' ); | ||
| 28 | |||
| 29 | our %Actions = ( | ||
| 30 | 'index' => 1, | ||
| 31 | 'create' => 1, | ||
| 32 | 'update' => 1, | ||
| 33 | 'delete' => 1 | ||
| 34 | ); | ||
| 35 | |||
| 36 | #=================================== | ||
| 37 | sub _build__serializer { shift->es->transport->serializer } | ||
| 38 | #=================================== | ||
| 39 | |||
| 40 | #=================================== | ||
| 41 | sub _build_on_error { | ||
| 42 | #=================================== | ||
| 43 | my $self = shift; | ||
| 44 | my $serializer = $self->_serializer; | ||
| 45 | return sub { | ||
| 46 | my ( $action, $result, $src ) = @_; | ||
| 47 | warn( "Bulk error [$action]: " . $serializer->encode($result) ); | ||
| 48 | }; | ||
| 49 | } | ||
| 50 | |||
| 51 | #=================================== | ||
| 52 | sub BUILDARGS { | ||
| 53 | #=================================== | ||
| 54 | my ( $class, $params ) = parse_params(@_); | ||
| 55 | my $es = $params->{es} or throw( 'Param', 'Missing required param <es>' ); | ||
| 56 | $params->{_metadata_params} = $es->api('bulk.metadata')->{params}; | ||
| 57 | $params->{_update_params} = $es->api('bulk.update')->{params}; | ||
| 58 | $params->{_required_params} = $es->api('bulk.required')->{params}; | ||
| 59 | my $bulk_spec = $es->api('bulk'); | ||
| 60 | my %args; | ||
| 61 | for ( keys %{ $bulk_spec->{qs} }, keys %{ $bulk_spec->{parts} } ) { | ||
| 62 | $args{$_} = delete $params->{$_} | ||
| 63 | if exists $params->{$_}; | ||
| 64 | } | ||
| 65 | $params->{_bulk_args} = \%args; | ||
| 66 | return $params; | ||
| 67 | } | ||
| 68 | |||
| 69 | #=================================== | ||
| 70 | sub index { | ||
| 71 | #=================================== | ||
| 72 | shift->add_action( map { ( 'index' => $_ ) } @_ ); | ||
| 73 | } | ||
| 74 | |||
| 75 | #=================================== | ||
| 76 | sub create { | ||
| 77 | #=================================== | ||
| 78 | shift->add_action( map { ( 'create' => $_ ) } @_ ); | ||
| 79 | } | ||
| 80 | |||
| 81 | #=================================== | ||
| 82 | sub delete { | ||
| 83 | #=================================== | ||
| 84 | shift->add_action( map { ( 'delete' => $_ ) } @_ ); | ||
| 85 | } | ||
| 86 | |||
| 87 | #=================================== | ||
| 88 | sub update { | ||
| 89 | #=================================== | ||
| 90 | shift->add_action( map { ( 'update' => $_ ) } @_ ); | ||
| 91 | } | ||
| 92 | |||
| 93 | #=================================== | ||
| 94 | sub create_docs { | ||
| 95 | #=================================== | ||
| 96 | my $self = shift; | ||
| 97 | $self->add_action( map { ( 'create' => { source => $_ } ) } @_ ); | ||
| 98 | } | ||
| 99 | |||
| 100 | #=================================== | ||
| 101 | sub delete_ids { | ||
| 102 | #=================================== | ||
| 103 | my $self = shift; | ||
| 104 | $self->add_action( map { ( 'delete' => { _id => $_ } ) } @_ ); | ||
| 105 | } | ||
| 106 | |||
| 107 | #=================================== | ||
| 108 | sub _encode_action { | ||
| 109 | #=================================== | ||
| 110 | my $self = shift; | ||
| 111 | my $action = shift || ''; | ||
| 112 | my $orig = shift; | ||
| 113 | |||
| 114 | throw( 'Param', "Unrecognised action <$action>" ) | ||
| 115 | unless $Actions{$action}; | ||
| 116 | |||
| 117 | throw( 'Param', "Missing <params> for action <$action>" ) | ||
| 118 | unless ref($orig) eq 'HASH'; | ||
| 119 | |||
| 120 | my %metadata; | ||
| 121 | my $params = {%$orig}; | ||
| 122 | my $serializer = $self->_serializer; | ||
| 123 | |||
| 124 | my $meta_params = $self->_metadata_params; | ||
| 125 | for ( keys %$meta_params ) { | ||
| 126 | next unless exists $params->{$_}; | ||
| 127 | $metadata{ $meta_params->{$_} } = delete $params->{$_}; | ||
| 128 | } | ||
| 129 | |||
| 130 | for ( @{ $self->_required_params } ) { | ||
| 131 | throw( 'Param', "Missing required param <$_>" ) | ||
| 132 | unless $metadata{"_$_"} || $self->_bulk_args->{$_}; | ||
| 133 | } | ||
| 134 | |||
| 135 | my $source; | ||
| 136 | if ( $action eq 'update' ) { | ||
| 137 | for ( @{ $self->_update_params } ) { | ||
| 138 | $source->{$_} = delete $params->{$_} | ||
| 139 | if exists $params->{$_}; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | elsif ( $action ne 'delete' ) { | ||
| 143 | $source = delete $params->{source} | ||
| 144 | || throw( 'Param', | ||
| 145 | "Missing <source> for action <$action>: " | ||
| 146 | . $serializer->encode($orig) ); | ||
| 147 | } | ||
| 148 | |||
| 149 | throw( "Unknown params <" | ||
| 150 | . ( join ',', sort keys %$params ) | ||
| 151 | . "> in <$action>: " | ||
| 152 | . $serializer->encode($orig) ) | ||
| 153 | if keys %$params; | ||
| 154 | |||
| 155 | return map { $serializer->encode($_) } | ||
| 156 | grep {$_} ( { $action => \%metadata }, $source ); | ||
| 157 | } | ||
| 158 | |||
| 159 | #=================================== | ||
| 160 | sub _report { | ||
| 161 | #=================================== | ||
| 162 | my ( $self, $buffer, $results ) = @_; | ||
| 163 | my $on_success = $self->on_success; | ||
| 164 | my $on_error = $self->on_error; | ||
| 165 | my $on_conflict = $self->on_conflict; | ||
| 166 | |||
| 167 | # assume errors if key not present, bwc | ||
| 168 | $results->{errors} = 1 unless exists $results->{errors}; | ||
| 169 | |||
| 170 | return | ||
| 171 | unless $on_success | ||
| 172 | || ( $results->{errors} and $on_error || $on_conflict ); | ||
| 173 | |||
| 174 | my $serializer = $self->_serializer; | ||
| 175 | |||
| 176 | my $j = 0; | ||
| 177 | |||
| 178 | for my $item ( @{ $results->{items} } ) { | ||
| 179 | my ( $action, $result ) = %$item; | ||
| 180 | my @args = ($action); | ||
| 181 | if ( my $error = $result->{error} ) { | ||
| 182 | if ($on_conflict) { | ||
| 183 | my ( $is_conflict, $version ) | ||
| 184 | = $self->_is_conflict_error($error); | ||
| 185 | if ($is_conflict) { | ||
| 186 | $on_conflict->( $action, $result, $j, $version ); | ||
| 187 | next; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | $on_error && $on_error->( $action, $result, $j ); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $on_success && $on_success->( $action, $result, $j ); | ||
| 194 | } | ||
| 195 | $j++; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | #=================================== | ||
| 200 | sub _is_conflict_error { | ||
| 201 | #=================================== | ||
| 202 | my ( $self, $error ) = @_; | ||
| 203 | my $version; | ||
| 204 | if ( ref($error) eq 'HASH' ) { | ||
| 205 | return 1 if $error->{type} eq 'document_already_exists_exception'; | ||
| 206 | return unless $error->{type} eq 'version_conflict_engine_exception'; | ||
| 207 | $error->{reason} =~ /version.conflict,.current.(?:version.)?\[(\d+)\]/; | ||
| 208 | return ( 1, $1 ); | ||
| 209 | } | ||
| 210 | return unless $error =~ / | ||
| 211 | DocumentAlreadyExistsException | ||
| 212 | |version.conflict,.current.\[(\d+)\] | ||
| 213 | /x; | ||
| 214 | return ( 1, $1 ); | ||
| 215 | } | ||
| 216 | |||
| 217 | #=================================== | ||
| 218 | sub clear_buffer { | ||
| 219 | #=================================== | ||
| 220 | my $self = shift; | ||
| 221 | @{ $self->_buffer } = (); | ||
| 222 | $self->_buffer_size(0); | ||
| 223 | $self->_buffer_count(0); | ||
| 224 | } | ||
| 225 | |||
| 226 | #=================================== | ||
| 227 | sub _doc_transformer { | ||
| 228 | #=================================== | ||
| 229 | my ( $self, $params ) = @_; | ||
| 230 | |||
| 231 | my $bulk_args = $self->_bulk_args; | ||
| 232 | my %allowed = map { $_ => 1, "_$_" => 1 } | ||
| 233 | ( @{ $self->_metadata_params }, 'source' ); | ||
| 234 | $allowed{fields} = 1; | ||
| 235 | |||
| 236 | delete @allowed{ 'index', '_index' } if $bulk_args->{index}; | ||
| 237 | delete @allowed{ 'type', '_type' } if $bulk_args->{type}; | ||
| 238 | |||
| 239 | my $version_type = $params->{version_type}; | ||
| 240 | my $transform = $params->{transform}; | ||
| 241 | |||
| 242 | return sub { | ||
| 243 | my %doc = %{ shift() }; | ||
| 244 | for ( keys %doc ) { | ||
| 245 | delete $doc{$_} unless $allowed{$_}; | ||
| 246 | } | ||
| 247 | |||
| 248 | if ( my $fields = delete $doc{fields} ) { | ||
| 249 | for (qw(_routing routing _parent parent)) { | ||
| 250 | $doc{$_} = $fields->{$_} | ||
| 251 | if exists $fields->{$_}; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | $doc{_version_type} = $version_type if $version_type; | ||
| 255 | |||
| 256 | return \%doc unless $transform; | ||
| 257 | return $transform->( \%doc ); | ||
| 258 | }; | ||
| 259 | } | ||
| 260 | |||
| 261 | 1; | ||
| 262 | |||
| 263 | # ABSTRACT: Provides common functionality to L<Elasticseach::Client::6_0::Bulk> and L<Search::Elasticsearch::Client::6_0::Async::Bulk> | ||
| 264 | |||
| 265 | __END__ |