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