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