| 1 | package Search::Elasticsearch::Client::Compat; | ||
| 2 | $Search::Elasticsearch::Client::Compat::VERSION = '0.10'; | ||
| 3 | use Moo; | ||
| 4 | with 'Search::Elasticsearch::Role::Client'; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Any::URI::Escape qw(uri_escape); | ||
| 9 | use JSON; | ||
| 10 | use Search::Elasticsearch::Util qw(throw parse_params); | ||
| 11 | use Carp; | ||
| 12 | |||
| 13 | has 'JSON' => ( is => 'lazy' ); | ||
| 14 | has '_base_qs' => ( is => 'ro', default => sub { {} } ); | ||
| 15 | has '_default' => ( is => 'ro', default => sub { {} } ); | ||
| 16 | has 'builder' => ( is => 'lazy' ); | ||
| 17 | has 'builder_class' => | ||
| 18 | ( is => 'ro', default => 'ElasticSearch::SearchBuilder' ); | ||
| 19 | |||
| 20 | use constant { | ||
| 21 | ONE_REQ => 1, | ||
| 22 | ONE_OPT => 2, | ||
| 23 | ONE_ALL => 3, | ||
| 24 | MULTI_ALL => 4, | ||
| 25 | MULTI_BLANK => 5, | ||
| 26 | MULTI_REQ => 6, | ||
| 27 | }; | ||
| 28 | |||
| 29 | use constant { | ||
| 30 | CMD_NONE => [], | ||
| 31 | CMD_INDEX_TYPE_ID => [ index => ONE_REQ, type => ONE_REQ, id => ONE_REQ ], | ||
| 32 | CMD_INDEX_TYPE_id => [ index => ONE_REQ, type => ONE_REQ, id => ONE_OPT ], | ||
| 33 | CMD_INDEX_type_ID => [ index => ONE_REQ, type => ONE_ALL, id => ONE_REQ ], | ||
| 34 | CMD_Index => [ index => ONE_OPT ], | ||
| 35 | CMD_index => [ index => MULTI_BLANK ], | ||
| 36 | CMD_indices => [ index => MULTI_ALL ], | ||
| 37 | CMD_INDICES => [ index => MULTI_REQ ], | ||
| 38 | CMD_INDEX => [ index => ONE_REQ ], | ||
| 39 | CMD_INDEX_TYPE => [ index => ONE_REQ, type => ONE_REQ ], | ||
| 40 | CMD_INDEX_type => [ index => ONE_REQ, type => MULTI_BLANK ], | ||
| 41 | CMD_index_TYPE => [ index => MULTI_ALL, type => ONE_REQ ], | ||
| 42 | CMD_index_types => [ index => MULTI_ALL, type => MULTI_REQ ], | ||
| 43 | CMD_INDICES_TYPE => [ index => MULTI_REQ, type => ONE_REQ ], | ||
| 44 | CMD_index_type => [ index => MULTI_ALL, type => MULTI_BLANK ], | ||
| 45 | CMD_index_then_type => [ index => ONE_OPT, type => ONE_OPT ], | ||
| 46 | CMD_RIVER => [ river => ONE_REQ ], | ||
| 47 | CMD_nodes => [ node => MULTI_BLANK ], | ||
| 48 | CMD_NAME => [ name => ONE_REQ ], | ||
| 49 | CMD_INDEX_PERC => [ index => ONE_REQ, percolator => ONE_REQ ], | ||
| 50 | |||
| 51 | CONSISTENCY => [ 'enum', [ 'one', 'quorum', 'all' ] ], | ||
| 52 | REPLICATION => [ 'enum', [ 'async', 'sync' ] ], | ||
| 53 | SEARCH_TYPE => [ | ||
| 54 | 'enum', | ||
| 55 | [ 'dfs_query_then_fetch', 'dfs_query_and_fetch', | ||
| 56 | 'query_then_fetch', 'query_and_fetch', | ||
| 57 | 'count', 'scan' | ||
| 58 | ] | ||
| 59 | ], | ||
| 60 | IGNORE_INDICES => [ 'enum', [ 'missing', 'none' ] ], | ||
| 61 | |||
| 62 | }; | ||
| 63 | |||
| 64 | our %QS_Format = ( | ||
| 65 | boolean => '1 | 0', | ||
| 66 | duration => "'5m' | '10s'", | ||
| 67 | optional => "'scalar value'", | ||
| 68 | flatten => "'scalar' or ['scalar_1', 'scalar_n']", | ||
| 69 | 'int' => "integer", | ||
| 70 | string => sub { | ||
| 71 | my $k = shift; | ||
| 72 | return $k eq 'preference' | ||
| 73 | ? '_local | _primary | _primary_first | $string' | ||
| 74 | : $k eq 'percolate' || $k eq 'q' ? '$query_string' | ||
| 75 | : $k eq 'scroll_id' ? '$scroll_id' | ||
| 76 | : $k eq 'df' ? '$default_field' | ||
| 77 | : '$string'; | ||
| 78 | }, | ||
| 79 | float => 'float', | ||
| 80 | enum => sub { join " | ", @{ $_[1][1] } }, | ||
| 81 | coderef => 'sub {..} | "IGNORE"', | ||
| 82 | ); | ||
| 83 | |||
| 84 | our %QS_Formatter = ( | ||
| 85 | boolean => sub { | ||
| 86 | my $key = shift; | ||
| 87 | my $val = $_[0] ? $_[1] : $_[2]; | ||
| 88 | return unless defined $val; | ||
| 89 | return ref $val ? $val : [ $key, $val ? 'true' : 'false' ]; | ||
| 90 | }, | ||
| 91 | duration => sub { | ||
| 92 | my ( $k, $t ) = @_; | ||
| 93 | return unless defined $t; | ||
| 94 | return [ $k, $t ] if $t =~ /^\d+([smh]|ms)$/i; | ||
| 95 | die "$k '$t' is not in the form $QS_Format{duration}\n"; | ||
| 96 | }, | ||
| 97 | flatten => sub { | ||
| 98 | my $key = shift; | ||
| 99 | my $array = shift or return; | ||
| 100 | return [ $key, ref $array ? join( ',', @$array ) : $array ]; | ||
| 101 | }, | ||
| 102 | 'int' => sub { | ||
| 103 | my $key = shift; | ||
| 104 | my $int = shift; | ||
| 105 | return unless defined $int; | ||
| 106 | eval { $int += 0; 1 } or die "'$key' is not an integer"; | ||
| 107 | return [ $key, $int ]; | ||
| 108 | }, | ||
| 109 | 'float' => sub { | ||
| 110 | my $key = shift; | ||
| 111 | my $float = shift; | ||
| 112 | return unless defined $float; | ||
| 113 | $key = shift if @_; | ||
| 114 | eval { $float += 0; 1 } or die "'$key' is not a float"; | ||
| 115 | return [ $key, $float ]; | ||
| 116 | }, | ||
| 117 | 'string' => sub { | ||
| 118 | my $key = shift; | ||
| 119 | my $string = shift; | ||
| 120 | return unless defined $string; | ||
| 121 | return [ $key, $string ]; | ||
| 122 | }, | ||
| 123 | 'coderef' => sub { | ||
| 124 | my $key = shift; | ||
| 125 | my $coderef = shift; | ||
| 126 | return unless defined $coderef; | ||
| 127 | unless ( ref $coderef ) { | ||
| 128 | die "'$key' is not a code ref or the string 'IGNORE'" | ||
| 129 | unless $coderef eq 'IGNORE'; | ||
| 130 | $coderef = sub { }; | ||
| 131 | } | ||
| 132 | return [ $key, $coderef ]; | ||
| 133 | }, | ||
| 134 | 'enum' => sub { | ||
| 135 | my $key = shift; | ||
| 136 | my $val = shift; | ||
| 137 | return unless defined $val; | ||
| 138 | my $vals = $_[0]; | ||
| 139 | for (@$vals) { | ||
| 140 | return [ $key, $val ] if $val eq $_; | ||
| 141 | } | ||
| 142 | die "Unrecognised value '$val'. Allowed values: " | ||
| 143 | . join( ', ', @$vals ); | ||
| 144 | }, | ||
| 145 | |||
| 146 | ); | ||
| 147 | |||
| 148 | #=================================== | ||
| 149 | sub _build_JSON { | ||
| 150 | #=================================== | ||
| 151 | JSON->new->utf8(1); | ||
| 152 | } | ||
| 153 | |||
| 154 | #=================================== | ||
| 155 | sub _build_builder { | ||
| 156 | #=================================== | ||
| 157 | my $self = shift; | ||
| 158 | my $class = $self->builder_class | ||
| 159 | or throw( 'Param', "No builder_class specified" ); | ||
| 160 | eval "require $class; 1" | ||
| 161 | or throw( 'Internal', | ||
| 162 | "Couldn't load class $class: " . ( $@ || 'Unknown error' ) ); | ||
| 163 | return $self->{_builder} = $class->new(@_); | ||
| 164 | } | ||
| 165 | |||
| 166 | #=================================== | ||
| 167 | sub request { | ||
| 168 | #=================================== | ||
| 169 | my ( $self, $params ) = parse_params(@_); | ||
| 170 | |||
| 171 | my $result; | ||
| 172 | eval { $result = $self->transport->perform_request($params); 1 }; | ||
| 173 | if ( my $error = $@ ) { | ||
| 174 | $error->{-vars} = delete $error->{vars}; | ||
| 175 | die $error; | ||
| 176 | } | ||
| 177 | |||
| 178 | $result | ||
| 179 | = $params->{post_process} | ||
| 180 | ? $params->{post_process}->($result) | ||
| 181 | : $result; | ||
| 182 | return $params->{as_json} ? $self->JSON->encode($result) : $result; | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | #=================================== | ||
| 187 | sub use_index { | ||
| 188 | #=================================== | ||
| 189 | my $self = shift; | ||
| 190 | if (@_) { | ||
| 191 | $self->{_default}{index} = shift; | ||
| 192 | } | ||
| 193 | return $self->{_default}{index}; | ||
| 194 | } | ||
| 195 | |||
| 196 | #=================================== | ||
| 197 | sub use_type { | ||
| 198 | #=================================== | ||
| 199 | my $self = shift; | ||
| 200 | if (@_) { | ||
| 201 | $self->{_default}{type} = shift; | ||
| 202 | } | ||
| 203 | return $self->{_default}{type}; | ||
| 204 | } | ||
| 205 | |||
| 206 | #=================================== | ||
| 207 | sub reindex { | ||
| 208 | #=================================== | ||
| 209 | my ( $self, $params ) = parse_params(@_); | ||
| 210 | |||
| 211 | my $source = $params->{source} | ||
| 212 | or throw( 'Param', 'Missing source param' ); | ||
| 213 | |||
| 214 | my $transform = $params->{transform} || sub { shift() }; | ||
| 215 | my $verbose = !$params->{quiet}; | ||
| 216 | my $dest_index = $params->{dest_index}; | ||
| 217 | my $bulk_size = $params->{bulk_size} || 1000; | ||
| 218 | my $method = $params->{_method_name} || 'next'; | ||
| 219 | |||
| 220 | local $| = $verbose; | ||
| 221 | printf( "Reindexing %d docs\n", $source->total ) | ||
| 222 | if $verbose; | ||
| 223 | |||
| 224 | my @docs; | ||
| 225 | while (1) { | ||
| 226 | my $doc = $source->$method(); | ||
| 227 | if ( !$doc or @docs == $bulk_size ) { | ||
| 228 | my $results = $self->bulk_index( | ||
| 229 | docs => \@docs, | ||
| 230 | map { $_ => $params->{$_} } qw(on_conflict on_error), | ||
| 231 | ); | ||
| 232 | if ( my $err = $results->{errors} ) { | ||
| 233 | my @errors = splice @$err, 0, 5; | ||
| 234 | push @errors, sprintf "...and %d more", scalar @$err | ||
| 235 | if @$err; | ||
| 236 | throw( 'Request', "Errors occurred while reindexing:", | ||
| 237 | \@errors ); | ||
| 238 | } | ||
| 239 | @docs = (); | ||
| 240 | print "." if $verbose; | ||
| 241 | } | ||
| 242 | last unless $doc; | ||
| 243 | |||
| 244 | $doc = $transform->($doc) or next; | ||
| 245 | $doc->{version_type} = 'external' | ||
| 246 | if defined $doc->{_version}; | ||
| 247 | if ( my $fields = delete $doc->{fields} ) { | ||
| 248 | $doc->{parent} = $fields->{_parent} | ||
| 249 | if defined $fields->{_parent}; | ||
| 250 | } | ||
| 251 | $doc->{_index} = $dest_index | ||
| 252 | if $dest_index; | ||
| 253 | push @docs, $doc; | ||
| 254 | } | ||
| 255 | |||
| 256 | print "\nDone\n" if $verbose; | ||
| 257 | } | ||
| 258 | |||
| 259 | #=================================== | ||
| 260 | sub query_parser { | ||
| 261 | #=================================== | ||
| 262 | require Search::Elasticsearch::Compat::QueryParser; | ||
| 263 | shift; # drop class/$self | ||
| 264 | Search::Elasticsearch::Compat::QueryParser->new(@_); | ||
| 265 | } | ||
| 266 | |||
| 267 | ################################## | ||
| 268 | ## DOCUMENT MANAGEMENT | ||
| 269 | ################################## | ||
| 270 | |||
| 271 | #=================================== | ||
| 272 | sub get { | ||
| 273 | #=================================== | ||
| 274 | shift()->_do_action( | ||
| 275 | 'get', | ||
| 276 | { cmd => CMD_INDEX_type_ID, | ||
| 277 | qs => { | ||
| 278 | fields => ['flatten'], | ||
| 279 | ignore_missing => [ 'boolean', 1 ], | ||
| 280 | preference => ['string'], | ||
| 281 | refresh => [ 'boolean', 1 ], | ||
| 282 | routing => ['string'], | ||
| 283 | parent => ['string'], | ||
| 284 | }, | ||
| 285 | }, | ||
| 286 | @_ | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | |||
| 290 | #=================================== | ||
| 291 | sub exists : method { | ||
| 292 | #=================================== | ||
| 293 | shift()->_do_action( | ||
| 294 | 'exists', | ||
| 295 | { method => 'HEAD', | ||
| 296 | cmd => CMD_INDEX_TYPE_ID, | ||
| 297 | qs => { | ||
| 298 | preference => ['string'], | ||
| 299 | refresh => [ 'boolean', 1 ], | ||
| 300 | routing => ['string'], | ||
| 301 | parent => ['string'], | ||
| 302 | }, | ||
| 303 | fixup => sub { $_[1]->{qs}{ignore_missing} = 1 }, | ||
| 304 | post_process => sub { $_[0] ? { ok => 1 } : undef }, | ||
| 305 | }, | ||
| 306 | @_ | ||
| 307 | ); | ||
| 308 | } | ||
| 309 | |||
| 310 | #=================================== | ||
| 311 | sub mget { | ||
| 312 | #=================================== | ||
| 313 | my ( $self, $params ) = parse_params(@_); | ||
| 314 | |||
| 315 | $params->{$_} ||= $self->{_default}{$_} for qw(index type); | ||
| 316 | |||
| 317 | if ( $params->{index} ) { | ||
| 318 | if ( my $ids = delete $params->{ids} ) { | ||
| 319 | throw( 'Param', 'mget', | ||
| 320 | 'Cannot specify both ids and docs in mget()' ) | ||
| 321 | if $params->{docs}; | ||
| 322 | $params->{docs} = [ map { +{ _id => $_ } } @$ids ]; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | throw( 'Param', | ||
| 327 | 'Cannot specify a type for mget() without specifying index' ) | ||
| 328 | if $params->{type}; | ||
| 329 | throw( 'Param', 'Use of the ids param with mget() requires an index' ) | ||
| 330 | if $params->{ids}; | ||
| 331 | } | ||
| 332 | |||
| 333 | my $filter; | ||
| 334 | $self->_do_action( | ||
| 335 | 'mget', | ||
| 336 | { cmd => [ index => ONE_OPT, type => ONE_OPT ], | ||
| 337 | postfix => '_mget', | ||
| 338 | data => { docs => 'docs' }, | ||
| 339 | qs => { | ||
| 340 | fields => ['flatten'], | ||
| 341 | filter_missing => [ 'boolean', 1 ], | ||
| 342 | }, | ||
| 343 | fixup => sub { | ||
| 344 | $_[1]->{skip} = [] unless @{ $_[1]{body}{docs} }; | ||
| 345 | $filter = delete $_[1]->{qs}{filter_missing}; | ||
| 346 | }, | ||
| 347 | post_process => sub { | ||
| 348 | my $result = shift; | ||
| 349 | my $docs = $result->{docs}; | ||
| 350 | return $filter ? [ grep { $_->{exists} } @$docs ] : $docs; | ||
| 351 | } | ||
| 352 | }, | ||
| 353 | $params | ||
| 354 | ); | ||
| 355 | } | ||
| 356 | |||
| 357 | my %Index_Defn = ( | ||
| 358 | cmd => CMD_INDEX_TYPE_id, | ||
| 359 | qs => { | ||
| 360 | consistency => CONSISTENCY, | ||
| 361 | create => [ 'boolean', [ op_type => 'create' ] ], | ||
| 362 | parent => ['string'], | ||
| 363 | percolate => ['string'], | ||
| 364 | refresh => [ 'boolean', 1 ], | ||
| 365 | replication => REPLICATION, | ||
| 366 | routing => ['string'], | ||
| 367 | timeout => ['duration'], | ||
| 368 | timestamp => ['string'], | ||
| 369 | ttl => ['int'], | ||
| 370 | version => ['int'], | ||
| 371 | version_type => [ 'enum', [ 'internal', 'external' ] ], | ||
| 372 | }, | ||
| 373 | data => { data => 'data' }, | ||
| 374 | fixup => sub { | ||
| 375 | $_[1]{body} = $_[1]{body}{data}; | ||
| 376 | } | ||
| 377 | ); | ||
| 378 | |||
| 379 | #=================================== | ||
| 380 | sub index { | ||
| 381 | #=================================== | ||
| 382 | my ( $self, $params ) = parse_params(@_); | ||
| 383 | $self->_index( 'index', \%Index_Defn, $params ); | ||
| 384 | } | ||
| 385 | |||
| 386 | #=================================== | ||
| 387 | sub set { | ||
| 388 | #=================================== | ||
| 389 | my ( $self, $params ) = parse_params(@_); | ||
| 390 | $self->_index( 'set', \%Index_Defn, $params ); | ||
| 391 | } | ||
| 392 | |||
| 393 | #=================================== | ||
| 394 | sub create { | ||
| 395 | #=================================== | ||
| 396 | my ( $self, $params ) = parse_params(@_); | ||
| 397 | $self->_index( 'create', \%Index_Defn, { %$params, create => 1 } ); | ||
| 398 | } | ||
| 399 | |||
| 400 | #=================================== | ||
| 401 | sub _index { | ||
| 402 | #=================================== | ||
| 403 | my $self = shift; | ||
| 404 | $_[1]->{method} = $_[2]->{id} ? 'PUT' : 'POST'; | ||
| 405 | $self->_do_action(@_); | ||
| 406 | } | ||
| 407 | |||
| 408 | #=================================== | ||
| 409 | sub update { | ||
| 410 | #=================================== | ||
| 411 | shift()->_do_action( | ||
| 412 | 'update', | ||
| 413 | { method => 'POST', | ||
| 414 | cmd => CMD_INDEX_TYPE_ID, | ||
| 415 | postfix => '_update', | ||
| 416 | data => { | ||
| 417 | script => ['script'], | ||
| 418 | params => ['params'], | ||
| 419 | doc => ['doc'], | ||
| 420 | upsert => ['upsert'], | ||
| 421 | }, | ||
| 422 | qs => { | ||
| 423 | consistency => CONSISTENCY, | ||
| 424 | fields => ['flatten'], | ||
| 425 | ignore_missing => [ 'boolean', 1 ], | ||
| 426 | parent => ['string'], | ||
| 427 | percolate => ['string'], | ||
| 428 | retry_on_conflict => ['int'], | ||
| 429 | routing => ['string'], | ||
| 430 | timeout => ['duration'], | ||
| 431 | replication => REPLICATION, | ||
| 432 | } | ||
| 433 | }, | ||
| 434 | @_ | ||
| 435 | ); | ||
| 436 | } | ||
| 437 | |||
| 438 | #=================================== | ||
| 439 | sub delete { | ||
| 440 | #=================================== | ||
| 441 | shift()->_do_action( | ||
| 442 | 'delete', | ||
| 443 | { method => 'DELETE', | ||
| 444 | cmd => CMD_INDEX_TYPE_ID, | ||
| 445 | qs => { | ||
| 446 | consistency => CONSISTENCY, | ||
| 447 | ignore_missing => [ 'boolean', 1 ], | ||
| 448 | refresh => [ 'boolean', 1 ], | ||
| 449 | parent => ['string'], | ||
| 450 | routing => ['string'], | ||
| 451 | version => ['int'], | ||
| 452 | replication => REPLICATION, | ||
| 453 | } | ||
| 454 | }, | ||
| 455 | @_ | ||
| 456 | ); | ||
| 457 | } | ||
| 458 | |||
| 459 | #=================================== | ||
| 460 | sub analyze { | ||
| 461 | #=================================== | ||
| 462 | shift()->_do_action( | ||
| 463 | 'analyze', | ||
| 464 | { method => 'GET', | ||
| 465 | cmd => CMD_Index, | ||
| 466 | postfix => '_analyze', | ||
| 467 | qs => { | ||
| 468 | text => ['string'], | ||
| 469 | analyzer => ['string'], | ||
| 470 | tokenizer => ['string'], | ||
| 471 | filters => ['flatten'], | ||
| 472 | field => ['string'], | ||
| 473 | format => [ 'enum', [ 'detailed', 'text' ] ], | ||
| 474 | prefer_local => [ 'boolean', undef, 0 ], | ||
| 475 | } | ||
| 476 | }, | ||
| 477 | @_ | ||
| 478 | ); | ||
| 479 | } | ||
| 480 | |||
| 481 | ################################## | ||
| 482 | ## BULK INTERFACE | ||
| 483 | ################################## | ||
| 484 | |||
| 485 | #=================================== | ||
| 486 | sub bulk { | ||
| 487 | #=================================== | ||
| 488 | my $self = shift; | ||
| 489 | $self->_bulk( 'bulk', $self->_bulk_params( 'actions', @_ ) ); | ||
| 490 | } | ||
| 491 | |||
| 492 | #=================================== | ||
| 493 | sub _bulk { | ||
| 494 | #=================================== | ||
| 495 | my ( $self, $method, $params ) = @_; | ||
| 496 | my %callbacks; | ||
| 497 | my $actions = $params->{actions} || []; | ||
| 498 | |||
| 499 | $self->_do_action( | ||
| 500 | $method, | ||
| 501 | { cmd => CMD_index_then_type, | ||
| 502 | method => 'POST', | ||
| 503 | postfix => '_bulk', | ||
| 504 | qs => { | ||
| 505 | consistency => CONSISTENCY, | ||
| 506 | replication => REPLICATION, | ||
| 507 | refresh => [ 'boolean', 1 ], | ||
| 508 | timeout => ['duration'], | ||
| 509 | on_conflict => ['coderef'], | ||
| 510 | on_error => ['coderef'], | ||
| 511 | }, | ||
| 512 | data => { actions => 'actions' }, | ||
| 513 | fixup => sub { | ||
| 514 | die "Cannot specify type without index" | ||
| 515 | if $params->{type} && !$params->{index}; | ||
| 516 | $_[1]->{body} = $self->_bulk_request($actions); | ||
| 517 | $_[1]->{skip} = { actions => [], results => [] } | ||
| 518 | unless $_[1]->{body}; | ||
| 519 | $callbacks{$_} = delete $_[1]->{qs}{$_} | ||
| 520 | for qw(on_error on_conflict); | ||
| 521 | }, | ||
| 522 | post_process => sub { | ||
| 523 | $self->_bulk_response( \%callbacks, $actions, @_ ); | ||
| 524 | }, | ||
| 525 | }, | ||
| 526 | $params | ||
| 527 | ); | ||
| 528 | } | ||
| 529 | |||
| 530 | #=================================== | ||
| 531 | sub bulk_index { shift->_bulk_action( 'index', @_ ) } | ||
| 532 | sub bulk_create { shift->_bulk_action( 'create', @_ ) } | ||
| 533 | sub bulk_delete { shift->_bulk_action( 'delete', @_ ) } | ||
| 534 | #=================================== | ||
| 535 | |||
| 536 | #=================================== | ||
| 537 | sub _bulk_action { | ||
| 538 | #=================================== | ||
| 539 | my $self = shift; | ||
| 540 | my $action = shift; | ||
| 541 | my $params = $self->_bulk_params( 'docs', @_ ); | ||
| 542 | $params->{actions} | ||
| 543 | = [ map { +{ $action => $_ } } @{ delete $params->{docs} } ]; | ||
| 544 | return $self->_bulk( "bulk_$action", $params ); | ||
| 545 | } | ||
| 546 | |||
| 547 | #=================================== | ||
| 548 | sub _bulk_params { | ||
| 549 | #=================================== | ||
| 550 | my $self = shift; | ||
| 551 | my $key = shift; | ||
| 552 | |||
| 553 | return { $key => [], @_ } unless ref $_[0]; | ||
| 554 | return | ||
| 555 | ref $_[0] eq 'ARRAY' ? { $key => $_[0] } : { $key => [], %{ $_[0] } } | ||
| 556 | unless @_ > 1; | ||
| 557 | |||
| 558 | carp "The method signature for bulk methods has changed. " | ||
| 559 | . "Please check the docs."; | ||
| 560 | |||
| 561 | if ( ref $_[0] eq 'ARRAY' ) { | ||
| 562 | my $first = shift; | ||
| 563 | my $params = ref $_[0] ? shift : {@_}; | ||
| 564 | $params->{$key} = $first; | ||
| 565 | return $params; | ||
| 566 | } | ||
| 567 | return { $key => \@_ }; | ||
| 568 | } | ||
| 569 | |||
| 570 | my %Bulk_Actions = ( | ||
| 571 | 'delete' => { | ||
| 572 | index => ONE_OPT, | ||
| 573 | type => ONE_OPT, | ||
| 574 | id => ONE_REQ, | ||
| 575 | parent => ONE_OPT, | ||
| 576 | routing => ONE_OPT, | ||
| 577 | version => ONE_OPT, | ||
| 578 | version_type => ONE_OPT, | ||
| 579 | }, | ||
| 580 | 'index' => { | ||
| 581 | index => ONE_OPT, | ||
| 582 | type => ONE_OPT, | ||
| 583 | id => ONE_OPT, | ||
| 584 | data => ONE_REQ, | ||
| 585 | routing => ONE_OPT, | ||
| 586 | parent => ONE_OPT, | ||
| 587 | percolate => ONE_OPT, | ||
| 588 | timestamp => ONE_OPT, | ||
| 589 | ttl => ONE_OPT, | ||
| 590 | version => ONE_OPT, | ||
| 591 | version_type => ONE_OPT, | ||
| 592 | }, | ||
| 593 | ); | ||
| 594 | $Bulk_Actions{create} = $Bulk_Actions{index}; | ||
| 595 | |||
| 596 | #=================================== | ||
| 597 | sub _bulk_request { | ||
| 598 | #=================================== | ||
| 599 | my $self = shift; | ||
| 600 | my $actions = shift; | ||
| 601 | |||
| 602 | my $json = $self->JSON; | ||
| 603 | my $indenting = $json->get_indent; | ||
| 604 | $json->indent(0); | ||
| 605 | |||
| 606 | my $json_docs = ''; | ||
| 607 | my $error; | ||
| 608 | eval { | ||
| 609 | for my $data (@$actions) { | ||
| 610 | die "'actions' must be an ARRAY ref of HASH refs" | ||
| 611 | unless ref $data eq 'HASH'; | ||
| 612 | |||
| 613 | my ( $action, $params ) = %$data; | ||
| 614 | $action ||= ''; | ||
| 615 | my $defn = $Bulk_Actions{$action} | ||
| 616 | || die "Unknown action '$action'"; | ||
| 617 | |||
| 618 | my %metadata; | ||
| 619 | $params = {%$params}; | ||
| 620 | delete @{$params}{qw(_score sort)}; | ||
| 621 | $params->{data} ||= delete $params->{_source} | ||
| 622 | if $params->{_source}; | ||
| 623 | |||
| 624 | for my $key ( keys %$defn ) { | ||
| 625 | my $val = delete $params->{$key}; | ||
| 626 | $val = delete $params->{"_$key"} unless defined $val; | ||
| 627 | unless ( defined $val ) { | ||
| 628 | next if $defn->{$key} == ONE_OPT; | ||
| 629 | die "Missing required param '$key' for action '$action'"; | ||
| 630 | } | ||
| 631 | $metadata{"_$key"} = $val; | ||
| 632 | } | ||
| 633 | die "Unknown params for bulk action '$action': " | ||
| 634 | . join( ', ', keys %$params ) | ||
| 635 | if keys %$params; | ||
| 636 | |||
| 637 | my $data = delete $metadata{_data}; | ||
| 638 | my $request = $json->encode( { $action => \%metadata } ) . "\n"; | ||
| 639 | if ($data) { | ||
| 640 | $data = $json->encode($data) if ref $data eq 'HASH'; | ||
| 641 | $request .= $data . "\n"; | ||
| 642 | } | ||
| 643 | $json_docs .= $request; | ||
| 644 | } | ||
| 645 | 1; | ||
| 646 | } or $error = $@ || 'Unknown error'; | ||
| 647 | |||
| 648 | $json->indent($indenting); | ||
| 649 | die $error if $error; | ||
| 650 | |||
| 651 | return $json_docs; | ||
| 652 | } | ||
| 653 | |||
| 654 | #=================================== | ||
| 655 | sub _bulk_response { | ||
| 656 | #=================================== | ||
| 657 | my $self = shift; | ||
| 658 | my $callbacks = shift; | ||
| 659 | my $actions = shift; | ||
| 660 | my $results = shift; | ||
| 661 | |||
| 662 | my $items = ref($results) eq 'HASH' && $results->{items} | ||
| 663 | || throw( 'Request', 'Malformed response to bulk query', $results ); | ||
| 664 | |||
| 665 | my ( @errors, %matches ); | ||
| 666 | my ( $on_conflict, $on_error ) = @{$callbacks}{qw(on_conflict on_error)}; | ||
| 667 | |||
| 668 | for ( my $i = 0; $i < @$actions; $i++ ) { | ||
| 669 | my ( $action, $item ) = ( %{ $items->[$i] } ); | ||
| 670 | if ( my $match = $item->{matches} ) { | ||
| 671 | push @{ $matches{$_} }, $item for @$match; | ||
| 672 | } | ||
| 673 | |||
| 674 | my $error = $items->[$i]{$action}{error} or next; | ||
| 675 | if ( $on_conflict | ||
| 676 | and $error =~ / | ||
| 677 | VersionConflictEngineException | ||
| 678 | | DocumentAlreadyExistsException | ||
| 679 | /x | ||
| 680 | ) | ||
| 681 | { | ||
| 682 | $on_conflict->( $action, $actions->[$i]{$action}, $error, $i ); | ||
| 683 | } | ||
| 684 | elsif ($on_error) { | ||
| 685 | $on_error->( $action, $actions->[$i]{$action}, $error, $i ); | ||
| 686 | } | ||
| 687 | else { | ||
| 688 | push @errors, { action => $actions->[$i], error => $error }; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | return { | ||
| 693 | actions => $actions, | ||
| 694 | results => $items, | ||
| 695 | matches => \%matches, | ||
| 696 | took => $results->{took}, | ||
| 697 | ( @errors ? ( errors => \@errors ) : () ) | ||
| 698 | }; | ||
| 699 | } | ||
| 700 | |||
| 701 | ################################## | ||
| 702 | ## DSL FIXUP | ||
| 703 | ################################## | ||
| 704 | |||
| 705 | #=================================== | ||
| 706 | sub _to_dsl { | ||
| 707 | #=================================== | ||
| 708 | my $self = shift; | ||
| 709 | my $ops = shift; | ||
| 710 | my $builder; | ||
| 711 | foreach my $clause (@_) { | ||
| 712 | while ( my ( $old, $new ) = each %$ops ) { | ||
| 713 | my $src = delete $clause->{$old} or next; | ||
| 714 | die "Cannot specify $old and $new parameters.\n" | ||
| 715 | if $clause->{$new}; | ||
| 716 | $builder ||= $self->builder; | ||
| 717 | my $method = $new eq 'query' ? 'query' : 'filter'; | ||
| 718 | my $sub_clause = $builder->$method($src) or next; | ||
| 719 | $clause->{$new} = $sub_clause->{$method}; | ||
| 720 | } | ||
| 721 | } | ||
| 722 | } | ||
| 723 | |||
| 724 | #=================================== | ||
| 725 | sub _data_fixup { | ||
| 726 | #=================================== | ||
| 727 | my $self = shift; | ||
| 728 | my $data = shift; | ||
| 729 | $self->_to_dsl( { queryb => 'query', filterb => 'filter' }, $data ); | ||
| 730 | |||
| 731 | my $facets = $data->{facets} or return; | ||
| 732 | die "(facets) must be a HASH ref" unless ref $facets eq 'HASH'; | ||
| 733 | $facets = $data->{facets} = {%$facets}; | ||
| 734 | for ( values %$facets ) { | ||
| 735 | die "All (facets) must be HASH refs" unless ref $_ eq 'HASH'; | ||
| 736 | $_ = my $facet = {%$_}; | ||
| 737 | $self->_to_dsl( | ||
| 738 | { queryb => 'query', | ||
| 739 | filterb => 'filter', | ||
| 740 | facet_filterb => 'facet_filter' | ||
| 741 | }, | ||
| 742 | $facet | ||
| 743 | ); | ||
| 744 | } | ||
| 745 | } | ||
| 746 | |||
| 747 | #=================================== | ||
| 748 | sub _query_fixup { | ||
| 749 | #=================================== | ||
| 750 | my $self = shift; | ||
| 751 | my $args = shift; | ||
| 752 | $self->_to_dsl( { queryb => 'query' }, $args->{body} ); | ||
| 753 | if ( my $query = delete $args->{body}{query} ) { | ||
| 754 | my ( $k, $v ) = %$query; | ||
| 755 | $args->{body}{$k} = $v; | ||
| 756 | } | ||
| 757 | } | ||
| 758 | |||
| 759 | #=================================== | ||
| 760 | sub _warmer_fixup { | ||
| 761 | #=================================== | ||
| 762 | my ( $self, $args ) = @_; | ||
| 763 | my $warmers = $args->{body}{warmers} or return; | ||
| 764 | $warmers = $args->{body}{warmers} = {%$warmers}; | ||
| 765 | for ( values %$warmers ) { | ||
| 766 | $_ = {%$_}; | ||
| 767 | my $source = $_->{source} or next; | ||
| 768 | $_->{source} = $source = {%$source}; | ||
| 769 | $self->_data_fixup($source); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | |||
| 773 | ################################## | ||
| 774 | ## QUERIES | ||
| 775 | ################################## | ||
| 776 | |||
| 777 | my %Search_Data = ( | ||
| 778 | explain => ['explain'], | ||
| 779 | facets => ['facets'], | ||
| 780 | fields => ['fields'], | ||
| 781 | filter => ['filter'], | ||
| 782 | filterb => ['filterb'], | ||
| 783 | from => ['from'], | ||
| 784 | highlight => ['highlight'], | ||
| 785 | indices_boost => ['indices_boost'], | ||
| 786 | min_score => ['min_score'], | ||
| 787 | script_fields => ['script_fields'], | ||
| 788 | size => ['size'], | ||
| 789 | 'sort' => ['sort'], | ||
| 790 | track_scores => ['track_scores'], | ||
| 791 | ); | ||
| 792 | |||
| 793 | my %Search_Defn = ( | ||
| 794 | cmd => CMD_index_type, | ||
| 795 | postfix => '_search', | ||
| 796 | data => { | ||
| 797 | %Search_Data, | ||
| 798 | query => ['query'], | ||
| 799 | queryb => ['queryb'], | ||
| 800 | partial_fields => ['partial_fields'] | ||
| 801 | }, | ||
| 802 | qs => { | ||
| 803 | search_type => SEARCH_TYPE, | ||
| 804 | ignore_indices => IGNORE_INDICES, | ||
| 805 | preference => ['string'], | ||
| 806 | routing => ['flatten'], | ||
| 807 | timeout => ['duration'], | ||
| 808 | scroll => ['duration'], | ||
| 809 | stats => ['flatten'], | ||
| 810 | version => [ 'boolean', 1 ] | ||
| 811 | }, | ||
| 812 | fixup => sub { $_[0]->_data_fixup( $_[1]->{body} ) }, | ||
| 813 | ); | ||
| 814 | |||
| 815 | my %SearchQS_Defn = ( | ||
| 816 | cmd => CMD_index_type, | ||
| 817 | postfix => '_search', | ||
| 818 | qs => { | ||
| 819 | q => ['string'], | ||
| 820 | df => ['string'], | ||
| 821 | analyze_wildcard => [ 'boolean', 1 ], | ||
| 822 | analyzer => ['string'], | ||
| 823 | default_operator => [ 'enum', [ 'OR', 'AND' ] ], | ||
| 824 | explain => [ 'boolean', 1 ], | ||
| 825 | fields => ['flatten'], | ||
| 826 | from => ['int'], | ||
| 827 | ignore_indices => IGNORE_INDICES, | ||
| 828 | lenient => [ 'boolean', 1 ], | ||
| 829 | lowercase_expanded_terms => [ 'boolean', 1 ], | ||
| 830 | min_score => ['float'], | ||
| 831 | preference => ['string'], | ||
| 832 | quote_analyzer => ['string'], | ||
| 833 | quote_field_suffix => ['string'], | ||
| 834 | routing => ['flatten'], | ||
| 835 | scroll => ['duration'], | ||
| 836 | search_type => SEARCH_TYPE, | ||
| 837 | size => ['int'], | ||
| 838 | 'sort' => ['flatten'], | ||
| 839 | stats => ['flatten'], | ||
| 840 | timeout => ['duration'], | ||
| 841 | version => [ 'boolean', 1 ], | ||
| 842 | }, | ||
| 843 | ); | ||
| 844 | |||
| 845 | my %Query_Defn = ( | ||
| 846 | data => { | ||
| 847 | query => ['query'], | ||
| 848 | queryb => ['queryb'], | ||
| 849 | }, | ||
| 850 | deprecated => { | ||
| 851 | bool => ['bool'], | ||
| 852 | boosting => ['boosting'], | ||
| 853 | constant_score => ['constant_score'], | ||
| 854 | custom_score => ['custom_score'], | ||
| 855 | dis_max => ['dis_max'], | ||
| 856 | field => ['field'], | ||
| 857 | field_masking_span => ['field_masking_span'], | ||
| 858 | filtered => ['filtered'], | ||
| 859 | flt => [ 'flt', 'fuzzy_like_this' ], | ||
| 860 | flt_field => [ 'flt_field', 'fuzzy_like_this_field' ], | ||
| 861 | fuzzy => ['fuzzy'], | ||
| 862 | has_child => ['has_child'], | ||
| 863 | ids => ['ids'], | ||
| 864 | match_all => ['match_all'], | ||
| 865 | mlt => [ 'mlt', 'more_like_this' ], | ||
| 866 | mlt_field => [ 'mlt_field', 'more_like_this_field' ], | ||
| 867 | prefix => ['prefix'], | ||
| 868 | query_string => ['query_string'], | ||
| 869 | range => ['range'], | ||
| 870 | span_first => ['span_first'], | ||
| 871 | span_near => ['span_near'], | ||
| 872 | span_not => ['span_not'], | ||
| 873 | span_or => ['span_or'], | ||
| 874 | span_term => ['span_term'], | ||
| 875 | term => ['term'], | ||
| 876 | terms => [ 'terms', 'in' ], | ||
| 877 | text => ['text'], | ||
| 878 | text_phrase => ['text_phrase'], | ||
| 879 | text_phrase_prefix => ['text_phrase_prefix'], | ||
| 880 | top_children => ['top_children'], | ||
| 881 | wildcard => ['wildcard'], | ||
| 882 | } | ||
| 883 | ); | ||
| 884 | |||
| 885 | #=================================== | ||
| 886 | sub search { shift()->_do_action( 'search', \%Search_Defn, @_ ) } | ||
| 887 | sub searchqs { shift()->_do_action( 'searchqs', \%SearchQS_Defn, @_ ) } | ||
| 888 | #=================================== | ||
| 889 | |||
| 890 | #=================================== | ||
| 891 | sub msearch { | ||
| 892 | #=================================== | ||
| 893 | my ( $self, $params ) = parse_params(@_); | ||
| 894 | my $queries = $params->{queries} || []; | ||
| 895 | |||
| 896 | my $order; | ||
| 897 | if ( ref $queries eq 'HASH' ) { | ||
| 898 | $order = {}; | ||
| 899 | my $i = 0; | ||
| 900 | my @queries; | ||
| 901 | for ( sort keys %$queries ) { | ||
| 902 | $order->{$_} = $i++; | ||
| 903 | push @queries, $queries->{$_}; | ||
| 904 | } | ||
| 905 | $queries = \@queries; | ||
| 906 | } | ||
| 907 | |||
| 908 | $self->_do_action( | ||
| 909 | 'msearch', | ||
| 910 | { cmd => CMD_index_type, | ||
| 911 | method => 'GET', | ||
| 912 | postfix => '_msearch', | ||
| 913 | qs => { search_type => SEARCH_TYPE }, | ||
| 914 | data => { queries => 'queries' }, | ||
| 915 | fixup => sub { | ||
| 916 | my ( $self, $args ) = @_; | ||
| 917 | $args->{body} = $self->_msearch_queries($queries); | ||
| 918 | $args->{skip} = $order ? {} : [] unless $args->{body}; | ||
| 919 | }, | ||
| 920 | post_process => sub { | ||
| 921 | my $responses = shift->{responses}; | ||
| 922 | return $responses unless $order; | ||
| 923 | return { | ||
| 924 | map { $_ => $responses->[ $order->{$_} ] } | ||
| 925 | keys %$order | ||
| 926 | }; | ||
| 927 | }, | ||
| 928 | }, | ||
| 929 | $params | ||
| 930 | ); | ||
| 931 | } | ||
| 932 | |||
| 933 | my %MSearch = ( | ||
| 934 | ( map { $_ => 'h' } 'index', 'type', keys %{ $Search_Defn{qs} } ), | ||
| 935 | ( map { $_ => 'b' } 'timeout', 'stats', | ||
| 936 | 'version', keys %{ $Search_Defn{data} } | ||
| 937 | ) | ||
| 938 | ); | ||
| 939 | delete $MSearch{scroll}; | ||
| 940 | |||
| 941 | #=================================== | ||
| 942 | sub _msearch_queries { | ||
| 943 | #=================================== | ||
| 944 | my $self = shift; | ||
| 945 | my $queries = shift; | ||
| 946 | |||
| 947 | my $json = $self->JSON; | ||
| 948 | my $indenting = $json->get_indent; | ||
| 949 | $json->indent(0); | ||
| 950 | |||
| 951 | my $json_docs = ''; | ||
| 952 | my $error; | ||
| 953 | eval { | ||
| 954 | for my $query (@$queries) { | ||
| 955 | die "'queries' must contain HASH refs\n" | ||
| 956 | unless ref $query eq 'HASH'; | ||
| 957 | |||
| 958 | my %request = ( h => {}, b => {} ); | ||
| 959 | for ( keys %$query ) { | ||
| 960 | my $dest = $MSearch{$_} | ||
| 961 | or die "Unknown param for msearch: $_\n"; | ||
| 962 | $request{$dest}{$_} = $query->{$_}; | ||
| 963 | } | ||
| 964 | |||
| 965 | # flatten arrays | ||
| 966 | for (qw(index type stats routing)) { | ||
| 967 | $request{h}{$_} = join ",", @{ $request{h}{$_} } | ||
| 968 | if ref $request{h}{$_} eq 'ARRAY'; | ||
| 969 | } | ||
| 970 | $self->_data_fixup( $request{b} ); | ||
| 971 | $json_docs .= $json->encode( $request{h} ) . "\n" | ||
| 972 | . $json->encode( $request{b} ) . "\n"; | ||
| 973 | } | ||
| 974 | 1; | ||
| 975 | } or $error = $@ || 'Unknown error'; | ||
| 976 | |||
| 977 | $json->indent($indenting); | ||
| 978 | die $error if $error; | ||
| 979 | |||
| 980 | return $json_docs; | ||
| 981 | } | ||
| 982 | |||
| 983 | #=================================== | ||
| 984 | sub validate_query { | ||
| 985 | #=================================== | ||
| 986 | shift->_do_action( | ||
| 987 | 'validate_query', | ||
| 988 | { cmd => CMD_index_type, | ||
| 989 | postfix => '_validate/query', | ||
| 990 | data => { | ||
| 991 | query => ['query'], | ||
| 992 | queryb => ['queryb'], | ||
| 993 | }, | ||
| 994 | qs => { | ||
| 995 | q => ['string'], | ||
| 996 | explain => [ 'boolean', 1 ], | ||
| 997 | ignore_indices => IGNORE_INDICES, | ||
| 998 | }, | ||
| 999 | fixup => sub { | ||
| 1000 | my $args = $_[1]; | ||
| 1001 | if ( defined $args->{qs}{q} ) { | ||
| 1002 | die "Cannot specify q and query/queryb parameters.\n" | ||
| 1003 | if %{ $args->{body} }; | ||
| 1004 | delete $args->{body}; | ||
| 1005 | } | ||
| 1006 | else { | ||
| 1007 | eval { _query_fixup(@_); 1 } or do { | ||
| 1008 | die $@ if $@ =~ /Cannot specify queryb and query/; | ||
| 1009 | }; | ||
| 1010 | } | ||
| 1011 | }, | ||
| 1012 | }, | ||
| 1013 | @_ | ||
| 1014 | ); | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | #=================================== | ||
| 1018 | sub explain { | ||
| 1019 | #=================================== | ||
| 1020 | shift->_do_action( | ||
| 1021 | 'explain', | ||
| 1022 | { cmd => CMD_INDEX_TYPE_ID, | ||
| 1023 | postfix => '_explain', | ||
| 1024 | data => { | ||
| 1025 | query => ['query'], | ||
| 1026 | queryb => ['queryb'], | ||
| 1027 | }, | ||
| 1028 | qs => { | ||
| 1029 | preference => ['string'], | ||
| 1030 | routing => ['string'], | ||
| 1031 | q => ['string'], | ||
| 1032 | df => ['string'], | ||
| 1033 | analyzer => ['string'], | ||
| 1034 | analyze_wildcard => [ 'boolean', 1 ], | ||
| 1035 | default_operator => [ 'enum', [ 'OR', 'AND' ] ], | ||
| 1036 | fields => ['flatten'], | ||
| 1037 | lowercase_expanded_terms => [ 'boolean', undef, 0 ], | ||
| 1038 | lenient => [ 'boolean', 1 ], | ||
| 1039 | }, | ||
| 1040 | fixup => sub { | ||
| 1041 | my $args = $_[1]; | ||
| 1042 | if ( defined $args->{qs}{q} ) { | ||
| 1043 | die "Cannot specify q and query/queryb parameters.\n" | ||
| 1044 | if %{ $args->{body} }; | ||
| 1045 | delete $args->{body}; | ||
| 1046 | } | ||
| 1047 | else { | ||
| 1048 | $_[0]->_data_fixup( $args->{body} ); | ||
| 1049 | } | ||
| 1050 | }, | ||
| 1051 | }, | ||
| 1052 | @_ | ||
| 1053 | ); | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | #=================================== | ||
| 1057 | sub scroll { | ||
| 1058 | #=================================== | ||
| 1059 | shift()->_do_action( | ||
| 1060 | 'scroll', | ||
| 1061 | { cmd => [], | ||
| 1062 | prefix => '_search/scroll', | ||
| 1063 | qs => { | ||
| 1064 | scroll_id => ['string'], | ||
| 1065 | scroll => ['duration'], | ||
| 1066 | } | ||
| 1067 | }, | ||
| 1068 | @_ | ||
| 1069 | ); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | #=================================== | ||
| 1073 | sub scrolled_search { | ||
| 1074 | #=================================== | ||
| 1075 | my $self = shift; | ||
| 1076 | require Search::Elasticsearch::Compat::ScrolledSearch; | ||
| 1077 | return Search::Elasticsearch::Compat::ScrolledSearch->new( $self, @_ ); | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | #=================================== | ||
| 1081 | sub delete_by_query { | ||
| 1082 | #=================================== | ||
| 1083 | shift()->_do_action( | ||
| 1084 | 'delete_by_query', | ||
| 1085 | { %Search_Defn, | ||
| 1086 | method => 'DELETE', | ||
| 1087 | postfix => '_query', | ||
| 1088 | qs => { | ||
| 1089 | consistency => CONSISTENCY, | ||
| 1090 | replication => REPLICATION, | ||
| 1091 | routing => ['flatten'], | ||
| 1092 | }, | ||
| 1093 | %Query_Defn, | ||
| 1094 | fixup => sub { | ||
| 1095 | _query_fixup(@_); | ||
| 1096 | die "Missing required param 'query' or 'queryb'\n" | ||
| 1097 | unless %{ $_[1]->{body} }; | ||
| 1098 | }, | ||
| 1099 | }, | ||
| 1100 | @_ | ||
| 1101 | ); | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | #=================================== | ||
| 1105 | sub count { | ||
| 1106 | #=================================== | ||
| 1107 | shift()->_do_action( | ||
| 1108 | 'count', | ||
| 1109 | { %Search_Defn, | ||
| 1110 | postfix => '_count', | ||
| 1111 | %Query_Defn, | ||
| 1112 | qs => { | ||
| 1113 | routing => ['flatten'], | ||
| 1114 | ignore_indices => IGNORE_INDICES, | ||
| 1115 | }, | ||
| 1116 | fixup => sub { | ||
| 1117 | _query_fixup(@_); | ||
| 1118 | delete $_[1]{body} unless %{ $_[1]{body} }; | ||
| 1119 | }, | ||
| 1120 | }, | ||
| 1121 | @_ | ||
| 1122 | ); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | #=================================== | ||
| 1126 | sub mlt { | ||
| 1127 | #=================================== | ||
| 1128 | shift()->_do_action( | ||
| 1129 | 'mlt', | ||
| 1130 | { cmd => CMD_INDEX_TYPE_ID, | ||
| 1131 | method => 'GET', | ||
| 1132 | qs => { | ||
| 1133 | mlt_fields => ['flatten'], | ||
| 1134 | pct_terms_to_match => [ 'float', 'percent_terms_to_match' ], | ||
| 1135 | min_term_freq => ['int'], | ||
| 1136 | max_query_terms => ['int'], | ||
| 1137 | stop_words => ['flatten'], | ||
| 1138 | min_doc_freq => ['int'], | ||
| 1139 | max_doc_freq => ['int'], | ||
| 1140 | min_word_len => ['int'], | ||
| 1141 | max_word_len => ['int'], | ||
| 1142 | boost_terms => ['float'], | ||
| 1143 | routing => ['flatten'], | ||
| 1144 | search_indices => ['flatten'], | ||
| 1145 | search_from => ['int'], | ||
| 1146 | search_size => ['int'], | ||
| 1147 | search_type => SEARCH_TYPE, | ||
| 1148 | search_types => ['flatten'], | ||
| 1149 | search_scroll => ['string'], | ||
| 1150 | }, | ||
| 1151 | postfix => '_mlt', | ||
| 1152 | data => { | ||
| 1153 | explain => ['explain'], | ||
| 1154 | facets => ['facets'], | ||
| 1155 | fields => ['fields'], | ||
| 1156 | filter => ['filter'], | ||
| 1157 | filterb => ['filterb'], | ||
| 1158 | highlight => ['highlight'], | ||
| 1159 | indices_boost => ['indices_boost'], | ||
| 1160 | min_score => ['min_score'], | ||
| 1161 | script_fields => ['script_fields'], | ||
| 1162 | 'sort' => ['sort'], | ||
| 1163 | track_scores => ['track_scores'], | ||
| 1164 | }, | ||
| 1165 | fixup => sub { | ||
| 1166 | shift()->_to_dsl( { filterb => 'filter' }, $_[0]->{body} ); | ||
| 1167 | }, | ||
| 1168 | }, | ||
| 1169 | @_ | ||
| 1170 | ); | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | ################################## | ||
| 1174 | ## PERCOLATOR | ||
| 1175 | ################################## | ||
| 1176 | #=================================== | ||
| 1177 | sub create_percolator { | ||
| 1178 | #=================================== | ||
| 1179 | shift()->_do_action( | ||
| 1180 | 'create_percolator', | ||
| 1181 | { cmd => CMD_INDEX_PERC, | ||
| 1182 | prefix => '_percolator', | ||
| 1183 | method => 'PUT', | ||
| 1184 | data => { | ||
| 1185 | query => ['query'], | ||
| 1186 | queryb => ['queryb'], | ||
| 1187 | data => ['data'] | ||
| 1188 | }, | ||
| 1189 | fixup => sub { | ||
| 1190 | my $self = shift; | ||
| 1191 | my $args = shift; | ||
| 1192 | $self->_to_dsl( { queryb => 'query' }, $args->{body} ); | ||
| 1193 | die('create_percolator() requires either the query or queryb param' | ||
| 1194 | ) unless $args->{body}{query}; | ||
| 1195 | die 'The "data" param cannot include a "query" key' | ||
| 1196 | if $args->{body}{data}{query}; | ||
| 1197 | $args->{body} = { | ||
| 1198 | query => $args->{body}{query}, | ||
| 1199 | %{ $args->{body}{data} } | ||
| 1200 | }; | ||
| 1201 | }, | ||
| 1202 | }, | ||
| 1203 | @_ | ||
| 1204 | ); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | #=================================== | ||
| 1208 | sub delete_percolator { | ||
| 1209 | #=================================== | ||
| 1210 | shift()->_do_action( | ||
| 1211 | 'delete_percolator', | ||
| 1212 | { cmd => CMD_INDEX_PERC, | ||
| 1213 | prefix => '_percolator', | ||
| 1214 | method => 'DELETE', | ||
| 1215 | qs => { ignore_missing => [ 'boolean', 1 ], } | ||
| 1216 | }, | ||
| 1217 | @_ | ||
| 1218 | ); | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | #=================================== | ||
| 1222 | sub get_percolator { | ||
| 1223 | #=================================== | ||
| 1224 | shift()->_do_action( | ||
| 1225 | 'get_percolator', | ||
| 1226 | { cmd => CMD_INDEX_PERC, | ||
| 1227 | prefix => '_percolator', | ||
| 1228 | method => 'GET', | ||
| 1229 | qs => { ignore_missing => [ 'boolean', 1 ], }, | ||
| 1230 | post_process => sub { | ||
| 1231 | my $result = shift; | ||
| 1232 | return $result | ||
| 1233 | unless ref $result eq 'HASH'; | ||
| 1234 | return { | ||
| 1235 | index => $result->{_type}, | ||
| 1236 | percolator => $result->{_id}, | ||
| 1237 | query => delete $result->{_source}{query}, | ||
| 1238 | data => $result->{_source}, | ||
| 1239 | }; | ||
| 1240 | }, | ||
| 1241 | }, | ||
| 1242 | @_ | ||
| 1243 | ); | ||
| 1244 | } | ||
| 1245 | |||
| 1246 | #=================================== | ||
| 1247 | sub percolate { | ||
| 1248 | #=================================== | ||
| 1249 | shift()->_do_action( | ||
| 1250 | 'percolate', | ||
| 1251 | { cmd => CMD_INDEX_TYPE, | ||
| 1252 | postfix => '_percolate', | ||
| 1253 | method => 'GET', | ||
| 1254 | qs => { prefer_local => [ 'boolean', undef, 0 ] }, | ||
| 1255 | data => { doc => 'doc', query => ['query'] }, | ||
| 1256 | }, | ||
| 1257 | @_ | ||
| 1258 | ); | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | ################################## | ||
| 1262 | ## INDEX ADMIN | ||
| 1263 | ################################## | ||
| 1264 | |||
| 1265 | #=================================== | ||
| 1266 | sub index_status { | ||
| 1267 | #=================================== | ||
| 1268 | shift()->_do_action( | ||
| 1269 | 'index_status', | ||
| 1270 | { cmd => CMD_index, | ||
| 1271 | postfix => '_status', | ||
| 1272 | qs => { | ||
| 1273 | recovery => [ 'boolean', 1 ], | ||
| 1274 | snapshot => [ 'boolean', 1 ], | ||
| 1275 | ignore_indices => IGNORE_INDICES, | ||
| 1276 | }, | ||
| 1277 | }, | ||
| 1278 | @_ | ||
| 1279 | ); | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | #=================================== | ||
| 1283 | sub index_stats { | ||
| 1284 | #=================================== | ||
| 1285 | shift()->_do_action( | ||
| 1286 | 'index_stats', | ||
| 1287 | { cmd => CMD_index, | ||
| 1288 | postfix => '_stats', | ||
| 1289 | qs => { | ||
| 1290 | docs => [ 'boolean', 1, 0 ], | ||
| 1291 | store => [ 'boolean', 1, 0 ], | ||
| 1292 | indexing => [ 'boolean', 1, 0 ], | ||
| 1293 | get => [ 'boolean', 1, 0 ], | ||
| 1294 | search => [ 'boolean', 1, 0 ], | ||
| 1295 | clear => [ 'boolean', 1 ], | ||
| 1296 | all => [ 'boolean', 1 ], | ||
| 1297 | merge => [ 'boolean', 1 ], | ||
| 1298 | flush => [ 'boolean', 1 ], | ||
| 1299 | refresh => [ 'boolean', 1 ], | ||
| 1300 | types => ['flatten'], | ||
| 1301 | groups => ['flatten'], | ||
| 1302 | level => [ 'enum', [qw(shards)] ], | ||
| 1303 | ignore_indices => IGNORE_INDICES, | ||
| 1304 | }, | ||
| 1305 | }, | ||
| 1306 | @_ | ||
| 1307 | ); | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | #=================================== | ||
| 1311 | sub index_segments { | ||
| 1312 | #=================================== | ||
| 1313 | shift()->_do_action( | ||
| 1314 | 'index_segments', | ||
| 1315 | { cmd => CMD_index, | ||
| 1316 | postfix => '_segments', | ||
| 1317 | qs => { ignore_indices => IGNORE_INDICES, } | ||
| 1318 | }, | ||
| 1319 | @_ | ||
| 1320 | ); | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | #=================================== | ||
| 1324 | sub create_index { | ||
| 1325 | #=================================== | ||
| 1326 | shift()->_do_action( | ||
| 1327 | 'create_index', | ||
| 1328 | { method => 'PUT', | ||
| 1329 | cmd => CMD_INDEX, | ||
| 1330 | postfix => '', | ||
| 1331 | data => { | ||
| 1332 | settings => ['settings'], | ||
| 1333 | mappings => ['mappings'], | ||
| 1334 | warmers => ['warmers'], | ||
| 1335 | }, | ||
| 1336 | fixup => \&_warmer_fixup | ||
| 1337 | }, | ||
| 1338 | @_ | ||
| 1339 | ); | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | #=================================== | ||
| 1343 | sub delete_index { | ||
| 1344 | #=================================== | ||
| 1345 | shift()->_do_action( | ||
| 1346 | 'delete_index', | ||
| 1347 | { method => 'DELETE', | ||
| 1348 | cmd => CMD_INDICES, | ||
| 1349 | qs => { ignore_missing => [ 'boolean', 1 ], }, | ||
| 1350 | postfix => '' | ||
| 1351 | }, | ||
| 1352 | @_ | ||
| 1353 | ); | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | #=================================== | ||
| 1357 | sub index_exists { | ||
| 1358 | #=================================== | ||
| 1359 | shift()->_do_action( | ||
| 1360 | 'index_exists', | ||
| 1361 | { method => 'HEAD', | ||
| 1362 | cmd => CMD_index, | ||
| 1363 | fixup => sub { $_[1]->{qs}{ignore_missing} = 1 }, | ||
| 1364 | post_process => sub { $_[0] ? { ok => 1 } : undef }, | ||
| 1365 | }, | ||
| 1366 | @_ | ||
| 1367 | ); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | #=================================== | ||
| 1371 | sub open_index { | ||
| 1372 | #=================================== | ||
| 1373 | shift()->_do_action( | ||
| 1374 | 'open_index', | ||
| 1375 | { method => 'POST', | ||
| 1376 | cmd => CMD_INDEX, | ||
| 1377 | postfix => '_open' | ||
| 1378 | }, | ||
| 1379 | @_ | ||
| 1380 | ); | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | #=================================== | ||
| 1384 | sub close_index { | ||
| 1385 | #=================================== | ||
| 1386 | shift()->_do_action( | ||
| 1387 | 'close_index', | ||
| 1388 | { method => 'POST', | ||
| 1389 | cmd => CMD_INDEX, | ||
| 1390 | postfix => '_close' | ||
| 1391 | }, | ||
| 1392 | @_ | ||
| 1393 | ); | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | #=================================== | ||
| 1397 | sub aliases { | ||
| 1398 | #=================================== | ||
| 1399 | my ( $self, $params ) = parse_params(@_); | ||
| 1400 | my $actions = $params->{actions}; | ||
| 1401 | if ( defined $actions && ref $actions ne 'ARRAY' ) { | ||
| 1402 | $params->{actions} = [$actions]; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | $self->_do_action( | ||
| 1406 | 'aliases', | ||
| 1407 | { prefix => '_aliases', | ||
| 1408 | method => 'POST', | ||
| 1409 | cmd => [], | ||
| 1410 | data => { actions => 'actions' }, | ||
| 1411 | fixup => sub { | ||
| 1412 | my $self = shift; | ||
| 1413 | my $args = shift; | ||
| 1414 | my @actions = @{ $args->{body}{actions} }; | ||
| 1415 | for (@actions) { | ||
| 1416 | my ( $key, $value ) = %$_; | ||
| 1417 | $value = {%$value}; | ||
| 1418 | $self->_to_dsl( { filterb => 'filter' }, $value ); | ||
| 1419 | $_ = { $key => $value }; | ||
| 1420 | } | ||
| 1421 | $args->{body}{actions} = \@actions; | ||
| 1422 | }, | ||
| 1423 | }, | ||
| 1424 | $params | ||
| 1425 | ); | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | #=================================== | ||
| 1429 | sub get_aliases { | ||
| 1430 | #=================================== | ||
| 1431 | shift->_do_action( | ||
| 1432 | 'aliases', | ||
| 1433 | { postfix => '_aliases', | ||
| 1434 | cmd => CMD_index, | ||
| 1435 | qs => { ignore_missing => [ 'boolean', 1 ] }, | ||
| 1436 | }, | ||
| 1437 | @_ | ||
| 1438 | ); | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | #=================================== | ||
| 1442 | sub create_warmer { | ||
| 1443 | #=================================== | ||
| 1444 | shift()->_do_action( | ||
| 1445 | 'create_warmer', | ||
| 1446 | { method => 'PUT', | ||
| 1447 | cmd => CMD_index_type, | ||
| 1448 | postfix => '_warmer/', | ||
| 1449 | data => { | ||
| 1450 | warmer => 'warmer', | ||
| 1451 | facets => ['facets'], | ||
| 1452 | filter => ['filter'], | ||
| 1453 | filterb => ['filterb'], | ||
| 1454 | script_fields => ['script_fields'], | ||
| 1455 | 'sort' => ['sort'], | ||
| 1456 | query => ['query'], | ||
| 1457 | queryb => ['queryb'], | ||
| 1458 | }, | ||
| 1459 | fixup => sub { | ||
| 1460 | my ( $self, $args ) = @_; | ||
| 1461 | $args->{path} .= delete $args->{body}{warmer}; | ||
| 1462 | $self->_data_fixup( $args->{body} ); | ||
| 1463 | }, | ||
| 1464 | }, | ||
| 1465 | @_ | ||
| 1466 | ); | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | #=================================== | ||
| 1470 | sub warmer { | ||
| 1471 | #=================================== | ||
| 1472 | my ( $self, $params ) = parse_params(@_); | ||
| 1473 | $params->{warmer} = '*' | ||
| 1474 | unless defined $params->{warmer} and length $params->{warmer}; | ||
| 1475 | |||
| 1476 | $self->_do_action( | ||
| 1477 | 'warmer', | ||
| 1478 | { method => 'GET', | ||
| 1479 | cmd => CMD_indices, | ||
| 1480 | postfix => '_warmer/', | ||
| 1481 | data => { warmer => ['warmer'] }, | ||
| 1482 | qs => { ignore_missing => [ 'boolean', 1 ] }, | ||
| 1483 | fixup => sub { | ||
| 1484 | my ( $self, $args ) = @_; | ||
| 1485 | $args->{path} .= delete $args->{body}{warmer}; | ||
| 1486 | }, | ||
| 1487 | }, | ||
| 1488 | $params | ||
| 1489 | ); | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | #=================================== | ||
| 1493 | sub delete_warmer { | ||
| 1494 | #=================================== | ||
| 1495 | shift()->_do_action( | ||
| 1496 | 'delete_warmer', | ||
| 1497 | { method => 'DELETE', | ||
| 1498 | cmd => CMD_INDICES, | ||
| 1499 | postfix => '_warmer/', | ||
| 1500 | data => { warmer => 'warmer' }, | ||
| 1501 | qs => { ignore_missing => [ 'boolean', 1 ] }, | ||
| 1502 | fixup => sub { | ||
| 1503 | my ( $self, $args ) = @_; | ||
| 1504 | $args->{path} .= delete $args->{body}{warmer}; | ||
| 1505 | }, | ||
| 1506 | }, | ||
| 1507 | @_ | ||
| 1508 | ); | ||
| 1509 | } | ||
| 1510 | |||
| 1511 | #=================================== | ||
| 1512 | sub create_index_template { | ||
| 1513 | #=================================== | ||
| 1514 | shift()->_do_action( | ||
| 1515 | 'create_index_template', | ||
| 1516 | { method => 'PUT', | ||
| 1517 | cmd => CMD_NAME, | ||
| 1518 | prefix => '_template', | ||
| 1519 | data => { | ||
| 1520 | template => 'template', | ||
| 1521 | settings => ['settings'], | ||
| 1522 | mappings => ['mappings'], | ||
| 1523 | warmers => ['warmers'], | ||
| 1524 | order => ['order'], | ||
| 1525 | }, | ||
| 1526 | fixup => \&_warmer_fixup | ||
| 1527 | }, | ||
| 1528 | @_ | ||
| 1529 | ); | ||
| 1530 | } | ||
| 1531 | |||
| 1532 | #=================================== | ||
| 1533 | sub delete_index_template { | ||
| 1534 | #=================================== | ||
| 1535 | shift()->_do_action( | ||
| 1536 | 'delete_index_template', | ||
| 1537 | { method => 'DELETE', | ||
| 1538 | cmd => CMD_NAME, | ||
| 1539 | prefix => '_template', | ||
| 1540 | qs => { ignore_missing => [ 'boolean', 1 ] }, | ||
| 1541 | }, | ||
| 1542 | @_ | ||
| 1543 | ); | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | #=================================== | ||
| 1547 | sub index_template { | ||
| 1548 | #=================================== | ||
| 1549 | shift()->_do_action( | ||
| 1550 | 'index_template', | ||
| 1551 | { method => 'GET', | ||
| 1552 | cmd => CMD_NAME, | ||
| 1553 | prefix => '_template', | ||
| 1554 | }, | ||
| 1555 | @_ | ||
| 1556 | ); | ||
| 1557 | } | ||
| 1558 | |||
| 1559 | #=================================== | ||
| 1560 | sub flush_index { | ||
| 1561 | #=================================== | ||
| 1562 | shift()->_do_action( | ||
| 1563 | 'flush_index', | ||
| 1564 | { method => 'POST', | ||
| 1565 | cmd => CMD_index, | ||
| 1566 | postfix => '_flush', | ||
| 1567 | qs => { | ||
| 1568 | refresh => [ 'boolean', 1 ], | ||
| 1569 | full => [ 'boolean', 1 ], | ||
| 1570 | ignore_indices => IGNORE_INDICES, | ||
| 1571 | }, | ||
| 1572 | }, | ||
| 1573 | @_ | ||
| 1574 | ); | ||
| 1575 | } | ||
| 1576 | |||
| 1577 | #=================================== | ||
| 1578 | sub refresh_index { | ||
| 1579 | #=================================== | ||
| 1580 | shift()->_do_action( | ||
| 1581 | 'refresh_index', | ||
| 1582 | { method => 'POST', | ||
| 1583 | cmd => CMD_index, | ||
| 1584 | postfix => '_refresh', | ||
| 1585 | qs => { ignore_indices => IGNORE_INDICES, } | ||
| 1586 | }, | ||
| 1587 | @_ | ||
| 1588 | ); | ||
| 1589 | } | ||
| 1590 | |||
| 1591 | #=================================== | ||
| 1592 | sub optimize_index { | ||
| 1593 | #=================================== | ||
| 1594 | shift()->_do_action( | ||
| 1595 | 'optimize_index', | ||
| 1596 | { method => 'POST', | ||
| 1597 | cmd => CMD_index, | ||
| 1598 | postfix => '_optimize', | ||
| 1599 | qs => { | ||
| 1600 | only_deletes => | ||
| 1601 | [ 'boolean', [ only_expunge_deletes => 'true' ] ], | ||
| 1602 | max_num_segments => ['int'], | ||
| 1603 | refresh => [ 'boolean', undef, 0 ], | ||
| 1604 | flush => [ 'boolean', undef, 0 ], | ||
| 1605 | wait_for_merge => [ 'boolean', undef, 0 ], | ||
| 1606 | ignore_indices => IGNORE_INDICES, | ||
| 1607 | }, | ||
| 1608 | }, | ||
| 1609 | @_ | ||
| 1610 | ); | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | #=================================== | ||
| 1614 | sub snapshot_index { | ||
| 1615 | #=================================== | ||
| 1616 | shift()->_do_action( | ||
| 1617 | 'snapshot_index', | ||
| 1618 | { method => 'POST', | ||
| 1619 | cmd => CMD_index, | ||
| 1620 | postfix => '_gateway/snapshot', | ||
| 1621 | qs => { ignore_indices => IGNORE_INDICES, } | ||
| 1622 | }, | ||
| 1623 | @_ | ||
| 1624 | ); | ||
| 1625 | } | ||
| 1626 | |||
| 1627 | #=================================== | ||
| 1628 | sub gateway_snapshot { | ||
| 1629 | #=================================== | ||
| 1630 | shift()->_do_action( | ||
| 1631 | 'gateway_snapshot', | ||
| 1632 | { method => 'POST', | ||
| 1633 | cmd => CMD_index, | ||
| 1634 | postfix => '_gateway/snapshot' | ||
| 1635 | }, | ||
| 1636 | @_ | ||
| 1637 | ); | ||
| 1638 | } | ||
| 1639 | |||
| 1640 | #=================================== | ||
| 1641 | sub put_mapping { | ||
| 1642 | #=================================== | ||
| 1643 | my ( $self, $params ) = parse_params(@_); | ||
| 1644 | my %defn = ( | ||
| 1645 | data => { mapping => 'mapping' }, | ||
| 1646 | deprecated => { | ||
| 1647 | dynamic => ['dynamic'], | ||
| 1648 | dynamic_templates => ['dynamic_templates'], | ||
| 1649 | properties => ['properties'], | ||
| 1650 | _all => ['_all'], | ||
| 1651 | _analyzer => ['_analyzer'], | ||
| 1652 | _boost => ['_boost'], | ||
| 1653 | _id => ['_id'], | ||
| 1654 | _index => ['_index'], | ||
| 1655 | _meta => ['_meta'], | ||
| 1656 | _parent => ['_parent'], | ||
| 1657 | _routing => ['_routing'], | ||
| 1658 | _source => ['_source'], | ||
| 1659 | }, | ||
| 1660 | ); | ||
| 1661 | |||
| 1662 | $defn{deprecated}{mapping} = undef | ||
| 1663 | if !$params->{mapping} && grep { exists $params->{$_} } | ||
| 1664 | keys %{ $defn{deprecated} }; | ||
| 1665 | |||
| 1666 | my $type = $params->{type} || $self->{_default}{type}; | ||
| 1667 | $self->_do_action( | ||
| 1668 | 'put_mapping', | ||
| 1669 | { method => 'PUT', | ||
| 1670 | cmd => CMD_index_TYPE, | ||
| 1671 | postfix => '_mapping', | ||
| 1672 | qs => { ignore_conflicts => [ 'boolean', 1 ] }, | ||
| 1673 | %defn, | ||
| 1674 | fixup => sub { | ||
| 1675 | my $args = $_[1]; | ||
| 1676 | my $mapping = $args->{body}{mapping} || $args->{body}; | ||
| 1677 | $args->{body} = { $type => $mapping }; | ||
| 1678 | }, | ||
| 1679 | }, | ||
| 1680 | $params | ||
| 1681 | ); | ||
| 1682 | } | ||
| 1683 | |||
| 1684 | #=================================== | ||
| 1685 | sub delete_mapping { | ||
| 1686 | #=================================== | ||
| 1687 | my ( $self, $params ) = parse_params(@_); | ||
| 1688 | |||
| 1689 | $self->_do_action( | ||
| 1690 | 'delete_mapping', | ||
| 1691 | { method => 'DELETE', | ||
| 1692 | cmd => CMD_INDICES_TYPE, | ||
| 1693 | qs => { ignore_missing => [ 'boolean', 1 ], } | ||
| 1694 | }, | ||
| 1695 | $params | ||
| 1696 | ); | ||
| 1697 | } | ||
| 1698 | |||
| 1699 | #=================================== | ||
| 1700 | sub mapping { | ||
| 1701 | #=================================== | ||
| 1702 | my ( $self, $params ) = parse_params(@_); | ||
| 1703 | |||
| 1704 | $self->_do_action( | ||
| 1705 | 'mapping', | ||
| 1706 | { method => 'GET', | ||
| 1707 | cmd => CMD_index_type, | ||
| 1708 | postfix => '_mapping', | ||
| 1709 | qs => { ignore_missing => [ 'boolean', 1 ], } | ||
| 1710 | }, | ||
| 1711 | $params | ||
| 1712 | ); | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | #=================================== | ||
| 1716 | sub type_exists { | ||
| 1717 | #=================================== | ||
| 1718 | shift()->_do_action( | ||
| 1719 | 'type_exists', | ||
| 1720 | { method => 'HEAD', | ||
| 1721 | cmd => CMD_index_types, | ||
| 1722 | qs => { ignore_indices => IGNORE_INDICES, }, | ||
| 1723 | fixup => sub { $_[1]->{qs}{ignore_missing} = 1 }, | ||
| 1724 | post_process => sub { $_[0] ? { ok => 1 } : undef }, | ||
| 1725 | }, | ||
| 1726 | @_ | ||
| 1727 | ); | ||
| 1728 | } | ||
| 1729 | |||
| 1730 | #=================================== | ||
| 1731 | sub clear_cache { | ||
| 1732 | #=================================== | ||
| 1733 | shift()->_do_action( | ||
| 1734 | 'clear_cache', | ||
| 1735 | { method => 'POST', | ||
| 1736 | cmd => CMD_index, | ||
| 1737 | postfix => '_cache/clear', | ||
| 1738 | qs => { | ||
| 1739 | id => [ 'boolean', 1 ], | ||
| 1740 | filter => [ 'boolean', 1 ], | ||
| 1741 | field_data => [ 'boolean', 1 ], | ||
| 1742 | bloom => [ 'boolean', 1 ], | ||
| 1743 | fields => ['flatten'], | ||
| 1744 | ignore_indices => IGNORE_INDICES, | ||
| 1745 | } | ||
| 1746 | }, | ||
| 1747 | @_ | ||
| 1748 | ); | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | #=================================== | ||
| 1752 | sub index_settings { | ||
| 1753 | #=================================== | ||
| 1754 | my ( $self, $params ) = parse_params(@_); | ||
| 1755 | |||
| 1756 | $self->_do_action( | ||
| 1757 | 'index_settings', | ||
| 1758 | { method => 'GET', | ||
| 1759 | cmd => CMD_index, | ||
| 1760 | postfix => '_settings' | ||
| 1761 | }, | ||
| 1762 | $params | ||
| 1763 | ); | ||
| 1764 | } | ||
| 1765 | |||
| 1766 | #=================================== | ||
| 1767 | sub update_index_settings { | ||
| 1768 | #=================================== | ||
| 1769 | my ( $self, $params ) = parse_params(@_); | ||
| 1770 | |||
| 1771 | $self->_do_action( | ||
| 1772 | 'update_index_settings', | ||
| 1773 | { method => 'PUT', | ||
| 1774 | cmd => CMD_index, | ||
| 1775 | postfix => '_settings', | ||
| 1776 | data => { index => 'settings' } | ||
| 1777 | }, | ||
| 1778 | $params | ||
| 1779 | ); | ||
| 1780 | } | ||
| 1781 | |||
| 1782 | ################################## | ||
| 1783 | ## RIVER MANAGEMENT | ||
| 1784 | ################################## | ||
| 1785 | |||
| 1786 | #=================================== | ||
| 1787 | sub create_river { | ||
| 1788 | #=================================== | ||
| 1789 | my ( $self, $params ) = parse_params(@_); | ||
| 1790 | my $type = $params->{type} | ||
| 1791 | or throw( 'Param', 'No river type specified', $params ); | ||
| 1792 | my $data = { type => 'type', index => ['index'], $type => [$type] }; | ||
| 1793 | $self->_do_action( | ||
| 1794 | 'create_river', | ||
| 1795 | { method => 'PUT', | ||
| 1796 | prefix => '_river', | ||
| 1797 | cmd => CMD_RIVER, | ||
| 1798 | postfix => '_meta', | ||
| 1799 | data => $data | ||
| 1800 | }, | ||
| 1801 | $params | ||
| 1802 | ); | ||
| 1803 | } | ||
| 1804 | |||
| 1805 | #=================================== | ||
| 1806 | sub get_river { | ||
| 1807 | #=================================== | ||
| 1808 | my ( $self, $params ) = parse_params(@_); | ||
| 1809 | $self->_do_action( | ||
| 1810 | 'get_river', | ||
| 1811 | { method => 'GET', | ||
| 1812 | prefix => '_river', | ||
| 1813 | cmd => CMD_RIVER, | ||
| 1814 | postfix => '_meta', | ||
| 1815 | qs => { ignore_missing => [ 'boolean', 1 ] } | ||
| 1816 | }, | ||
| 1817 | $params | ||
| 1818 | ); | ||
| 1819 | } | ||
| 1820 | |||
| 1821 | #=================================== | ||
| 1822 | sub delete_river { | ||
| 1823 | #=================================== | ||
| 1824 | my ( $self, $params ) = parse_params(@_); | ||
| 1825 | $self->_do_action( | ||
| 1826 | 'delete_river', | ||
| 1827 | { method => 'DELETE', | ||
| 1828 | prefix => '_river', | ||
| 1829 | cmd => CMD_RIVER, | ||
| 1830 | }, | ||
| 1831 | $params | ||
| 1832 | ); | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | #=================================== | ||
| 1836 | sub river_status { | ||
| 1837 | #=================================== | ||
| 1838 | my ( $self, $params ) = parse_params(@_); | ||
| 1839 | $self->_do_action( | ||
| 1840 | 'river_status', | ||
| 1841 | { method => 'GET', | ||
| 1842 | prefix => '_river', | ||
| 1843 | cmd => CMD_RIVER, | ||
| 1844 | postfix => '_status', | ||
| 1845 | qs => { ignore_missing => [ 'boolean', 1 ] } | ||
| 1846 | }, | ||
| 1847 | $params | ||
| 1848 | ); | ||
| 1849 | } | ||
| 1850 | |||
| 1851 | ################################## | ||
| 1852 | ## CLUSTER MANAGEMENT | ||
| 1853 | ################################## | ||
| 1854 | |||
| 1855 | #=================================== | ||
| 1856 | sub cluster_state { | ||
| 1857 | #=================================== | ||
| 1858 | shift()->_do_action( | ||
| 1859 | 'cluster_state', | ||
| 1860 | { prefix => '_cluster/state', | ||
| 1861 | qs => { | ||
| 1862 | filter_blocks => [ 'boolean', 1 ], | ||
| 1863 | filter_nodes => [ 'boolean', 1 ], | ||
| 1864 | filter_metadata => [ 'boolean', 1 ], | ||
| 1865 | filter_routing_table => [ 'boolean', 1 ], | ||
| 1866 | filter_indices => ['flatten'], | ||
| 1867 | } | ||
| 1868 | |||
| 1869 | }, | ||
| 1870 | @_ | ||
| 1871 | ); | ||
| 1872 | } | ||
| 1873 | |||
| 1874 | #=================================== | ||
| 1875 | sub current_server_version { | ||
| 1876 | #=================================== | ||
| 1877 | shift()->_do_action( | ||
| 1878 | 'current_server_version', | ||
| 1879 | { cmd => CMD_NONE, | ||
| 1880 | prefix => '', | ||
| 1881 | post_process => sub { | ||
| 1882 | return shift->{version}; | ||
| 1883 | }, | ||
| 1884 | } | ||
| 1885 | ); | ||
| 1886 | } | ||
| 1887 | |||
| 1888 | #=================================== | ||
| 1889 | sub nodes { | ||
| 1890 | #=================================== | ||
| 1891 | shift()->_do_action( | ||
| 1892 | 'nodes', | ||
| 1893 | { prefix => '_cluster/nodes', | ||
| 1894 | cmd => CMD_nodes, | ||
| 1895 | qs => { | ||
| 1896 | settings => [ 'boolean', 1 ], | ||
| 1897 | http => [ 'boolean', 1 ], | ||
| 1898 | jvm => [ 'boolean', 1 ], | ||
| 1899 | network => [ 'boolean', 1 ], | ||
| 1900 | os => [ 'boolean', 1 ], | ||
| 1901 | process => [ 'boolean', 1 ], | ||
| 1902 | thread_pool => [ 'boolean', 1 ], | ||
| 1903 | transport => [ 'boolean', 1 ], | ||
| 1904 | }, | ||
| 1905 | }, | ||
| 1906 | @_ | ||
| 1907 | ); | ||
| 1908 | } | ||
| 1909 | |||
| 1910 | #=================================== | ||
| 1911 | sub nodes_stats { | ||
| 1912 | #=================================== | ||
| 1913 | shift()->_do_action( | ||
| 1914 | 'nodes', | ||
| 1915 | { prefix => '_cluster/nodes', | ||
| 1916 | postfix => 'stats', | ||
| 1917 | cmd => CMD_nodes, | ||
| 1918 | qs => { | ||
| 1919 | indices => [ 'boolean', 1, 0 ], | ||
| 1920 | clear => [ 'boolean', 1 ], | ||
| 1921 | all => [ 'boolean', 1 ], | ||
| 1922 | fs => [ 'boolean', 1 ], | ||
| 1923 | http => [ 'boolean', 1 ], | ||
| 1924 | jvm => [ 'boolean', 1 ], | ||
| 1925 | network => [ 'boolean', 1 ], | ||
| 1926 | os => [ 'boolean', 1 ], | ||
| 1927 | process => [ 'boolean', 1 ], | ||
| 1928 | thread_pool => [ 'boolean', 1 ], | ||
| 1929 | transport => [ 'boolean', 1 ], | ||
| 1930 | }, | ||
| 1931 | }, | ||
| 1932 | @_ | ||
| 1933 | ); | ||
| 1934 | } | ||
| 1935 | |||
| 1936 | #=================================== | ||
| 1937 | sub shutdown { | ||
| 1938 | #=================================== | ||
| 1939 | shift()->_do_action( | ||
| 1940 | 'shutdown', | ||
| 1941 | { method => 'POST', | ||
| 1942 | prefix => '_cluster/nodes', | ||
| 1943 | cmd => CMD_nodes, | ||
| 1944 | postfix => '_shutdown', | ||
| 1945 | qs => { delay => ['duration'] } | ||
| 1946 | }, | ||
| 1947 | @_ | ||
| 1948 | ); | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | #=================================== | ||
| 1952 | sub restart { | ||
| 1953 | #=================================== | ||
| 1954 | shift()->_do_action( | ||
| 1955 | 'shutdown', | ||
| 1956 | { method => 'POST', | ||
| 1957 | prefix => '_cluster/nodes', | ||
| 1958 | cmd => CMD_nodes, | ||
| 1959 | postfix => '_restart', | ||
| 1960 | qs => { delay => ['duration'] } | ||
| 1961 | }, | ||
| 1962 | @_ | ||
| 1963 | ); | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | #=================================== | ||
| 1967 | sub cluster_health { | ||
| 1968 | #=================================== | ||
| 1969 | shift()->_do_action( | ||
| 1970 | 'cluster_health', | ||
| 1971 | { prefix => '_cluster/health', | ||
| 1972 | cmd => CMD_index, | ||
| 1973 | qs => { | ||
| 1974 | level => [ 'enum', [qw(cluster indices shards)] ], | ||
| 1975 | wait_for_status => [ 'enum', [qw(green yellow red)] ], | ||
| 1976 | wait_for_relocating_shards => ['int'], | ||
| 1977 | wait_for_nodes => ['string'], | ||
| 1978 | timeout => ['duration'] | ||
| 1979 | } | ||
| 1980 | }, | ||
| 1981 | @_ | ||
| 1982 | ); | ||
| 1983 | } | ||
| 1984 | |||
| 1985 | #=================================== | ||
| 1986 | sub cluster_settings { | ||
| 1987 | #=================================== | ||
| 1988 | my ( $self, $params ) = parse_params(@_); | ||
| 1989 | |||
| 1990 | $self->_do_action( | ||
| 1991 | 'cluster_settings', | ||
| 1992 | { method => 'GET', | ||
| 1993 | cmd => CMD_NONE, | ||
| 1994 | postfix => '_cluster/settings' | ||
| 1995 | }, | ||
| 1996 | $params | ||
| 1997 | ); | ||
| 1998 | } | ||
| 1999 | |||
| 2000 | #=================================== | ||
| 2001 | sub update_cluster_settings { | ||
| 2002 | #=================================== | ||
| 2003 | my ( $self, $params ) = parse_params(@_); | ||
| 2004 | |||
| 2005 | $self->_do_action( | ||
| 2006 | 'update_cluster_settings', | ||
| 2007 | { method => 'PUT', | ||
| 2008 | cmd => CMD_NONE, | ||
| 2009 | postfix => '_cluster/settings', | ||
| 2010 | data => { | ||
| 2011 | persistent => ['persistent'], | ||
| 2012 | transient => ['transient'] | ||
| 2013 | } | ||
| 2014 | }, | ||
| 2015 | $params | ||
| 2016 | ); | ||
| 2017 | } | ||
| 2018 | |||
| 2019 | #=================================== | ||
| 2020 | sub cluster_reroute { | ||
| 2021 | #=================================== | ||
| 2022 | my ( $self, $params ) = parse_params(@_); | ||
| 2023 | $params->{commands} = [ $params->{commands} ] | ||
| 2024 | if $params->{commands} and ref( $params->{commands} ) ne 'ARRAY'; | ||
| 2025 | |||
| 2026 | $self->_do_action( | ||
| 2027 | 'cluster_reroute', | ||
| 2028 | { prefix => '_cluster/reroute', | ||
| 2029 | cmd => [], | ||
| 2030 | method => 'POST', | ||
| 2031 | data => { commands => ['commands'] }, | ||
| 2032 | qs => { dry_run => [ 'boolean', 1 ], }, | ||
| 2033 | }, | ||
| 2034 | $params | ||
| 2035 | ); | ||
| 2036 | } | ||
| 2037 | |||
| 2038 | ################################## | ||
| 2039 | ## FLAGS | ||
| 2040 | ################################## | ||
| 2041 | |||
| 2042 | #=================================== | ||
| 2043 | sub camel_case { | ||
| 2044 | #=================================== | ||
| 2045 | my $self = shift; | ||
| 2046 | if (@_) { | ||
| 2047 | if ( shift() ) { | ||
| 2048 | $self->{_base_qs}{case} = 'camelCase'; | ||
| 2049 | } | ||
| 2050 | else { | ||
| 2051 | delete $self->{_base_qs}{case}; | ||
| 2052 | } | ||
| 2053 | } | ||
| 2054 | return $self->{_base_qs}{case} ? 1 : 0; | ||
| 2055 | } | ||
| 2056 | |||
| 2057 | #=================================== | ||
| 2058 | sub error_trace { | ||
| 2059 | #=================================== | ||
| 2060 | my $self = shift; | ||
| 2061 | if (@_) { | ||
| 2062 | if ( shift() ) { | ||
| 2063 | $self->{_base_qs}{error_trace} = 'true'; | ||
| 2064 | } | ||
| 2065 | else { | ||
| 2066 | delete $self->{_base_qs}{error_trace}; | ||
| 2067 | } | ||
| 2068 | } | ||
| 2069 | return $self->{_base_qs}{error_trace} ? 1 : 0; | ||
| 2070 | } | ||
| 2071 | |||
| 2072 | ################################## | ||
| 2073 | ## INTERNAL | ||
| 2074 | ################################## | ||
| 2075 | |||
| 2076 | #=================================== | ||
| 2077 | sub parse_request { shift->_doc_action(@_) } | ||
| 2078 | #=================================== | ||
| 2079 | |||
| 2080 | #=================================== | ||
| 2081 | sub _do_action { | ||
| 2082 | #=================================== | ||
| 2083 | my $self = shift; | ||
| 2084 | my $action = shift || ''; | ||
| 2085 | my $defn = shift || {}; | ||
| 2086 | my $original_params = ref $_[0] eq 'HASH' ? { %{ shift() } } : {@_}; | ||
| 2087 | |||
| 2088 | my $error; | ||
| 2089 | |||
| 2090 | my $params = {%$original_params}; | ||
| 2091 | my %args = ( method => $defn->{method} || 'GET' ); | ||
| 2092 | $args{as_json} = delete $params->{as_json}; | ||
| 2093 | |||
| 2094 | eval { | ||
| 2095 | $args{path} | ||
| 2096 | = $self->_build_cmd( $params, @{$defn}{qw(prefix cmd postfix)} ); | ||
| 2097 | $args{qs} = $self->_build_qs( $params, $defn->{qs} ); | ||
| 2098 | $args{body} | ||
| 2099 | = $self->_build_data( $params, @{$defn}{ 'data', 'deprecated' } ); | ||
| 2100 | $args{ignore} = 404 if delete $args{qs}{ignore_missing}; | ||
| 2101 | if ( my $fixup = $defn->{fixup} ) { | ||
| 2102 | $fixup->( $self, \%args ); | ||
| 2103 | } | ||
| 2104 | die "Unknown parameters: " . join( ', ', keys %$params ) . "\n" | ||
| 2105 | if keys %$params; | ||
| 2106 | 1; | ||
| 2107 | } or $error = $@ || 'Unknown error'; | ||
| 2108 | |||
| 2109 | $args{post_process} = $defn->{post_process}; | ||
| 2110 | if ($error) { | ||
| 2111 | die $error if ref $error; | ||
| 2112 | throw( | ||
| 2113 | 'Param', | ||
| 2114 | $error . $self->_usage( $action, $defn ), | ||
| 2115 | { params => $original_params } | ||
| 2116 | ); | ||
| 2117 | } | ||
| 2118 | if ( my $skip = $args{skip} ) { | ||
| 2119 | return $self->_skip_request( $args{as_json}, $skip ); | ||
| 2120 | } | ||
| 2121 | $args{serialize} ||= 'std'; | ||
| 2122 | $args{mime_type} = 'application/json'; | ||
| 2123 | $self->request( \%args ); | ||
| 2124 | } | ||
| 2125 | |||
| 2126 | #=================================== | ||
| 2127 | sub _skip_request { | ||
| 2128 | #=================================== | ||
| 2129 | my $self = shift; | ||
| 2130 | my $as_json = shift; | ||
| 2131 | my $result = shift; | ||
| 2132 | return $result unless $as_json; | ||
| 2133 | return $self->JSON->encode($result); | ||
| 2134 | } | ||
| 2135 | |||
| 2136 | #=================================== | ||
| 2137 | sub _usage { | ||
| 2138 | #=================================== | ||
| 2139 | my $self = shift; | ||
| 2140 | my $action = shift; | ||
| 2141 | my $defn = shift; | ||
| 2142 | |||
| 2143 | my $usage = "Usage for '$action()':\n"; | ||
| 2144 | my @cmd = @{ $defn->{cmd} || [] }; | ||
| 2145 | while ( my $key = shift @cmd ) { | ||
| 2146 | my $type = shift @cmd; | ||
| 2147 | my $arg_format | ||
| 2148 | = $type == ONE_REQ ? "\$$key" | ||
| 2149 | : $type == ONE_OPT ? "\$$key" | ||
| 2150 | : "\$$key | [\$${key}_1,\$${key}_n]"; | ||
| 2151 | |||
| 2152 | my $required | ||
| 2153 | = ( $type == ONE_REQ or $type == MULTI_REQ ) | ||
| 2154 | ? 'required' | ||
| 2155 | : 'optional'; | ||
| 2156 | $usage .= sprintf( " - %-26s => %-45s # %s\n", | ||
| 2157 | $key, $arg_format, $required ); | ||
| 2158 | } | ||
| 2159 | |||
| 2160 | if ( my $data = $defn->{body} ) { | ||
| 2161 | my @keys = sort { $a->[0] cmp $b->[0] } | ||
| 2162 | map { ref $_ ? [ $_->[0], 'optional' ] : [ $_, 'required' ] } | ||
| 2163 | values %$data; | ||
| 2164 | |||
| 2165 | for (@keys) { | ||
| 2166 | $usage .= sprintf( | ||
| 2167 | " - %-26s => %-45s # %s\n", | ||
| 2168 | $_->[0], '{' . $_->[0] . '}', | ||
| 2169 | $_->[1] | ||
| 2170 | ); | ||
| 2171 | } | ||
| 2172 | } | ||
| 2173 | |||
| 2174 | if ( my $qs = $defn->{qs} ) { | ||
| 2175 | for ( sort keys %$qs ) { | ||
| 2176 | my $arg_format = $QS_Format{ $qs->{$_}[0] }; | ||
| 2177 | my @extra; | ||
| 2178 | $arg_format = $arg_format->( $_, $qs->{$_} ) | ||
| 2179 | if ref $arg_format; | ||
| 2180 | if ( length($arg_format) > 45 ) { | ||
| 2181 | ( $arg_format, @extra ) = split / [|] /, $arg_format; | ||
| 2182 | } | ||
| 2183 | $usage .= sprintf( " - %-26s => %-45s # optional\n", $_, | ||
| 2184 | $arg_format ); | ||
| 2185 | $usage .= ( ' ' x 34 ) . " | $_\n" for @extra; | ||
| 2186 | } | ||
| 2187 | } | ||
| 2188 | |||
| 2189 | return $usage; | ||
| 2190 | } | ||
| 2191 | |||
| 2192 | #=================================== | ||
| 2193 | sub _build_qs { | ||
| 2194 | #=================================== | ||
| 2195 | my $self = shift; | ||
| 2196 | my $params = shift; | ||
| 2197 | my $defn = shift || {}; | ||
| 2198 | my %qs = %{ $self->{_base_qs} }; | ||
| 2199 | foreach my $key ( keys %$defn ) { | ||
| 2200 | my ( $format_name, @args ) = @{ $defn->{$key} || [] }; | ||
| 2201 | $format_name ||= ''; | ||
| 2202 | |||
| 2203 | next unless exists $params->{$key}; | ||
| 2204 | |||
| 2205 | my $formatter = $QS_Formatter{$format_name} | ||
| 2206 | or die "Unknown QS formatter '$format_name'"; | ||
| 2207 | |||
| 2208 | my $val = $formatter->( $key, delete $params->{$key}, @args ) | ||
| 2209 | or next; | ||
| 2210 | $qs{ $val->[0] } = $val->[1]; | ||
| 2211 | } | ||
| 2212 | return \%qs; | ||
| 2213 | } | ||
| 2214 | |||
| 2215 | #=================================== | ||
| 2216 | sub _build_data { | ||
| 2217 | #=================================== | ||
| 2218 | my $self = shift; | ||
| 2219 | my $params = shift; | ||
| 2220 | my $defn = shift or return; | ||
| 2221 | |||
| 2222 | if ( my $deprecated = shift ) { | ||
| 2223 | $defn = { %$defn, %$deprecated }; | ||
| 2224 | } | ||
| 2225 | |||
| 2226 | my %data; | ||
| 2227 | KEY: while ( my ( $key, $source ) = each %$defn ) { | ||
| 2228 | next unless defined $source; | ||
| 2229 | if ( ref $source eq 'ARRAY' ) { | ||
| 2230 | foreach (@$source) { | ||
| 2231 | my $val = delete $params->{$_}; | ||
| 2232 | next unless defined $val; | ||
| 2233 | $data{$key} = $val; | ||
| 2234 | next KEY; | ||
| 2235 | } | ||
| 2236 | } | ||
| 2237 | else { | ||
| 2238 | $data{$key} = delete $params->{$source} | ||
| 2239 | or die "Missing required param '$source'\n"; | ||
| 2240 | } | ||
| 2241 | } | ||
| 2242 | return \%data; | ||
| 2243 | } | ||
| 2244 | |||
| 2245 | #=================================== | ||
| 2246 | sub _build_cmd { | ||
| 2247 | #=================================== | ||
| 2248 | my $self = shift; | ||
| 2249 | my $params = shift; | ||
| 2250 | my ( $prefix, $defn, $postfix ) = @_; | ||
| 2251 | |||
| 2252 | my @defn = ( @{ $defn || [] } ); | ||
| 2253 | my @cmd; | ||
| 2254 | while (@defn) { | ||
| 2255 | my $key = shift @defn; | ||
| 2256 | my $type = shift @defn; | ||
| 2257 | |||
| 2258 | my $val | ||
| 2259 | = exists $params->{$key} | ||
| 2260 | ? delete $params->{$key} | ||
| 2261 | : $self->{_default}{$key}; | ||
| 2262 | |||
| 2263 | $val = '' unless defined $val; | ||
| 2264 | |||
| 2265 | if ( ref $val eq 'ARRAY' ) { | ||
| 2266 | die "'$key' must be a single value\n" | ||
| 2267 | if $type <= ONE_ALL; | ||
| 2268 | $val = join ',', @$val; | ||
| 2269 | } | ||
| 2270 | unless ( length $val ) { | ||
| 2271 | next if $type == ONE_OPT || $type == MULTI_BLANK; | ||
| 2272 | die "Param '$key' is required\n" | ||
| 2273 | if $type == ONE_REQ || $type == MULTI_REQ; | ||
| 2274 | $val = '_all'; | ||
| 2275 | } | ||
| 2276 | push @cmd, uri_escape($val); | ||
| 2277 | } | ||
| 2278 | |||
| 2279 | return join '/', '', grep {defined} ( $prefix, @cmd, $postfix ); | ||
| 2280 | } | ||
| 2281 | |||
| 2282 | 1; | ||
| 2283 | |||
| 2284 | =pod | ||
| 2285 | |||
| 2286 | =encoding UTF-8 | ||
| 2287 | |||
| 2288 | =head1 NAME | ||
| 2289 | |||
| 2290 | Search::Elasticsearch::Client::Compat - The client compatibility layer for migrating from ElasticSearch.pm | ||
| 2291 | |||
| 2292 | =head1 VERSION | ||
| 2293 | |||
| 2294 | version 0.10 | ||
| 2295 | |||
| 2296 | =head1 SYNOPSIS | ||
| 2297 | |||
| 2298 | use ElasticSearch::Compat; | ||
| 2299 | my $es = ElasticSearch::Compat->new( | ||
| 2300 | servers => 'search.foo.com:9200', # default '127.0.0.1:9200' | ||
| 2301 | transport => 'http' # default 'http' | ||
| 2302 | | 'httptiny', | ||
| 2303 | trace_calls => 'log_file', | ||
| 2304 | no_refresh => 0 | 1, | ||
| 2305 | ); | ||
| 2306 | |||
| 2307 | $es->index( | ||
| 2308 | index => 'twitter', | ||
| 2309 | type => 'tweet', | ||
| 2310 | id => 1, | ||
| 2311 | data => { | ||
| 2312 | user => 'kimchy', | ||
| 2313 | post_date => '2009-11-15T14:12:12', | ||
| 2314 | message => 'trying out Elastic Search' | ||
| 2315 | } | ||
| 2316 | ); | ||
| 2317 | |||
| 2318 | $data = $es->get( | ||
| 2319 | index => 'twitter', | ||
| 2320 | type => 'tweet', | ||
| 2321 | id => 1 | ||
| 2322 | ); | ||
| 2323 | |||
| 2324 | # native elasticsearch query language | ||
| 2325 | $results = $es->search( | ||
| 2326 | index => 'twitter', | ||
| 2327 | type => 'tweet', | ||
| 2328 | query => { | ||
| 2329 | text => { user => 'kimchy' } | ||
| 2330 | } | ||
| 2331 | ); | ||
| 2332 | |||
| 2333 | # ElasticSearch::SearchBuilder Perlish query language | ||
| 2334 | $results = $es->search( | ||
| 2335 | index => 'twitter', | ||
| 2336 | type => 'tweet', | ||
| 2337 | queryb => { | ||
| 2338 | message => 'Perl API', | ||
| 2339 | user => 'kimchy', | ||
| 2340 | post_date => { | ||
| 2341 | '>' => '2010-01-01', | ||
| 2342 | '<=' => '2011-01-01', | ||
| 2343 | } | ||
| 2344 | } | ||
| 2345 | ); | ||
| 2346 | |||
| 2347 | |||
| 2348 | $dodgy_qs = "foo AND AND bar"; | ||
| 2349 | $results = $es->search( | ||
| 2350 | index => 'twitter', | ||
| 2351 | type => 'tweet', | ||
| 2352 | query => { | ||
| 2353 | query_string => { | ||
| 2354 | query => $es->query_parser->filter($dodgy_qs) | ||
| 2355 | }, | ||
| 2356 | } | ||
| 2357 | ); | ||
| 2358 | |||
| 2359 | =head1 DESCRIPTION | ||
| 2360 | |||
| 2361 | See L<Search::Elasticsearch::Compat> for an explanation of why this module exists. | ||
| 2362 | |||
| 2363 | =head1 CALLING CONVENTIONS | ||
| 2364 | |||
| 2365 | I've tried to follow the same terminology as used in the Elasticsearch docs | ||
| 2366 | when naming methods, so it should be easy to tie the two together. | ||
| 2367 | |||
| 2368 | Some methods require a specific C<index> and a specific C<type>, while others | ||
| 2369 | allow a list of indices or types, or allow you to specify all indices or | ||
| 2370 | types. I distinguish between them as follows: | ||
| 2371 | |||
| 2372 | $es->method( index => multi, type => single, ...) | ||
| 2373 | |||
| 2374 | C<single> values must be a scalar, and are required parameters | ||
| 2375 | |||
| 2376 | type => 'tweet' | ||
| 2377 | |||
| 2378 | C<multi> values can be: | ||
| 2379 | |||
| 2380 | index => 'twitter' # specific index | ||
| 2381 | index => ['twitter','user'] # list of indices | ||
| 2382 | index => undef # (or not specified) = all indices | ||
| 2383 | |||
| 2384 | C<multi_req> values work like C<multi> values, but at least one value is | ||
| 2385 | required, so: | ||
| 2386 | |||
| 2387 | index => 'twitter' # specific index | ||
| 2388 | index => ['twitter','user'] # list of indices | ||
| 2389 | index => '_all' # all indices | ||
| 2390 | |||
| 2391 | index => [] # error | ||
| 2392 | index => undef # error | ||
| 2393 | |||
| 2394 | Also, see L</"use_index()/use_type()">. | ||
| 2395 | |||
| 2396 | =head2 as_json | ||
| 2397 | |||
| 2398 | If you pass C<< as_json => 1 >> to any request to the Elasticsearch server, | ||
| 2399 | it will return the raw UTF8-decoded JSON response, rather than a Perl | ||
| 2400 | datastructure. | ||
| 2401 | |||
| 2402 | =head1 RETURN VALUES AND EXCEPTIONS | ||
| 2403 | |||
| 2404 | Methods that query the Elasticsearch cluster return the raw data structure | ||
| 2405 | that the cluster returns. This may change in the future, but as these | ||
| 2406 | data structures are still in flux, I thought it safer not to try to interpret. | ||
| 2407 | |||
| 2408 | Anything that is known to be an error throws an exception, eg trying to delete | ||
| 2409 | a non-existent index. | ||
| 2410 | |||
| 2411 | =head1 INTEGRATION WITH ElasticSearch::SearchBuilder | ||
| 2412 | |||
| 2413 | L<ElasticSearch::SearchBuilder> provides a concise Perlish | ||
| 2414 | L<SQL::Abstract>-style query language, which gets translated into the native | ||
| 2415 | L<Query DSL|http://www.elasticsearch.org/guide/reference/query-dsl> that | ||
| 2416 | Elasticsearch uses. | ||
| 2417 | |||
| 2418 | For instance: | ||
| 2419 | |||
| 2420 | { | ||
| 2421 | content => 'search keywords', | ||
| 2422 | -filter => { | ||
| 2423 | tags => ['perl','ruby'], | ||
| 2424 | date => { | ||
| 2425 | '>' => '2010-01-01', | ||
| 2426 | '<=' => '2011-01-01' | ||
| 2427 | }, | ||
| 2428 | } | ||
| 2429 | } | ||
| 2430 | |||
| 2431 | Would be translated to: | ||
| 2432 | |||
| 2433 | { query => { | ||
| 2434 | filtered => { | ||
| 2435 | query => { text => { content => "search keywords" } }, | ||
| 2436 | filter => { | ||
| 2437 | and => [ | ||
| 2438 | { terms => { tags => ["perl", "ruby"] } }, | ||
| 2439 | { numeric_range => { | ||
| 2440 | date => { | ||
| 2441 | gt => "2010-01-01", | ||
| 2442 | lte => "2011-01-01" | ||
| 2443 | }}}, | ||
| 2444 | ], | ||
| 2445 | } | ||
| 2446 | }}} | ||
| 2447 | |||
| 2448 | All you have to do to start using L<ElasticSearch::SearchBuilder> is to change | ||
| 2449 | your C<query> or C<filter> parameter to C<queryb> or C<filterb> (where the | ||
| 2450 | extra C<b> stands for C<builder>): | ||
| 2451 | |||
| 2452 | $es->search( | ||
| 2453 | queryb => { content => 'keywords' } | ||
| 2454 | ) | ||
| 2455 | |||
| 2456 | If you want to see what your SearchBuilder-style query is being converted into, | ||
| 2457 | you can either use L</"trace_calls()"> or access it directly with: | ||
| 2458 | |||
| 2459 | $native_query = $es->builder->query( $query ) | ||
| 2460 | $native_filter = $es->builder->filter( $filter ) | ||
| 2461 | |||
| 2462 | See the L<ElasticSearch::SearchBuilder> docs for more information about | ||
| 2463 | the syntax. | ||
| 2464 | |||
| 2465 | =head1 METHODS | ||
| 2466 | |||
| 2467 | =head2 Document-indexing methods | ||
| 2468 | |||
| 2469 | =head3 index() | ||
| 2470 | |||
| 2471 | $result = $es->index( | ||
| 2472 | index => single, | ||
| 2473 | type => single, | ||
| 2474 | id => $document_id, # optional, otherwise auto-generated | ||
| 2475 | data => { | ||
| 2476 | key => value, | ||
| 2477 | ... | ||
| 2478 | }, | ||
| 2479 | |||
| 2480 | # optional | ||
| 2481 | consistency => 'quorum' | 'one' | 'all', | ||
| 2482 | create => 0 | 1, | ||
| 2483 | parent => $parent, | ||
| 2484 | percolate => $percolate, | ||
| 2485 | refresh => 0 | 1, | ||
| 2486 | replication => 'sync' | 'async', | ||
| 2487 | routing => $routing, | ||
| 2488 | timeout => eg '1m' or '10s' | ||
| 2489 | version => int, | ||
| 2490 | version_type => 'internal' | 'external', | ||
| 2491 | ); | ||
| 2492 | |||
| 2493 | eg: | ||
| 2494 | |||
| 2495 | $result = $es->index( | ||
| 2496 | index => 'twitter', | ||
| 2497 | type => 'tweet', | ||
| 2498 | id => 1, | ||
| 2499 | data => { | ||
| 2500 | user => 'kimchy', | ||
| 2501 | post_date => '2009-11-15T14:12:12', | ||
| 2502 | message => 'trying out Elastic Search' | ||
| 2503 | }, | ||
| 2504 | ); | ||
| 2505 | |||
| 2506 | Used to add a document to a specific C<index> as a specific C<type> with | ||
| 2507 | a specific C<id>. If the C<index/type/id> combination already exists, | ||
| 2508 | then that document is updated, otherwise it is created. | ||
| 2509 | |||
| 2510 | Note: | ||
| 2511 | |||
| 2512 | =over | ||
| 2513 | |||
| 2514 | =item * | ||
| 2515 | |||
| 2516 | If the C<id> is not specified, then Elasticsearch autogenerates a unique | ||
| 2517 | ID and a new document is always created. | ||
| 2518 | |||
| 2519 | =item * | ||
| 2520 | |||
| 2521 | If C<version> is passed, and the current version in Elasticsearch is | ||
| 2522 | different, then a C<Conflict> error will be thrown. | ||
| 2523 | |||
| 2524 | =item * | ||
| 2525 | |||
| 2526 | C<data> can also be a raw JSON encoded string (but ensure that it is correctly | ||
| 2527 | encoded, otherwise you see errors when trying to retrieve it from Elasticsearch). | ||
| 2528 | |||
| 2529 | $es->index( | ||
| 2530 | index => 'foo', | ||
| 2531 | type => 'bar', | ||
| 2532 | id => 1, | ||
| 2533 | data => '{"foo":"bar"}' | ||
| 2534 | ); | ||
| 2535 | |||
| 2536 | =item * | ||
| 2537 | |||
| 2538 | C<timeout> for all CRUD methods and L</"search()"> is a query timeout, | ||
| 2539 | specifying the amount of time Elasticsearch will spend (roughly) processing a | ||
| 2540 | query. Units can be concatenated with the integer value, e.g., C<500ms> or | ||
| 2541 | C<1s>. | ||
| 2542 | |||
| 2543 | See also: L<http://www.elasticsearch.org/guide/reference/api/search/request-body.html> | ||
| 2544 | |||
| 2545 | Note: this is distinct from the transport timeout, see L</"timeout()">. | ||
| 2546 | |||
| 2547 | =back | ||
| 2548 | |||
| 2549 | See also: L<http://www.elasticsearch.org/guide/reference/api/index_.html>, | ||
| 2550 | L</"bulk()"> and L</"put_mapping()"> | ||
| 2551 | |||
| 2552 | =head3 set() | ||
| 2553 | |||
| 2554 | C<set()> is a synonym for L</"index()"> | ||
| 2555 | |||
| 2556 | =head3 create() | ||
| 2557 | |||
| 2558 | $result = $es->create( | ||
| 2559 | index => single, | ||
| 2560 | type => single, | ||
| 2561 | id => $document_id, # optional, otherwise auto-generated | ||
| 2562 | data => { | ||
| 2563 | key => value, | ||
| 2564 | ... | ||
| 2565 | }, | ||
| 2566 | |||
| 2567 | # optional | ||
| 2568 | consistency => 'quorum' | 'one' | 'all', | ||
| 2569 | parent => $parent, | ||
| 2570 | percolate => $percolate, | ||
| 2571 | refresh => 0 | 1, | ||
| 2572 | replication => 'sync' | 'async', | ||
| 2573 | routing => $routing, | ||
| 2574 | timeout => eg '1m' or '10s', | ||
| 2575 | version => int, | ||
| 2576 | version_type => 'internal' | 'external', | ||
| 2577 | ); | ||
| 2578 | |||
| 2579 | eg: | ||
| 2580 | |||
| 2581 | $result = $es->create( | ||
| 2582 | index => 'twitter', | ||
| 2583 | type => 'tweet', | ||
| 2584 | id => 1, | ||
| 2585 | data => { | ||
| 2586 | user => 'kimchy', | ||
| 2587 | post_date => '2009-11-15T14:12:12', | ||
| 2588 | message => 'trying out Elastic Search' | ||
| 2589 | }, | ||
| 2590 | ); | ||
| 2591 | |||
| 2592 | Used to add a NEW document to a specific C<index> as a specific C<type> with | ||
| 2593 | a specific C<id>. If the C<index/type/id> combination already exists, | ||
| 2594 | then a C<Conflict> error is thrown. | ||
| 2595 | |||
| 2596 | If the C<id> is not specified, then Elasticsearch autogenerates a unique | ||
| 2597 | ID. | ||
| 2598 | |||
| 2599 | If you pass a C<version> parameter to C<create>, then it must be C<0> unless | ||
| 2600 | you also set C<version_type> to C<external>. | ||
| 2601 | |||
| 2602 | See also: L</"index()"> | ||
| 2603 | |||
| 2604 | =head3 update() | ||
| 2605 | |||
| 2606 | $result = $es->update( | ||
| 2607 | index => single, | ||
| 2608 | type => single, | ||
| 2609 | id => single, | ||
| 2610 | |||
| 2611 | # required | ||
| 2612 | script => $script, | ||
| 2613 | | doc => $doc | ||
| 2614 | |||
| 2615 | # optional | ||
| 2616 | params => { params }, | ||
| 2617 | upsert => { new_doc }, | ||
| 2618 | consistency => 'quorum' | 'one' | 'all', | ||
| 2619 | fields => ['_source'], | ||
| 2620 | ignore_missing => 0 | 1, | ||
| 2621 | parent => $parent, | ||
| 2622 | percolate => $percolate, | ||
| 2623 | retry_on_conflict => 2, | ||
| 2624 | routing => $routing, | ||
| 2625 | timeout => '10s', | ||
| 2626 | replication => 'sync' | 'async' | ||
| 2627 | ) | ||
| 2628 | |||
| 2629 | The C<update()> method accepts a C<script> to update, or a C<doc> to be merged | ||
| 2630 | with, an existing doc, without having to retrieve and reindex the doc yourself, | ||
| 2631 | eg: | ||
| 2632 | |||
| 2633 | $es->update( | ||
| 2634 | index => 'test', | ||
| 2635 | type => 'foo', | ||
| 2636 | id => 123, | ||
| 2637 | script => 'ctx._source.tags+=[tag]', | ||
| 2638 | params => { tag => 'red' } | ||
| 2639 | ); | ||
| 2640 | |||
| 2641 | You can also pass a new doc which will be inserted if the doc does not | ||
| 2642 | already exist, via the C<upsert> paramater. | ||
| 2643 | |||
| 2644 | See L<http://www.elasticsearch.org/guide/reference/api/update.html> for more. | ||
| 2645 | |||
| 2646 | =head3 get() | ||
| 2647 | |||
| 2648 | $result = $es->get( | ||
| 2649 | index => single, | ||
| 2650 | type => single or blank, | ||
| 2651 | id => single, | ||
| 2652 | |||
| 2653 | # optional | ||
| 2654 | fields => 'field' or ['field1',...] | ||
| 2655 | preference => '_local' | '_primary' | '_primary_first' | $string, | ||
| 2656 | refresh => 0 | 1, | ||
| 2657 | routing => $routing, | ||
| 2658 | parent => $parent, | ||
| 2659 | ignore_missing => 0 | 1, | ||
| 2660 | |||
| 2661 | ); | ||
| 2662 | |||
| 2663 | Returns the document stored at C<index/type/id> or throws an exception if | ||
| 2664 | the document doesn't exist. | ||
| 2665 | |||
| 2666 | Example: | ||
| 2667 | |||
| 2668 | $es->get( index => 'twitter', type => 'tweet', id => 1) | ||
| 2669 | |||
| 2670 | Returns: | ||
| 2671 | |||
| 2672 | { | ||
| 2673 | _id => 1, | ||
| 2674 | _index => "twitter", | ||
| 2675 | _source => { | ||
| 2676 | message => "trying out Elastic Search", | ||
| 2677 | post_date=> "2009-11-15T14:12:12", | ||
| 2678 | user => "kimchy", | ||
| 2679 | }, | ||
| 2680 | _type => "tweet", | ||
| 2681 | } | ||
| 2682 | |||
| 2683 | By default the C<_source> field is returned. Use C<fields> to specify | ||
| 2684 | a list of (stored) fields to return instead, or C<[]> to return no fields. | ||
| 2685 | |||
| 2686 | Pass a true value for C<refresh> to force an index refresh before performing | ||
| 2687 | the get. | ||
| 2688 | |||
| 2689 | If the requested C<index>, C<type> or C<id> is not found, then a C<Missing> | ||
| 2690 | exception is thrown, unless C<ignore_missing> is true. | ||
| 2691 | |||
| 2692 | See also: L</"bulk()">, L<http://www.elasticsearch.org/guide/reference/api/get.html> | ||
| 2693 | |||
| 2694 | =head3 exists() | ||
| 2695 | |||
| 2696 | $bool = $es->exists( | ||
| 2697 | index => single, | ||
| 2698 | type => single, | ||
| 2699 | id => single, | ||
| 2700 | |||
| 2701 | preference => '_local' | '_primary' | '_primary_first' | $string, | ||
| 2702 | refresh => 0 | 1, | ||
| 2703 | routing => $routing, | ||
| 2704 | parent => $parent, | ||
| 2705 | ); | ||
| 2706 | |||
| 2707 | Returns true or false depending on whether the doc exists. | ||
| 2708 | |||
| 2709 | =head3 mget() | ||
| 2710 | |||
| 2711 | $docs = $es->mget( | ||
| 2712 | index => single, | ||
| 2713 | type => single or blank, | ||
| 2714 | ids => \@ids, | ||
| 2715 | fields => ['field_1','field_2'], | ||
| 2716 | filter_missing => 0 | 1 | ||
| 2717 | ); | ||
| 2718 | |||
| 2719 | $docs = $es->mget( | ||
| 2720 | index => single or blank, | ||
| 2721 | type => single or blank, | ||
| 2722 | docs => \@doc_info, | ||
| 2723 | fields => ['field_1','field_2'], | ||
| 2724 | filter_missing => 0 | 1 | ||
| 2725 | ); | ||
| 2726 | |||
| 2727 | C<mget> or "multi-get" returns multiple documents at once. There are two | ||
| 2728 | ways to call C<mget()>: | ||
| 2729 | |||
| 2730 | If all docs come from the same index (and potentially the same type): | ||
| 2731 | |||
| 2732 | $docs = $es->mget( | ||
| 2733 | index => 'myindex', | ||
| 2734 | type => 'mytype', # optional | ||
| 2735 | ids => [1,2,3], | ||
| 2736 | ) | ||
| 2737 | |||
| 2738 | Alternatively you can specify each doc separately: | ||
| 2739 | |||
| 2740 | $docs = $es->mget( | ||
| 2741 | docs => [ | ||
| 2742 | { _index => 'index_1', _type => 'type_1', _id => 1 }, | ||
| 2743 | { _index => 'index_2', _type => 'type_2', _id => 2 }, | ||
| 2744 | ] | ||
| 2745 | ) | ||
| 2746 | |||
| 2747 | Or: | ||
| 2748 | |||
| 2749 | $docs = $es->mget( | ||
| 2750 | index => 'myindex', # default index | ||
| 2751 | type => 'mytype', # default type | ||
| 2752 | fields => ['field_1','field_2'], # default fields | ||
| 2753 | docs => [ | ||
| 2754 | { _id => 1 }, # uses defaults | ||
| 2755 | { _index => 'index_2', | ||
| 2756 | _type => 'type_2', | ||
| 2757 | _id => 2, | ||
| 2758 | fields => ['field_2','field_3'], | ||
| 2759 | }, | ||
| 2760 | ] | ||
| 2761 | ); | ||
| 2762 | |||
| 2763 | If C<$docs> or C<$ids> is an empty array ref, then C<mget()> will just return | ||
| 2764 | an empty array ref. | ||
| 2765 | |||
| 2766 | Returns an array ref containing all of the documents requested. If a document | ||
| 2767 | is not found, then its entry will include C<< {exists => 0} >>. If you would | ||
| 2768 | rather filter these missing docs, pass C<< filter_missing => 1 >>. | ||
| 2769 | |||
| 2770 | See L<http://www.elasticsearch.org/guide/reference/api/multi-get.html> | ||
| 2771 | |||
| 2772 | =head3 delete() | ||
| 2773 | |||
| 2774 | $result = $es->delete( | ||
| 2775 | index => single, | ||
| 2776 | type => single, | ||
| 2777 | id => single, | ||
| 2778 | |||
| 2779 | # optional | ||
| 2780 | consistency => 'quorum' | 'one' | 'all' | ||
| 2781 | ignore_missing => 0 | 1 | ||
| 2782 | refresh => 0 | 1 | ||
| 2783 | parent => $parent, | ||
| 2784 | routing => $routing, | ||
| 2785 | replication => 'sync' | 'async' | ||
| 2786 | version => int | ||
| 2787 | ); | ||
| 2788 | |||
| 2789 | Deletes the document stored at C<index/type/id> or throws an C<Missing> | ||
| 2790 | exception if the document doesn't exist and C<ignore_missing> is not true. | ||
| 2791 | |||
| 2792 | If you specify a C<version> and the current version of the document is | ||
| 2793 | different (or if the document is not found), a C<Conflict> error will | ||
| 2794 | be thrown. | ||
| 2795 | |||
| 2796 | If C<refresh> is true, an index refresh will be forced after the delete has | ||
| 2797 | completed. | ||
| 2798 | |||
| 2799 | Example: | ||
| 2800 | |||
| 2801 | $es->delete( index => 'twitter', type => 'tweet', id => 1); | ||
| 2802 | |||
| 2803 | See also: L</"bulk()">, | ||
| 2804 | L<http://www.elasticsearch.org/guide/reference/api/delete.html> | ||
| 2805 | |||
| 2806 | =head3 bulk() | ||
| 2807 | |||
| 2808 | $result = $es->bulk( [ actions ] ) | ||
| 2809 | |||
| 2810 | $result = $es->bulk( | ||
| 2811 | actions => [ actions ] # required | ||
| 2812 | |||
| 2813 | index => 'foo', # optional | ||
| 2814 | type => 'bar', # optional | ||
| 2815 | consistency => 'quorum' | 'one' | 'all' # optional | ||
| 2816 | refresh => 0 | 1, # optional | ||
| 2817 | replication => 'sync' | 'async', # optional | ||
| 2818 | |||
| 2819 | on_conflict => sub {...} | 'IGNORE' # optional | ||
| 2820 | on_error => sub {...} | 'IGNORE' # optional | ||
| 2821 | ); | ||
| 2822 | |||
| 2823 | Perform multiple C<index>, C<create> and C<delete> actions in a single request. | ||
| 2824 | This is about 10x as fast as performing each action in a separate request. | ||
| 2825 | |||
| 2826 | Each C<action> is a HASH ref with a key indicating the action type (C<index>, | ||
| 2827 | C<create> or C<delete>), whose value is another HASH ref containing the | ||
| 2828 | associated metadata. | ||
| 2829 | |||
| 2830 | The C<index> and C<type> parameters can be specified for each individual action, | ||
| 2831 | or inherited from the top level C<index> and C<type> parameters, as shown | ||
| 2832 | above. | ||
| 2833 | |||
| 2834 | NOTE: C<bulk()> also accepts the C<_index>, C<_type>, C<_id>, C<_source>, | ||
| 2835 | C<_parent>, C<_routing> and C<_version> parameters so that you can pass search | ||
| 2836 | results directly to C<bulk()>. | ||
| 2837 | |||
| 2838 | =head4 C<index> and C<create> actions | ||
| 2839 | |||
| 2840 | { index => { | ||
| 2841 | index => 'foo', | ||
| 2842 | type => 'bar', | ||
| 2843 | id => 123, | ||
| 2844 | data => { text => 'foo bar'}, | ||
| 2845 | |||
| 2846 | # optional | ||
| 2847 | routing => $routing, | ||
| 2848 | parent => $parent, | ||
| 2849 | percolate => $percolate, | ||
| 2850 | timestamp => $timestamp, | ||
| 2851 | ttl => $ttl, | ||
| 2852 | version => $version, | ||
| 2853 | version_type => 'internal' | 'external' | ||
| 2854 | }} | ||
| 2855 | |||
| 2856 | { create => { ... same options as for 'index' }} | ||
| 2857 | |||
| 2858 | The C<index> and C<type> parameters, if not specified, are inherited from | ||
| 2859 | the top level bulk request. | ||
| 2860 | |||
| 2861 | C<data> can also be a raw JSON encoded string (but ensure that it is correctly | ||
| 2862 | encoded, otherwise you see errors when trying to retrieve it from Elasticsearch). | ||
| 2863 | |||
| 2864 | actions => [{ | ||
| 2865 | index => { | ||
| 2866 | index => 'foo', | ||
| 2867 | type => 'bar', | ||
| 2868 | id => 1, | ||
| 2869 | data => '{"foo":"bar"}' | ||
| 2870 | } | ||
| 2871 | }] | ||
| 2872 | |||
| 2873 | =head4 C<delete> action | ||
| 2874 | |||
| 2875 | { delete => { | ||
| 2876 | index => 'foo', | ||
| 2877 | type => 'bar', | ||
| 2878 | id => 123, | ||
| 2879 | |||
| 2880 | # optional | ||
| 2881 | routing => $routing, | ||
| 2882 | parent => $parent, | ||
| 2883 | version => $version, | ||
| 2884 | version_type => 'internal' | 'external' | ||
| 2885 | }} | ||
| 2886 | |||
| 2887 | The C<index> and C<type> parameters, if not specified, are inherited from | ||
| 2888 | the top level bulk request. | ||
| 2889 | |||
| 2890 | =head4 Error handlers | ||
| 2891 | |||
| 2892 | The C<on_conflict> and C<on_error> parameters accept either a coderef or the | ||
| 2893 | string C<'IGNORE'>. Normally, any errors are returned under the C<errors> | ||
| 2894 | key (see L</Return values>). | ||
| 2895 | |||
| 2896 | The logic works as follows: | ||
| 2897 | |||
| 2898 | =over | ||
| 2899 | |||
| 2900 | =item * | ||
| 2901 | |||
| 2902 | If the error is a versioning conflict error, or if you try to C<create> a doc | ||
| 2903 | whose ID already exists, and there is an C<on_conflict> | ||
| 2904 | handler, then call the handler and move on to the next document | ||
| 2905 | |||
| 2906 | =item * | ||
| 2907 | |||
| 2908 | If the error is still unhandled, and we have an C<on_error> handler, then call | ||
| 2909 | it and move on to the next document. | ||
| 2910 | |||
| 2911 | =item * | ||
| 2912 | |||
| 2913 | If no handler exists, then add the error to the C<@errors> array which is | ||
| 2914 | returned by L</bulk()> | ||
| 2915 | |||
| 2916 | =back | ||
| 2917 | |||
| 2918 | Setting C<on_conflict> or C<on_error> to C<'IGNORE'> is the equivalent | ||
| 2919 | of passing an empty no-op handler. | ||
| 2920 | |||
| 2921 | The handler callbacks are called as: | ||
| 2922 | |||
| 2923 | $handler->( $action, $document, $error, $req_no ); | ||
| 2924 | |||
| 2925 | For instance: | ||
| 2926 | |||
| 2927 | =over | ||
| 2928 | |||
| 2929 | =item C<$action> | ||
| 2930 | |||
| 2931 | "index" | ||
| 2932 | |||
| 2933 | =item C<$document> | ||
| 2934 | |||
| 2935 | { id => 1, data => { count => "foo" }} | ||
| 2936 | |||
| 2937 | =item C<$error> | ||
| 2938 | |||
| 2939 | "MapperParsingException[Failed to parse [count]]; ... etc ... " | ||
| 2940 | |||
| 2941 | =item C<$req_no> | ||
| 2942 | |||
| 2943 | 0 | ||
| 2944 | |||
| 2945 | =back | ||
| 2946 | |||
| 2947 | The C<$req_no> is the array index of the current C<$action> from the original | ||
| 2948 | array of C<@actions>. | ||
| 2949 | |||
| 2950 | =head4 Return values | ||
| 2951 | |||
| 2952 | The L</"bulk()"> method returns a HASH ref containing: | ||
| 2953 | |||
| 2954 | { | ||
| 2955 | actions => [ the list of actions you passed in ], | ||
| 2956 | results => [ the result of each of the actions ], | ||
| 2957 | errors => [ a list of any errors ] | ||
| 2958 | } | ||
| 2959 | |||
| 2960 | The C<results> ARRAY ref contains the same values that would be returned | ||
| 2961 | for individiual C<index>/C<create>/C<delete> statements, eg: | ||
| 2962 | |||
| 2963 | results => [ | ||
| 2964 | { create => { _id => 123, _index => "foo", _type => "bar", _version => 1 } }, | ||
| 2965 | { index => { _id => 123, _index => "foo", _type => "bar", _version => 2 } }, | ||
| 2966 | { delete => { _id => 123, _index => "foo", _type => "bar", _version => 3 } }, | ||
| 2967 | ] | ||
| 2968 | |||
| 2969 | The C<errors> key is only present if an error has occured and has not been handled | ||
| 2970 | by an C<on_conflict> or C<on_error> handler, so you can do: | ||
| 2971 | |||
| 2972 | $results = $es->bulk(\@actions); | ||
| 2973 | if ($results->{errors}) { | ||
| 2974 | # handle errors | ||
| 2975 | } | ||
| 2976 | |||
| 2977 | Each error element contains the C<error> message plus the C<action> that | ||
| 2978 | triggered the error. Each C<result> element will also contain the error | ||
| 2979 | message., eg: | ||
| 2980 | |||
| 2981 | $result = { | ||
| 2982 | actions => [ | ||
| 2983 | |||
| 2984 | ## NOTE - num is numeric | ||
| 2985 | { index => { index => 'bar', type => 'bar', id => 123, | ||
| 2986 | data => { num => 123 } } }, | ||
| 2987 | |||
| 2988 | ## NOTE - num is a string | ||
| 2989 | { index => { index => 'bar', type => 'bar', id => 123, | ||
| 2990 | data => { num => 'foo bar' } } }, | ||
| 2991 | ], | ||
| 2992 | errors => [ | ||
| 2993 | { | ||
| 2994 | action => { | ||
| 2995 | index => { index => 'bar', type => 'bar', id => 123, | ||
| 2996 | data => { num => 'text foo' } } | ||
| 2997 | }, | ||
| 2998 | error => "MapperParsingException[Failed to parse [num]]; ...", | ||
| 2999 | }, | ||
| 3000 | ], | ||
| 3001 | results => [ | ||
| 3002 | { index => { _id => 123, _index => "bar", _type => "bar", _version => 1 }}, | ||
| 3003 | { index => { | ||
| 3004 | error => "MapperParsingException[Failed to parse [num]];...", | ||
| 3005 | id => 123, index => "bar", type => "bar", | ||
| 3006 | }, | ||
| 3007 | }, | ||
| 3008 | ], | ||
| 3009 | |||
| 3010 | }; | ||
| 3011 | |||
| 3012 | See L<http://www.elasticsearch.org/guide/reference/api/bulk.html> for | ||
| 3013 | more details. | ||
| 3014 | |||
| 3015 | =head3 bulk_index(), bulk_create(), bulk_delete() | ||
| 3016 | |||
| 3017 | These are convenience methods which allow you to pass just the metadata, without | ||
| 3018 | the C<index>, C<create> or C<index> action for each record. | ||
| 3019 | |||
| 3020 | These methods accept the same parameters as the L</"bulk()"> method, except | ||
| 3021 | that the C<actions> parameter is replaced by C<docs>, eg: | ||
| 3022 | |||
| 3023 | $result = $es->bulk_index( [ docs ] ); | ||
| 3024 | |||
| 3025 | $result = $es->bulk_index( | ||
| 3026 | docs => [ docs ], # required | ||
| 3027 | |||
| 3028 | index => 'foo', # optional | ||
| 3029 | type => 'bar', # optional | ||
| 3030 | consistency => 'quorum' | 'one' | 'all' # optional | ||
| 3031 | refresh => 0 | 1, # optional | ||
| 3032 | replication => 'sync' | 'async', # optional | ||
| 3033 | |||
| 3034 | on_conflict => sub {...} | 'IGNORE' # optional | ||
| 3035 | on_error => sub {...} | 'IGNORE' # optional | ||
| 3036 | ); | ||
| 3037 | |||
| 3038 | For instance: | ||
| 3039 | |||
| 3040 | $es->bulk_index( | ||
| 3041 | index => 'foo', | ||
| 3042 | type => 'bar', | ||
| 3043 | refresh => 1, | ||
| 3044 | docs => [ | ||
| 3045 | { id => 123, data => { text=>'foo'} }, | ||
| 3046 | { id => 124, type => 'baz', data => { text=>'bar'} }, | ||
| 3047 | ] | ||
| 3048 | ); | ||
| 3049 | |||
| 3050 | =head3 reindex() | ||
| 3051 | |||
| 3052 | $es->reindex( | ||
| 3053 | source => $scrolled_search, | ||
| 3054 | |||
| 3055 | # optional | ||
| 3056 | bulk_size => 1000, | ||
| 3057 | dest_index => $index, | ||
| 3058 | quiet => 0 | 1, | ||
| 3059 | transform => sub {....}, | ||
| 3060 | |||
| 3061 | on_conflict => sub {...} | 'IGNORE' | ||
| 3062 | on_error => sub {...} | 'IGNORE' | ||
| 3063 | ) | ||
| 3064 | |||
| 3065 | C<reindex()> is a utility method which can be used for reindexing data | ||
| 3066 | from one index to another (eg if the mapping has changed), or copying | ||
| 3067 | data from one cluster to another. | ||
| 3068 | |||
| 3069 | =head4 Params | ||
| 3070 | |||
| 3071 | =over | ||
| 3072 | |||
| 3073 | =item * | ||
| 3074 | |||
| 3075 | C<source> is a required parameter, and should be an instance of | ||
| 3076 | L<Search::Elasticsearch::Compat::ScrolledSearch>. | ||
| 3077 | |||
| 3078 | =item * | ||
| 3079 | |||
| 3080 | C<dest_index> is the name of the destination index, ie where the docs are | ||
| 3081 | indexed to. If you are indexing your data from one cluster to another, | ||
| 3082 | and you want to use the same index name in your destination cluster, then | ||
| 3083 | you can leave this blank. | ||
| 3084 | |||
| 3085 | =item * | ||
| 3086 | |||
| 3087 | C<bulk_size> - the number of docs that will be indexed at a time. Defaults | ||
| 3088 | to 1,000 | ||
| 3089 | |||
| 3090 | =item * | ||
| 3091 | |||
| 3092 | Set C<quiet> to C<1> if you don't want any progress information to be | ||
| 3093 | printed to C<STDOUT> | ||
| 3094 | |||
| 3095 | =item * | ||
| 3096 | |||
| 3097 | C<transform> should be a sub-ref which will be called for each doc, allowing | ||
| 3098 | you to transform some element of the doc, or to skip the doc by returning | ||
| 3099 | C<undef>. | ||
| 3100 | |||
| 3101 | =item * | ||
| 3102 | |||
| 3103 | See L</Error handlers> for an explanation C<on_conflict> and C<on_error>. | ||
| 3104 | |||
| 3105 | =back | ||
| 3106 | |||
| 3107 | =head4 Examples: | ||
| 3108 | |||
| 3109 | To copy the Elasticsearch website index locally, you could do: | ||
| 3110 | |||
| 3111 | my $local = Search::Elasticsearch::Compat->new( | ||
| 3112 | servers => 'localhost:9200' | ||
| 3113 | ); | ||
| 3114 | my $remote = Search::Elasticsearch::Compat->new( | ||
| 3115 | servers => 'search.elasticsearch.org:80', | ||
| 3116 | no_refresh => 1 | ||
| 3117 | ); | ||
| 3118 | |||
| 3119 | my $source = $remote->scrolled_search( | ||
| 3120 | search_type => 'scan', | ||
| 3121 | scroll => '5m' | ||
| 3122 | ); | ||
| 3123 | $local->reindex(source=>$source); | ||
| 3124 | |||
| 3125 | To copy one local index to another, make the title upper case, | ||
| 3126 | exclude docs of type C<boring>, and to preserve the version numbers | ||
| 3127 | from the original index: | ||
| 3128 | |||
| 3129 | my $source = $es->scrolled_search( | ||
| 3130 | index => 'old_index', | ||
| 3131 | search_type => 'scan', | ||
| 3132 | scroll => '5m', | ||
| 3133 | version => 1 | ||
| 3134 | ); | ||
| 3135 | |||
| 3136 | $es->reindex( | ||
| 3137 | source => $source, | ||
| 3138 | dest_index => 'new_index', | ||
| 3139 | transform => sub { | ||
| 3140 | my $doc = shift; | ||
| 3141 | return if $doc->{_type} eq 'boring'; | ||
| 3142 | $doc->{_source}{title} = uc( $doc->{_source}{title} ); | ||
| 3143 | return $doc; | ||
| 3144 | } | ||
| 3145 | ); | ||
| 3146 | |||
| 3147 | B<NOTE:> If some of your docs have parent/child relationships, and you want | ||
| 3148 | to preserve this relationship, then you should add this to your | ||
| 3149 | scrolled search parameters: C<< fields => ['_source','_parent'] >>. | ||
| 3150 | |||
| 3151 | For example: | ||
| 3152 | |||
| 3153 | my $source = $es->scrolled_search( | ||
| 3154 | index => 'old_index', | ||
| 3155 | search_type => 'scan', | ||
| 3156 | fields => ['_source','_parent'], | ||
| 3157 | version => 1 | ||
| 3158 | ); | ||
| 3159 | |||
| 3160 | $es->reindex( | ||
| 3161 | source => $source, | ||
| 3162 | dest_index => 'new_index', | ||
| 3163 | ); | ||
| 3164 | |||
| 3165 | See also L</"scrolled_search()">, L<Search::Elasticsearch::Compat::ScrolledSearch>, | ||
| 3166 | and L</"search()">. | ||
| 3167 | |||
| 3168 | =head3 analyze() | ||
| 3169 | |||
| 3170 | $result = $es->analyze( | ||
| 3171 | text => $text_to_analyze, # required | ||
| 3172 | index => single, # optional | ||
| 3173 | |||
| 3174 | # either | ||
| 3175 | field => 'type.fieldname', # requires index | ||
| 3176 | |||
| 3177 | analyzer => $analyzer, | ||
| 3178 | |||
| 3179 | tokenizer => $tokenizer, | ||
| 3180 | filters => \@filters, | ||
| 3181 | |||
| 3182 | # other options | ||
| 3183 | format => 'detailed' | 'text', | ||
| 3184 | prefer_local => 1 | 0 | ||
| 3185 | ); | ||
| 3186 | |||
| 3187 | The C<analyze()> method allows you to see how Elasticsearch is analyzing | ||
| 3188 | the text that you pass in, eg: | ||
| 3189 | |||
| 3190 | $result = $es->analyze( text => 'The Man' ) | ||
| 3191 | |||
| 3192 | $result = $es->analyze( | ||
| 3193 | text => 'The Man', | ||
| 3194 | analyzer => 'simple' | ||
| 3195 | ); | ||
| 3196 | |||
| 3197 | $result = $es->analyze( | ||
| 3198 | text => 'The Man', | ||
| 3199 | tokenizer => 'keyword', | ||
| 3200 | filters => ['lowercase'], | ||
| 3201 | ); | ||
| 3202 | |||
| 3203 | $result = $es->analyze( | ||
| 3204 | text => 'The Man', | ||
| 3205 | index => 'my_index', | ||
| 3206 | analyzer => 'my_custom_analyzer' | ||
| 3207 | ); | ||
| 3208 | |||
| 3209 | $result = $es->analyze( | ||
| 3210 | text => 'The Man', | ||
| 3211 | index => 'my_index', | ||
| 3212 | field => 'my_type.my_field', | ||
| 3213 | ); | ||
| 3214 | |||
| 3215 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze.html> for | ||
| 3216 | more. | ||
| 3217 | |||
| 3218 | =head2 Query methods | ||
| 3219 | |||
| 3220 | =head3 search() | ||
| 3221 | |||
| 3222 | $result = $es->search( | ||
| 3223 | index => multi, | ||
| 3224 | type => multi, | ||
| 3225 | |||
| 3226 | # optional | ||
| 3227 | query => { native query }, | ||
| 3228 | queryb => { searchbuilder query }, | ||
| 3229 | |||
| 3230 | filter => { native filter }, | ||
| 3231 | filterb => { searchbuilder filter }, | ||
| 3232 | |||
| 3233 | explain => 1 | 0, | ||
| 3234 | facets => { facets }, | ||
| 3235 | fields => [$field_1,$field_n], | ||
| 3236 | partial_fields => { my_field => { include => 'foo.bar.*' }}, | ||
| 3237 | from => $start_from, | ||
| 3238 | highlight => { highlight }. | ||
| 3239 | ignore_indices => 'none' | 'missing', | ||
| 3240 | indices_boost => { index_1 => 1.5,... }, | ||
| 3241 | min_score => $score, | ||
| 3242 | preference => '_local' | '_primary' | '_primary_first' | $string, | ||
| 3243 | routing => [$routing, ...] | ||
| 3244 | script_fields => { script_fields } | ||
| 3245 | search_type => 'dfs_query_then_fetch' | ||
| 3246 | | 'dfs_query_and_fetch' | ||
| 3247 | | 'query_then_fetch' | ||
| 3248 | | 'query_and_fetch' | ||
| 3249 | | 'count' | ||
| 3250 | | 'scan' | ||
| 3251 | size => $no_of_results | ||
| 3252 | sort => ['_score',$field_1] | ||
| 3253 | scroll => '5m' | '30s', | ||
| 3254 | stats => ['group_1','group_2'], | ||
| 3255 | track_scores => 0 | 1, | ||
| 3256 | timeout => '10s' | ||
| 3257 | version => 0 | 1 | ||
| 3258 | ); | ||
| 3259 | |||
| 3260 | Searches for all documents matching the query, with a request-body search. | ||
| 3261 | Documents can be matched against multiple indices and multiple types, eg: | ||
| 3262 | |||
| 3263 | $result = $es->search( | ||
| 3264 | index => undef, # all | ||
| 3265 | type => ['user','tweet'], | ||
| 3266 | query => { term => {user => 'kimchy' }} | ||
| 3267 | ); | ||
| 3268 | |||
| 3269 | You can provide either the C<query> parameter, which uses the native | ||
| 3270 | Elasticsearch Query DSL, or the C<queryb> parameter, which uses the | ||
| 3271 | more concise L<ElasticSearch::SearchBuilder> query syntax. | ||
| 3272 | |||
| 3273 | Similarly, use C<filterb> instead of C<filter>. SearchBuilder can also be | ||
| 3274 | used in facets, for instance, instead of: | ||
| 3275 | |||
| 3276 | $es->search( | ||
| 3277 | facets => { | ||
| 3278 | wow_facet => { | ||
| 3279 | query => { text => { content => 'wow' }}, | ||
| 3280 | facet_filter => { term => {status => 'active' }}, | ||
| 3281 | } | ||
| 3282 | } | ||
| 3283 | ) | ||
| 3284 | |||
| 3285 | You can use: | ||
| 3286 | |||
| 3287 | $es->search( | ||
| 3288 | facets => { | ||
| 3289 | wow_facet => { | ||
| 3290 | queryb => { content => 'wow' }, # note the extra 'b' | ||
| 3291 | facet_filterb => { status => 'active' }, # note the extra 'b' | ||
| 3292 | } | ||
| 3293 | } | ||
| 3294 | ) | ||
| 3295 | |||
| 3296 | See L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> for more. | ||
| 3297 | |||
| 3298 | For all of the options that can be included in the native C<query> parameter, | ||
| 3299 | see L<http://www.elasticsearch.org/guide/reference/api/search>, | ||
| 3300 | L<http://www.elasticsearch.org/guide/reference/api/search/request-body.html> | ||
| 3301 | and L<http://www.elasticsearch.org/guide/reference/query-dsl> | ||
| 3302 | |||
| 3303 | =head3 searchqs() | ||
| 3304 | |||
| 3305 | $result = $es->searchqs( | ||
| 3306 | index => multi, | ||
| 3307 | type => multi, | ||
| 3308 | |||
| 3309 | # optional | ||
| 3310 | q => $query_string, | ||
| 3311 | analyze_wildcard => 0 | 1, | ||
| 3312 | analyzer => $analyzer, | ||
| 3313 | default_operator => 'OR | AND ', | ||
| 3314 | df => $default_field, | ||
| 3315 | explain => 1 | 0, | ||
| 3316 | fields => [$field_1,$field_n], | ||
| 3317 | from => $start_from, | ||
| 3318 | ignore_indices => 'none' | 'missing', | ||
| 3319 | lenient => 0 | 1, | ||
| 3320 | lowercase_expanded_terms => 0 | 1, | ||
| 3321 | preference => '_local' | '_primary' | '_primary_first' | $string, | ||
| 3322 | quote_analyzer => $analyzer, | ||
| 3323 | quote_field_suffix => '.unstemmed', | ||
| 3324 | routing => [$routing, ...] | ||
| 3325 | search_type => $search_type | ||
| 3326 | size => $no_of_results | ||
| 3327 | sort => ['_score:asc','last_modified:desc'], | ||
| 3328 | scroll => '5m' | '30s', | ||
| 3329 | stats => ['group_1','group_2'], | ||
| 3330 | timeout => '10s' | ||
| 3331 | version => 0 | 1 | ||
| 3332 | |||
| 3333 | Searches for all documents matching the C<q> query_string, with a URI request. | ||
| 3334 | Documents can be matched against multiple indices and multiple types, eg: | ||
| 3335 | |||
| 3336 | $result = $es->searchqs( | ||
| 3337 | index => undef, # all | ||
| 3338 | type => ['user','tweet'], | ||
| 3339 | q => 'john smith' | ||
| 3340 | ); | ||
| 3341 | |||
| 3342 | For all of the options that can be included in the C<query> parameter, see | ||
| 3343 | L<http://www.elasticsearch.org/guide/reference/api/search> and | ||
| 3344 | L<http://www.elasticsearch.org/guide/reference/api/search/uri-request.html>. | ||
| 3345 | |||
| 3346 | =head3 scroll() | ||
| 3347 | |||
| 3348 | $result = $es->scroll( | ||
| 3349 | scroll_id => $scroll_id, | ||
| 3350 | scroll => '5m' | '30s', | ||
| 3351 | ); | ||
| 3352 | |||
| 3353 | If a search has been executed with a C<scroll> parameter, then the returned | ||
| 3354 | C<scroll_id> can be used like a cursor to scroll through the rest of the | ||
| 3355 | results. | ||
| 3356 | |||
| 3357 | If a further scroll request will be issued, then the C<scroll> parameter | ||
| 3358 | should be passed as well. For instance; | ||
| 3359 | |||
| 3360 | my $result = $es->search( | ||
| 3361 | query=>{match_all=>{}}, | ||
| 3362 | scroll => '5m' | ||
| 3363 | ); | ||
| 3364 | |||
| 3365 | while (1) { | ||
| 3366 | my $hits = $result->{hits}{hits}; | ||
| 3367 | last unless @$hits; # if no hits, we're finished | ||
| 3368 | |||
| 3369 | do_something_with($hits); | ||
| 3370 | |||
| 3371 | $result = $es->scroll( | ||
| 3372 | scroll_id => $result->{_scroll_id}, | ||
| 3373 | scroll => '5m' | ||
| 3374 | ); | ||
| 3375 | } | ||
| 3376 | |||
| 3377 | See L<http://www.elasticsearch.org/guide/reference/api/search/scroll.html> | ||
| 3378 | |||
| 3379 | =head3 scrolled_search() | ||
| 3380 | |||
| 3381 | C<scrolled_search()> returns a convenience iterator for scrolled | ||
| 3382 | searches. It accepts the standard search parameters that would be passed | ||
| 3383 | to L</"search()"> and requires a C<scroll> parameter, eg: | ||
| 3384 | |||
| 3385 | $scroller = $es->scrolled_search( | ||
| 3386 | query => {match_all=>{}}, | ||
| 3387 | scroll => '5m' # keep the scroll request | ||
| 3388 | # live for 5 minutes | ||
| 3389 | ); | ||
| 3390 | |||
| 3391 | See L<Search::Elasticsearch::Compat::ScrolledSearch>, L</"search()">, L</"searchqs()"> | ||
| 3392 | and L</"scroll()">. | ||
| 3393 | |||
| 3394 | =head3 count() | ||
| 3395 | |||
| 3396 | $result = $es->count( | ||
| 3397 | index => multi, | ||
| 3398 | type => multi, | ||
| 3399 | |||
| 3400 | # optional | ||
| 3401 | routing => [$routing,...] | ||
| 3402 | ignore_indices => 'none' | 'missing', | ||
| 3403 | |||
| 3404 | # one of: | ||
| 3405 | query => { native query }, | ||
| 3406 | queryb => { search builder query }, | ||
| 3407 | ); | ||
| 3408 | |||
| 3409 | Counts the number of documents matching the query. Documents can be matched | ||
| 3410 | against multiple indices and multiple types, eg | ||
| 3411 | |||
| 3412 | $result = $es->count( | ||
| 3413 | index => undef, # all | ||
| 3414 | type => ['user','tweet'], | ||
| 3415 | queryb => { user => 'kimchy' } | ||
| 3416 | ); | ||
| 3417 | |||
| 3418 | B<Note>: C<count()> supports L<ElasticSearch::SearchBuilder>-style | ||
| 3419 | queries via the C<queryb> parameter. See | ||
| 3420 | L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> for more details. | ||
| 3421 | |||
| 3422 | C<query> defaults to C<< {match_all=>{}} >> unless specified. | ||
| 3423 | |||
| 3424 | B<DEPRECATION>: C<count()> previously took query types at the top level, eg | ||
| 3425 | C<< $es->count( term=> { ... }) >>. This form still works, but is deprecated. | ||
| 3426 | Instead use the C<queryb> or C<query> parameter as you would in L</"search()">. | ||
| 3427 | |||
| 3428 | See also L</"search()">, | ||
| 3429 | L<http://www.elasticsearch.org/guide/reference/api/count.html> | ||
| 3430 | and L<http://www.elasticsearch.org/guide/reference/query-dsl> | ||
| 3431 | |||
| 3432 | =head3 msearch() | ||
| 3433 | |||
| 3434 | $results = $es->msearch( | ||
| 3435 | index => multi, | ||
| 3436 | type => multi, | ||
| 3437 | queries => \@queries | \%queries, | ||
| 3438 | search_type => $search_type, | ||
| 3439 | ); | ||
| 3440 | |||
| 3441 | With L</"msearch()"> you can run multiple searches in parallel. C<queries> | ||
| 3442 | can contain either an array of queries, or a hash of named queries. C<$results> | ||
| 3443 | will return either an array or hash of results, depending on what you pass in. | ||
| 3444 | |||
| 3445 | The top-level C<index>, C<type> and C<search_type> parameters define default | ||
| 3446 | values which will be used for each query, although these can be overridden in | ||
| 3447 | the query parameters: | ||
| 3448 | |||
| 3449 | $results = $es->msearch( | ||
| 3450 | index => 'my_index', | ||
| 3451 | type => 'my_type', | ||
| 3452 | queries => { | ||
| 3453 | first => { | ||
| 3454 | query => { match_all: {}} # my_index/my_type | ||
| 3455 | }, | ||
| 3456 | second => { | ||
| 3457 | index => 'other_index', | ||
| 3458 | query => { match_all: {}} # other_index/my_type | ||
| 3459 | }, | ||
| 3460 | } | ||
| 3461 | ) | ||
| 3462 | |||
| 3463 | In the above example, C<$results> would look like: | ||
| 3464 | |||
| 3465 | { | ||
| 3466 | first => { hits => ... }, | ||
| 3467 | second => { hits => ... } | ||
| 3468 | } | ||
| 3469 | |||
| 3470 | A query can contain the following options: | ||
| 3471 | |||
| 3472 | { | ||
| 3473 | index => 'index_name' | ['index_1',...], | ||
| 3474 | type => 'type_name' | ['type_1',...], | ||
| 3475 | |||
| 3476 | query => { native query }, | ||
| 3477 | queryb => { search_builder query }, | ||
| 3478 | filter => { native filter }, | ||
| 3479 | filterb => { search_builder filter }, | ||
| 3480 | |||
| 3481 | facets => { facets }, | ||
| 3482 | from => 0, | ||
| 3483 | size => 10, | ||
| 3484 | sort => { sort }, | ||
| 3485 | highlight => { highlight }, | ||
| 3486 | fields => [ 'field1', ... ], | ||
| 3487 | |||
| 3488 | explain => 0 | 1, | ||
| 3489 | indices_boost => { index_1 => 5, ... }, | ||
| 3490 | ignore_indices => 'none' | 'missing', | ||
| 3491 | min_score => 2, | ||
| 3492 | partial_fields => { partial fields }, | ||
| 3493 | preference => '_local' | '_primary' | '_primary_first' | $string, | ||
| 3494 | routing => 'routing' | ['route_1',...], | ||
| 3495 | script_fields => { script fields }, | ||
| 3496 | search_type => $search_type, | ||
| 3497 | stats => 'group_1' | ['group_1','group_2'], | ||
| 3498 | timeout => '30s', | ||
| 3499 | track_scores => 0 | 1, | ||
| 3500 | version => 0 | 1, | ||
| 3501 | } | ||
| 3502 | |||
| 3503 | See L<http://www.elasticsearch.org/guide/reference/api/multi-search.html>. | ||
| 3504 | |||
| 3505 | =head3 delete_by_query() | ||
| 3506 | |||
| 3507 | $result = $es->delete_by_query( | ||
| 3508 | index => multi, | ||
| 3509 | type => multi, | ||
| 3510 | |||
| 3511 | # optional | ||
| 3512 | consistency => 'quorum' | 'one' | 'all' | ||
| 3513 | replication => 'sync' | 'async' | ||
| 3514 | routing => [$routing,...] | ||
| 3515 | |||
| 3516 | # one of: | ||
| 3517 | query => { native query }, | ||
| 3518 | queryb => { search builder query }, | ||
| 3519 | |||
| 3520 | ); | ||
| 3521 | |||
| 3522 | Deletes any documents matching the query. Documents can be matched against | ||
| 3523 | multiple indices and multiple types, eg | ||
| 3524 | |||
| 3525 | $result = $es->delete_by_query( | ||
| 3526 | index => undef, # all | ||
| 3527 | type => ['user','tweet'], | ||
| 3528 | queryb => {user => 'kimchy' }, | ||
| 3529 | ); | ||
| 3530 | |||
| 3531 | B<Note>: C<delete_by_query()> supports L<ElasticSearch::SearchBuilder>-style | ||
| 3532 | queries via the C<queryb> parameter. See | ||
| 3533 | L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> for more details. | ||
| 3534 | |||
| 3535 | B<DEPRECATION>: C<delete_by_query()> previously took query types at the top level, | ||
| 3536 | eg C<< $es->delete_by_query( term=> { ... }) >>. This form still works, but is | ||
| 3537 | deprecated. Instead use the C<queryb> or C<query> parameter as you would in | ||
| 3538 | L</"search()">. | ||
| 3539 | |||
| 3540 | See also L</"search()">, | ||
| 3541 | L<http://www.elasticsearch.org/guide/reference/api/delete-by-query.html> | ||
| 3542 | and L<http://www.elasticsearch.org/guide/reference/query-dsl> | ||
| 3543 | |||
| 3544 | =head3 mlt() | ||
| 3545 | |||
| 3546 | # mlt == more_like_this | ||
| 3547 | |||
| 3548 | $results = $es->mlt( | ||
| 3549 | index => single, # required | ||
| 3550 | type => single, # required | ||
| 3551 | id => $id, # required | ||
| 3552 | |||
| 3553 | # optional more-like-this params | ||
| 3554 | boost_terms => float | ||
| 3555 | mlt_fields => 'scalar' or ['scalar_1', 'scalar_n'] | ||
| 3556 | max_doc_freq => integer | ||
| 3557 | max_query_terms => integer | ||
| 3558 | max_word_len => integer | ||
| 3559 | min_doc_freq => integer | ||
| 3560 | min_term_freq => integer | ||
| 3561 | min_word_len => integer | ||
| 3562 | pct_terms_to_match => float | ||
| 3563 | stop_words => 'scalar' or ['scalar_1', 'scalar_n'] | ||
| 3564 | |||
| 3565 | # optional search params | ||
| 3566 | explain => {explain} | ||
| 3567 | facets => {facets} | ||
| 3568 | fields => {fields} | ||
| 3569 | filter => { native filter }, | ||
| 3570 | filterb => { search builder filter }, | ||
| 3571 | indices_boost => { index_1 => 1.5,... } | ||
| 3572 | min_score => $score | ||
| 3573 | routing => [$routing,...] | ||
| 3574 | script_fields => { script_fields } | ||
| 3575 | search_scroll => '5m' | '10s', | ||
| 3576 | search_indices => ['index1','index2], | ||
| 3577 | search_from => integer, | ||
| 3578 | search_size => integer, | ||
| 3579 | search_type => $search_type | ||
| 3580 | search_types => ['type1','type], | ||
| 3581 | sort => {sort} | ||
| 3582 | scroll => '5m' | '30s' | ||
| 3583 | ) | ||
| 3584 | |||
| 3585 | More-like-this (mlt) finds related/similar documents. It is possible to run | ||
| 3586 | a search query with a C<more_like_this> clause (where you pass in the text | ||
| 3587 | you're trying to match), or to use this method, which uses the text of | ||
| 3588 | the document referred to by C<index/type/id>. | ||
| 3589 | |||
| 3590 | This gets transformed into a search query, so all of the search parameters | ||
| 3591 | are also available. | ||
| 3592 | |||
| 3593 | Note: C<mlt()> supports L<ElasticSearch::SearchBuilder>-style filters via | ||
| 3594 | the C<filterb> parameter. See L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> | ||
| 3595 | for more details. | ||
| 3596 | |||
| 3597 | See L<http://www.elasticsearch.org/guide/reference/api/more-like-this.html> | ||
| 3598 | and L<http://www.elasticsearch.org/guide/reference/query-dsl/mlt-query.html> | ||
| 3599 | |||
| 3600 | =head3 explain() | ||
| 3601 | |||
| 3602 | $result = $ex->explain( | ||
| 3603 | index => single, | ||
| 3604 | type => single, | ||
| 3605 | id => single, | ||
| 3606 | |||
| 3607 | |||
| 3608 | query => { native query} | ||
| 3609 | | queryb => { search builder query } | ||
| 3610 | | q => $query_string, | ||
| 3611 | |||
| 3612 | analyze_wildcard => 1 | 0, | ||
| 3613 | analyzer => $string, | ||
| 3614 | default_operator => 'OR' | 'AND', | ||
| 3615 | df => $default_field | ||
| 3616 | fields => ['_source'], | ||
| 3617 | lenient => 1 | 0, | ||
| 3618 | lowercase_expanded_terms => 1 | 0, | ||
| 3619 | preference => _local | _primary | _primary_first | $string, | ||
| 3620 | routing => $routing | ||
| 3621 | ); | ||
| 3622 | |||
| 3623 | The L<explain()> method is very useful for debugging queries. It will run | ||
| 3624 | the query on the specified document and report whether the document matches | ||
| 3625 | the query or not, and why. | ||
| 3626 | |||
| 3627 | See L<http://www.elasticsearch.org/guide/reference/api/search/explain.html> | ||
| 3628 | |||
| 3629 | =head3 validate_query() | ||
| 3630 | |||
| 3631 | $bool = $es->validate_query( | ||
| 3632 | index => multi, | ||
| 3633 | type => multi, | ||
| 3634 | |||
| 3635 | query => { native query } | ||
| 3636 | | queryb => { search builder query } | ||
| 3637 | | q => $query_string | ||
| 3638 | |||
| 3639 | explain => 0 | 1, | ||
| 3640 | ignore_indices => 'none' | 'missing', | ||
| 3641 | ); | ||
| 3642 | |||
| 3643 | Returns a hashref with C<< { valid => 1} >> if the passed in C<query> | ||
| 3644 | (native ES query) C<queryb> (SearchBuilder style query) or C<q> (Lucene | ||
| 3645 | query string) is valid. Otherwise C<valid> is false. Set C<explain> to C<1> | ||
| 3646 | to include the explanation of why the query is invalid. | ||
| 3647 | |||
| 3648 | See L<http://www.elasticsearch.org/guide/reference/api/validate.html> | ||
| 3649 | |||
| 3650 | =head2 Index Admin methods | ||
| 3651 | |||
| 3652 | =head3 index_status() | ||
| 3653 | |||
| 3654 | $result = $es->index_status( | ||
| 3655 | index => multi, | ||
| 3656 | recovery => 0 | 1, | ||
| 3657 | snapshot => 0 | 1, | ||
| 3658 | ignore_indices => 'none' | 'missing', | ||
| 3659 | ); | ||
| 3660 | |||
| 3661 | Returns the status of | ||
| 3662 | $result = $es->index_status(); #all | ||
| 3663 | $result = $es->index_status( index => ['twitter','buzz'] ); | ||
| 3664 | $result = $es->index_status( index => 'twitter' ); | ||
| 3665 | |||
| 3666 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3667 | |||
| 3668 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-status.html> | ||
| 3669 | |||
| 3670 | =head3 index_stats() | ||
| 3671 | |||
| 3672 | $result = $es->index_stats( | ||
| 3673 | index => multi, | ||
| 3674 | types => multi, | ||
| 3675 | |||
| 3676 | docs => 1|0, | ||
| 3677 | store => 1|0, | ||
| 3678 | indexing => 1|0, | ||
| 3679 | get => 1|0, | ||
| 3680 | |||
| 3681 | all => 0|1, # returns all stats | ||
| 3682 | clear => 0|1, # clears default docs,store,indexing,get,search | ||
| 3683 | |||
| 3684 | flush => 0|1, | ||
| 3685 | merge => 0|1 | ||
| 3686 | refresh => 0|1, | ||
| 3687 | |||
| 3688 | level => 'shards', | ||
| 3689 | ignore_indices => 'none' | 'missing', | ||
| 3690 | ); | ||
| 3691 | |||
| 3692 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3693 | |||
| 3694 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-stats.html> | ||
| 3695 | |||
| 3696 | =head3 index_segments() | ||
| 3697 | |||
| 3698 | $result = $es->index_segments( | ||
| 3699 | index => multi, | ||
| 3700 | ignore_indices => 'none' | 'missing', | ||
| 3701 | ); | ||
| 3702 | |||
| 3703 | Returns low-level Lucene segments information for the specified indices. | ||
| 3704 | |||
| 3705 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3706 | |||
| 3707 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-segments.html> | ||
| 3708 | |||
| 3709 | =head3 create_index() | ||
| 3710 | |||
| 3711 | $result = $es->create_index( | ||
| 3712 | index => single, | ||
| 3713 | |||
| 3714 | # optional | ||
| 3715 | settings => {...}, | ||
| 3716 | mappings => {...}, | ||
| 3717 | warmers => {...}, | ||
| 3718 | ); | ||
| 3719 | |||
| 3720 | Creates a new index, optionally passing index settings and mappings, eg: | ||
| 3721 | |||
| 3722 | $result = $es->create_index( | ||
| 3723 | index => 'twitter', | ||
| 3724 | settings => { | ||
| 3725 | number_of_shards => 3, | ||
| 3726 | number_of_replicas => 2, | ||
| 3727 | analysis => { | ||
| 3728 | analyzer => { | ||
| 3729 | default => { | ||
| 3730 | tokenizer => 'standard', | ||
| 3731 | char_filter => ['html_strip'], | ||
| 3732 | filter => [qw(standard lowercase stop asciifolding)], | ||
| 3733 | } | ||
| 3734 | } | ||
| 3735 | } | ||
| 3736 | }, | ||
| 3737 | mappings => { | ||
| 3738 | tweet => { | ||
| 3739 | properties => { | ||
| 3740 | user => { type => 'string' }, | ||
| 3741 | content => { type => 'string' }, | ||
| 3742 | date => { type => 'date' } | ||
| 3743 | } | ||
| 3744 | } | ||
| 3745 | }, | ||
| 3746 | warmers => { | ||
| 3747 | warmer_1 => { | ||
| 3748 | types => ['tweet'], | ||
| 3749 | source => { | ||
| 3750 | queryb => { date => { gt => '2012-01-01' }}, | ||
| 3751 | facets => { | ||
| 3752 | content => { | ||
| 3753 | terms => { | ||
| 3754 | field=>'content' | ||
| 3755 | } | ||
| 3756 | } | ||
| 3757 | } | ||
| 3758 | } | ||
| 3759 | } | ||
| 3760 | } | ||
| 3761 | ); | ||
| 3762 | |||
| 3763 | Throws an exception if the index already exists. | ||
| 3764 | |||
| 3765 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html> | ||
| 3766 | |||
| 3767 | =head3 delete_index() | ||
| 3768 | |||
| 3769 | $result = $es->delete_index( | ||
| 3770 | index => multi_req, | ||
| 3771 | ignore_missing => 0 | 1 # optional | ||
| 3772 | ); | ||
| 3773 | |||
| 3774 | Deletes one or more existing indices, or throws a C<Missing> exception if a | ||
| 3775 | specified index doesn't exist and C<ignore_missing> is not true: | ||
| 3776 | |||
| 3777 | $result = $es->delete_index( index => 'twitter' ); | ||
| 3778 | |||
| 3779 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html> | ||
| 3780 | |||
| 3781 | =head3 index_exists() | ||
| 3782 | |||
| 3783 | $result = $e->index_exists( | ||
| 3784 | index => multi | ||
| 3785 | ); | ||
| 3786 | |||
| 3787 | Returns C<< {ok => 1} >> if all specified indices exist, or an empty list | ||
| 3788 | if it doesn't. | ||
| 3789 | |||
| 3790 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html> | ||
| 3791 | |||
| 3792 | =head3 index_settings() | ||
| 3793 | |||
| 3794 | $result = $es->index_settings( | ||
| 3795 | index => multi, | ||
| 3796 | ); | ||
| 3797 | |||
| 3798 | Returns the current settings for all, one or many indices. | ||
| 3799 | |||
| 3800 | $result = $es->index_settings( index=> ['index_1','index_2'] ); | ||
| 3801 | |||
| 3802 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-get-settings.html> | ||
| 3803 | |||
| 3804 | =head3 update_index_settings() | ||
| 3805 | |||
| 3806 | $result = $es->update_index_settings( | ||
| 3807 | index => multi, | ||
| 3808 | settings => { ... settings ...}, | ||
| 3809 | ); | ||
| 3810 | |||
| 3811 | Update the settings for all, one or many indices. Currently only the | ||
| 3812 | C<number_of_replicas> is exposed: | ||
| 3813 | |||
| 3814 | $result = $es->update_index_settings( | ||
| 3815 | settings => { number_of_replicas => 1 } | ||
| 3816 | ); | ||
| 3817 | |||
| 3818 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3819 | |||
| 3820 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html> | ||
| 3821 | |||
| 3822 | =head3 aliases() | ||
| 3823 | |||
| 3824 | $result = $es->aliases( actions => [actions] | {actions} ) | ||
| 3825 | |||
| 3826 | Adds or removes an alias for an index, eg: | ||
| 3827 | |||
| 3828 | $result = $es->aliases( actions => [ | ||
| 3829 | { remove => { index => 'foo', alias => 'bar' }}, | ||
| 3830 | { add => { index => 'foo', alias => 'baz' }} | ||
| 3831 | ]); | ||
| 3832 | |||
| 3833 | C<actions> can be a single HASH ref, or an ARRAY ref containing multiple HASH | ||
| 3834 | refs. | ||
| 3835 | |||
| 3836 | Note: C<aliases()> supports L<ElasticSearch::SearchBuilder>-style | ||
| 3837 | filters via the C<filterb> parameter. See | ||
| 3838 | L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> for more details. | ||
| 3839 | |||
| 3840 | $result = $es->aliases( actions => [ | ||
| 3841 | { add => { | ||
| 3842 | index => 'foo', | ||
| 3843 | alias => 'baz', | ||
| 3844 | index_routing => '1', | ||
| 3845 | search_routing => '1,2', | ||
| 3846 | filterb => { foo => 'bar' } | ||
| 3847 | }} | ||
| 3848 | ]); | ||
| 3849 | |||
| 3850 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html> | ||
| 3851 | |||
| 3852 | =head3 get_aliases() | ||
| 3853 | |||
| 3854 | $result = $es->get_aliases( | ||
| 3855 | index => multi, | ||
| 3856 | ignore_missing => 0 | 1, | ||
| 3857 | ); | ||
| 3858 | |||
| 3859 | Returns a hashref listing all indices and their corresponding aliases, eg: | ||
| 3860 | |||
| 3861 | { | ||
| 3862 | "foo" : { | ||
| 3863 | "aliases" : { | ||
| 3864 | "foo_1" : { | ||
| 3865 | "search_routing" : "1,2", | ||
| 3866 | "index_routing" : "1" | ||
| 3867 | "filter" : { | ||
| 3868 | "term" : { | ||
| 3869 | "foo" : "bar" | ||
| 3870 | } | ||
| 3871 | } | ||
| 3872 | }, | ||
| 3873 | "foo_2" : {} | ||
| 3874 | } | ||
| 3875 | } | ||
| 3876 | } | ||
| 3877 | |||
| 3878 | If you pass in the optional C<index> argument, which can be an index name | ||
| 3879 | or an alias name, then it will only return the indices related | ||
| 3880 | to that argument. | ||
| 3881 | |||
| 3882 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html> | ||
| 3883 | |||
| 3884 | =head3 open_index() | ||
| 3885 | |||
| 3886 | $result = $es->open_index( index => single); | ||
| 3887 | |||
| 3888 | Opens a closed index. | ||
| 3889 | |||
| 3890 | The open and close index APIs allow you to close an index, and later on open | ||
| 3891 | it. | ||
| 3892 | |||
| 3893 | A closed index has almost no overhead on the cluster (except for maintaining | ||
| 3894 | its metadata), and is blocked for read/write operations. A closed index can | ||
| 3895 | be opened which will then go through the normal recovery process. | ||
| 3896 | |||
| 3897 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close.html> for more | ||
| 3898 | |||
| 3899 | =head3 close_index() | ||
| 3900 | |||
| 3901 | $result = $es->close_index( index => single); | ||
| 3902 | |||
| 3903 | Closes an open index. See | ||
| 3904 | L<http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close.html> for more | ||
| 3905 | |||
| 3906 | =head3 create_index_template() | ||
| 3907 | |||
| 3908 | $result = $es->create_index_template( | ||
| 3909 | name => single, | ||
| 3910 | template => $template, # required | ||
| 3911 | mappings => {...}, # optional | ||
| 3912 | settings => {...}, # optional | ||
| 3913 | warmers => {...}, # optional | ||
| 3914 | order => $order, # optional | ||
| 3915 | ); | ||
| 3916 | |||
| 3917 | Index templates allow you to define templates that will automatically be | ||
| 3918 | applied to newly created indices. You can specify both C<settings> and | ||
| 3919 | C<mappings>, and a simple pattern C<template> that controls whether | ||
| 3920 | the template will be applied to a new index. | ||
| 3921 | |||
| 3922 | For example: | ||
| 3923 | |||
| 3924 | $result = $es->create_index_template( | ||
| 3925 | name => 'my_template', | ||
| 3926 | template => 'small_*', | ||
| 3927 | settings => { number_of_shards => 1 } | ||
| 3928 | ); | ||
| 3929 | |||
| 3930 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html> for more. | ||
| 3931 | |||
| 3932 | =head3 index_template() | ||
| 3933 | |||
| 3934 | $result = $es->index_template( | ||
| 3935 | name => single | ||
| 3936 | ); | ||
| 3937 | |||
| 3938 | Retrieves the named index template. | ||
| 3939 | |||
| 3940 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html#GETting_a_Template> | ||
| 3941 | |||
| 3942 | =head3 delete_index_template() | ||
| 3943 | |||
| 3944 | $result = $es->delete_index_template( | ||
| 3945 | name => single, | ||
| 3946 | ignore_missing => 0 | 1 # optional | ||
| 3947 | ); | ||
| 3948 | |||
| 3949 | Deletes the named index template. | ||
| 3950 | |||
| 3951 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html#Deleting_a_Template> | ||
| 3952 | |||
| 3953 | =head3 flush_index() | ||
| 3954 | |||
| 3955 | $result = $es->flush_index( | ||
| 3956 | index => multi, | ||
| 3957 | full => 0 | 1, | ||
| 3958 | refresh => 0 | 1, | ||
| 3959 | ignore_indices => 'none' | 'missing', | ||
| 3960 | ); | ||
| 3961 | |||
| 3962 | Flushes one or more indices, which frees | ||
| 3963 | memory from the index by flushing data to the index storage and clearing the | ||
| 3964 | internal transaction log. By default, Elasticsearch uses memory heuristics | ||
| 3965 | in order to automatically trigger flush operations as required in order to | ||
| 3966 | clear memory. | ||
| 3967 | |||
| 3968 | Example: | ||
| 3969 | |||
| 3970 | $result = $es->flush_index( index => 'twitter' ); | ||
| 3971 | |||
| 3972 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3973 | |||
| 3974 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-flush.html> | ||
| 3975 | |||
| 3976 | =head3 refresh_index() | ||
| 3977 | |||
| 3978 | $result = $es->refresh_index( | ||
| 3979 | index => multi, | ||
| 3980 | ignore_indices => 'none' | 'missing', | ||
| 3981 | ); | ||
| 3982 | |||
| 3983 | Explicitly refreshes one or more indices, making all operations performed | ||
| 3984 | since the last refresh available for search. The (near) real-time capabilities | ||
| 3985 | depends on the index engine used. For example, the robin one requires | ||
| 3986 | refresh to be called, but by default a refresh is scheduled periodically. | ||
| 3987 | |||
| 3988 | Example: | ||
| 3989 | |||
| 3990 | $result = $es->refresh_index( index => 'twitter' ); | ||
| 3991 | |||
| 3992 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 3993 | |||
| 3994 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html> | ||
| 3995 | |||
| 3996 | =head3 optimize_index() | ||
| 3997 | |||
| 3998 | $result = $es->optimize_index( | ||
| 3999 | index => multi, | ||
| 4000 | only_deletes => 0 | 1, # only_expunge_deletes | ||
| 4001 | flush => 0 | 1, # flush after optmization | ||
| 4002 | refresh => 0 | 1, # refresh after optmization | ||
| 4003 | wait_for_merge => 1 | 0, # wait for merge to finish | ||
| 4004 | max_num_segments => int, # number of segments to optimize to | ||
| 4005 | ignore_indices => 'none' | 'missing', | ||
| 4006 | ) | ||
| 4007 | |||
| 4008 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 4009 | |||
| 4010 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize.html> | ||
| 4011 | |||
| 4012 | =head3 gateway_snapshot() | ||
| 4013 | |||
| 4014 | $result = $es->gateway_snapshot( | ||
| 4015 | index => multi, | ||
| 4016 | ignore_indices => 'none' | 'missing', | ||
| 4017 | ); | ||
| 4018 | |||
| 4019 | Explicitly performs a snapshot through the gateway of one or more indices | ||
| 4020 | (backs them up ). By default, each index gateway periodically snapshot changes, | ||
| 4021 | though it can be disabled and be controlled completely through this API. | ||
| 4022 | |||
| 4023 | Example: | ||
| 4024 | |||
| 4025 | $result = $es->gateway_snapshot( index => 'twitter' ); | ||
| 4026 | |||
| 4027 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 4028 | |||
| 4029 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot.html> | ||
| 4030 | and L<http://www.elasticsearch.org/guide/reference/modules/gateway> | ||
| 4031 | |||
| 4032 | =head3 snapshot_index() | ||
| 4033 | |||
| 4034 | C<snapshot_index()> is a synonym for L</"gateway_snapshot()"> | ||
| 4035 | |||
| 4036 | =head3 clear_cache() | ||
| 4037 | |||
| 4038 | $result = $es->clear_cache( | ||
| 4039 | index => multi, | ||
| 4040 | bloom => 0 | 1, | ||
| 4041 | field_data => 0 | 1, | ||
| 4042 | filter => 0 | 1, | ||
| 4043 | id => 0 | 1, | ||
| 4044 | fields => 'field1' | ['field1','fieldn',...], | ||
| 4045 | ignore_indices => 'none' | 'missing', | ||
| 4046 | ); | ||
| 4047 | |||
| 4048 | Clears the caches for the specified indices. By default, clears all caches, | ||
| 4049 | but if any of C<id>, C<field>, C<field_data> or C<bloom> are true, then | ||
| 4050 | it clears just the specified caches. | ||
| 4051 | |||
| 4052 | Throws a C<Missing> exception if the specified indices do not exist. | ||
| 4053 | |||
| 4054 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache.html> | ||
| 4055 | |||
| 4056 | =head2 Mapping methods | ||
| 4057 | |||
| 4058 | =head3 put_mapping() | ||
| 4059 | |||
| 4060 | $result = $es->put_mapping( | ||
| 4061 | index => multi, | ||
| 4062 | type => single, | ||
| 4063 | mapping => { ... } # required | ||
| 4064 | ignore_conflicts => 0 | 1 | ||
| 4065 | ); | ||
| 4066 | |||
| 4067 | A C<mapping> is the data definition of a C<type>. If no mapping has been | ||
| 4068 | specified, then Elasticsearch tries to infer the types of each field in | ||
| 4069 | document, by looking at its contents, eg | ||
| 4070 | |||
| 4071 | 'foo' => string | ||
| 4072 | 123 => integer | ||
| 4073 | 1.23 => float | ||
| 4074 | |||
| 4075 | However, these heuristics can be confused, so it safer (and much more powerful) | ||
| 4076 | to specify an official C<mapping> instead, eg: | ||
| 4077 | |||
| 4078 | $result = $es->put_mapping( | ||
| 4079 | index => ['twitter','buzz'], | ||
| 4080 | type => 'tweet', | ||
| 4081 | mapping => { | ||
| 4082 | _source => { compress => 1 }, | ||
| 4083 | properties => { | ||
| 4084 | user => {type => "string", index => "not_analyzed"}, | ||
| 4085 | message => {type => "string", null_value => "na"}, | ||
| 4086 | post_date => {type => "date"}, | ||
| 4087 | priority => {type => "integer"}, | ||
| 4088 | rank => {type => "float"} | ||
| 4089 | } | ||
| 4090 | } | ||
| 4091 | ); | ||
| 4092 | |||
| 4093 | See also: L<http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html> | ||
| 4094 | and L<http://www.elasticsearch.org/guide/reference/mapping> | ||
| 4095 | |||
| 4096 | B<DEPRECATION>: C<put_mapping()> previously took the mapping parameters | ||
| 4097 | at the top level, eg C<< $es->put_mapping( properties=> { ... }) >>. | ||
| 4098 | This form still works, but is deprecated. Instead use the C<mapping> | ||
| 4099 | parameter. | ||
| 4100 | |||
| 4101 | =head3 delete_mapping() | ||
| 4102 | |||
| 4103 | $result = $es->delete_mapping( | ||
| 4104 | index => multi_req, | ||
| 4105 | type => single, | ||
| 4106 | ignore_missing => 0 | 1, | ||
| 4107 | ); | ||
| 4108 | |||
| 4109 | Deletes a mapping/type in one or more indices. | ||
| 4110 | See also L<http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping.html> | ||
| 4111 | |||
| 4112 | Throws a C<Missing> exception if the indices or type don't exist and | ||
| 4113 | C<ignore_missing> is false. | ||
| 4114 | |||
| 4115 | =head3 mapping() | ||
| 4116 | |||
| 4117 | $mapping = $es->mapping( | ||
| 4118 | index => single, | ||
| 4119 | type => multi | ||
| 4120 | ); | ||
| 4121 | |||
| 4122 | Returns the mappings for all types in an index, or the mapping for the specified | ||
| 4123 | type(s), eg: | ||
| 4124 | |||
| 4125 | $mapping = $es->mapping( | ||
| 4126 | index => 'twitter', | ||
| 4127 | type => 'tweet' | ||
| 4128 | ); | ||
| 4129 | |||
| 4130 | $mappings = $es->mapping( | ||
| 4131 | index => 'twitter', | ||
| 4132 | type => ['tweet','user'] | ||
| 4133 | ); | ||
| 4134 | # { twitter => { tweet => {mapping}, user => {mapping}} } | ||
| 4135 | |||
| 4136 | Note: the index name which as used in the results is the actual index name. If | ||
| 4137 | you pass an alias name as the C<index> name, then this key will be the | ||
| 4138 | index (or indices) that the alias points to. | ||
| 4139 | |||
| 4140 | See also: L<http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping.html> | ||
| 4141 | |||
| 4142 | =head3 type_exists() | ||
| 4143 | |||
| 4144 | $result = $e->type_exists( | ||
| 4145 | index => multi, # optional | ||
| 4146 | type => multi, # required | ||
| 4147 | ignore_indices => 'none' | 'missing', | ||
| 4148 | ); | ||
| 4149 | |||
| 4150 | Returns C<< {ok => 1} >> if all specified types exist in all specified indices, | ||
| 4151 | or an empty list if they doesn't. | ||
| 4152 | |||
| 4153 | See L<http://www.elasticsearch.org/guide/reference/api/admin-indices-types-exists.html> | ||
| 4154 | |||
| 4155 | =head2 Warmer methods | ||
| 4156 | |||
| 4157 | Index warming allow you to run typical search requests to "warm up" | ||
| 4158 | new segments before they become available for search. | ||
| 4159 | Warmup searches typically include requests that require heavy loading of | ||
| 4160 | data, such as faceting or sorting on specific fields. | ||
| 4161 | |||
| 4162 | =head3 create_warmer() | ||
| 4163 | |||
| 4164 | $es->create_warmer( | ||
| 4165 | warmer => $warmer, | ||
| 4166 | index => multi, | ||
| 4167 | type => multi, | ||
| 4168 | |||
| 4169 | # optional | ||
| 4170 | |||
| 4171 | query => { raw query } | ||
| 4172 | | queryb => { search builder query }, | ||
| 4173 | |||
| 4174 | filter => { raw filter } | ||
| 4175 | | filterb => { search builder filter}, | ||
| 4176 | |||
| 4177 | facets => { facets }, | ||
| 4178 | script_fields => { script fields }, | ||
| 4179 | sort => { sort }, | ||
| 4180 | ); | ||
| 4181 | |||
| 4182 | Create an index warmer called C<$warmer>: a search which is run whenever a | ||
| 4183 | matching C<index>/C<type> segment is about to be brought online. | ||
| 4184 | |||
| 4185 | See L<https://github.com/elasticsearch/elasticsearch/issues/1913> for more. | ||
| 4186 | |||
| 4187 | =head2 warmer() | ||
| 4188 | |||
| 4189 | $result = $es->warmer( | ||
| 4190 | index => multi, # optional | ||
| 4191 | warmer => $warmer, # optional | ||
| 4192 | |||
| 4193 | ignore_missing => 0 | 1 | ||
| 4194 | ); | ||
| 4195 | |||
| 4196 | Returns any matching registered warmers. The C<$warmer> can be blank, | ||
| 4197 | the name of a particular warmer, or use wilcards, eg C<"warmer_*">. Throws | ||
| 4198 | an error if no matching warmer is found, and C<ignore_missing> is false. | ||
| 4199 | |||
| 4200 | See L<https://github.com/elasticsearch/elasticsearch/issues/1913> for more. | ||
| 4201 | |||
| 4202 | =head2 delete_warmer() | ||
| 4203 | |||
| 4204 | $result = $es->delete_warmer( | ||
| 4205 | index => multi, # required | ||
| 4206 | warmer => $warmer, # required | ||
| 4207 | |||
| 4208 | ignore_missing => 0 | 1 | ||
| 4209 | ); | ||
| 4210 | |||
| 4211 | Deletes any matching registered warmers. The C<index> parameter is | ||
| 4212 | required and can be set to C<_all> to match all indices. The C<$warmer> can be | ||
| 4213 | the name of a particular warmer, or use wilcards, eg C<"warmer_*"> | ||
| 4214 | or C<"*"> for any warmer. Throws an error if no matching warmer is found, | ||
| 4215 | and C<ignore_missing> is false. | ||
| 4216 | |||
| 4217 | See L<https://github.com/elasticsearch/elasticsearch/issues/1913> for more. | ||
| 4218 | |||
| 4219 | =head2 River admin methods | ||
| 4220 | |||
| 4221 | See L<http://www.elasticsearch.org/guide/reference/river/> | ||
| 4222 | and L<http://www.elasticsearch.org/guide/reference/river/twitter.html>. | ||
| 4223 | |||
| 4224 | =head3 create_river() | ||
| 4225 | |||
| 4226 | $result = $es->create_river( | ||
| 4227 | river => $river_name, # required | ||
| 4228 | type => $type, # required | ||
| 4229 | $type => {...}, # depends on river type | ||
| 4230 | index => {...}, # depends on river type | ||
| 4231 | ); | ||
| 4232 | |||
| 4233 | Creates a new river with name C<$name>, eg: | ||
| 4234 | |||
| 4235 | $result = $es->create_river( | ||
| 4236 | river => 'my_twitter_river', | ||
| 4237 | type => 'twitter', | ||
| 4238 | twitter => { | ||
| 4239 | user => 'user', | ||
| 4240 | password => 'password', | ||
| 4241 | }, | ||
| 4242 | index => { | ||
| 4243 | index => 'my_twitter_index', | ||
| 4244 | type => 'status', | ||
| 4245 | bulk_size => 100 | ||
| 4246 | } | ||
| 4247 | ) | ||
| 4248 | |||
| 4249 | =head3 get_river() | ||
| 4250 | |||
| 4251 | $result = $es->get_river( | ||
| 4252 | river => $river_name, | ||
| 4253 | ignore_missing => 0 | 1 # optional | ||
| 4254 | ); | ||
| 4255 | |||
| 4256 | Returns the river details eg | ||
| 4257 | |||
| 4258 | $result = $es->get_river ( river => 'my_twitter_river' ) | ||
| 4259 | |||
| 4260 | Throws a C<Missing> exception if the river doesn't exist and C<ignore_missing> | ||
| 4261 | is false. | ||
| 4262 | |||
| 4263 | =head3 delete_river() | ||
| 4264 | |||
| 4265 | $result = $es->delete_river( river => $river_name ); | ||
| 4266 | |||
| 4267 | Deletes the corresponding river, eg: | ||
| 4268 | |||
| 4269 | $result = $es->delete_river ( river => 'my_twitter_river' ) | ||
| 4270 | |||
| 4271 | See L<http://www.elasticsearch.org/guide/reference/river/>. | ||
| 4272 | |||
| 4273 | =head3 river_status() | ||
| 4274 | |||
| 4275 | $result = $es->river_status( | ||
| 4276 | river => $river_name, | ||
| 4277 | ignore_missing => 0 | 1 # optional | ||
| 4278 | ); | ||
| 4279 | |||
| 4280 | Returns the status doc for the named river. | ||
| 4281 | |||
| 4282 | Throws a C<Missing> exception if the river doesn't exist and C<ignore_missing> | ||
| 4283 | is false. | ||
| 4284 | |||
| 4285 | =head2 Percolate methods | ||
| 4286 | |||
| 4287 | See also: L<http://www.elasticsearch.org/guide/reference/api/percolate.html> | ||
| 4288 | and L<http://www.elasticsearch.org/blog/2011/02/08/percolator.html> | ||
| 4289 | |||
| 4290 | =head3 create_percolator() | ||
| 4291 | |||
| 4292 | $es->create_percolator( | ||
| 4293 | index => single | ||
| 4294 | percolator => $percolator | ||
| 4295 | |||
| 4296 | # one of queryb or query is required | ||
| 4297 | query => { native query } | ||
| 4298 | queryb => { search builder query } | ||
| 4299 | |||
| 4300 | # optional | ||
| 4301 | data => {data} | ||
| 4302 | ) | ||
| 4303 | |||
| 4304 | Create a percolator, eg: | ||
| 4305 | |||
| 4306 | $es->create_percolator( | ||
| 4307 | index => 'myindex', | ||
| 4308 | percolator => 'mypercolator', | ||
| 4309 | queryb => { field => 'foo' }, | ||
| 4310 | data => { color => 'blue' } | ||
| 4311 | ) | ||
| 4312 | |||
| 4313 | Note: C<create_percolator()> supports L<ElasticSearch::SearchBuilder>-style | ||
| 4314 | queries via the C<queryb> parameter. See | ||
| 4315 | L</"INTEGRATION WITH ElasticSearch::SearchBuilder"> for more details. | ||
| 4316 | |||
| 4317 | =head3 get_percolator() | ||
| 4318 | |||
| 4319 | $es->get_percolator( | ||
| 4320 | index => single | ||
| 4321 | percolator => $percolator, | ||
| 4322 | ignore_missing => 0 | 1, | ||
| 4323 | ) | ||
| 4324 | |||
| 4325 | Retrieves a percolator, eg: | ||
| 4326 | |||
| 4327 | $es->get_percolator( | ||
| 4328 | index => 'myindex', | ||
| 4329 | percolator => 'mypercolator', | ||
| 4330 | ) | ||
| 4331 | |||
| 4332 | Throws a C<Missing> exception if the specified index or percolator does not exist, | ||
| 4333 | and C<ignore_missing> is false. | ||
| 4334 | |||
| 4335 | =head3 delete_percolator() | ||
| 4336 | |||
| 4337 | $es->delete_percolator( | ||
| 4338 | index => single | ||
| 4339 | percolator => $percolator, | ||
| 4340 | ignore_missing => 0 | 1, | ||
| 4341 | ) | ||
| 4342 | |||
| 4343 | Deletes a percolator, eg: | ||
| 4344 | |||
| 4345 | $es->delete_percolator( | ||
| 4346 | index => 'myindex', | ||
| 4347 | percolator => 'mypercolator', | ||
| 4348 | ) | ||
| 4349 | |||
| 4350 | Throws a C<Missing> exception if the specified index or percolator does not exist, | ||
| 4351 | and C<ignore_missing> is false. | ||
| 4352 | |||
| 4353 | =head3 percolate() | ||
| 4354 | |||
| 4355 | $result = $es->percolate( | ||
| 4356 | index => single, | ||
| 4357 | type => single, | ||
| 4358 | doc => { doc to percolate }, | ||
| 4359 | |||
| 4360 | # optional | ||
| 4361 | query => { query to filter percolators }, | ||
| 4362 | prefer_local => 1 | 0, | ||
| 4363 | ) | ||
| 4364 | |||
| 4365 | Check for any percolators which match a document, optionally filtering | ||
| 4366 | which percolators could match by passing a C<query> param, for instance: | ||
| 4367 | |||
| 4368 | $result = $es->percolate( | ||
| 4369 | index => 'myindex', | ||
| 4370 | type => 'mytype', | ||
| 4371 | doc => { text => 'foo' }, | ||
| 4372 | query => { term => { color => 'blue' }} | ||
| 4373 | ); | ||
| 4374 | |||
| 4375 | Returns: | ||
| 4376 | |||
| 4377 | { | ||
| 4378 | ok => 1, | ||
| 4379 | matches => ['mypercolator'] | ||
| 4380 | } | ||
| 4381 | |||
| 4382 | =head2 Cluster admin methods | ||
| 4383 | |||
| 4384 | =head3 cluster_state() | ||
| 4385 | |||
| 4386 | $result = $es->cluster_state( | ||
| 4387 | # optional | ||
| 4388 | filter_blocks => 0 | 1, | ||
| 4389 | filter_nodes => 0 | 1, | ||
| 4390 | filter_metadata => 0 | 1, | ||
| 4391 | filter_routing_table => 0 | 1, | ||
| 4392 | filter_indices => [ 'index_1', ... 'index_n' ], | ||
| 4393 | ); | ||
| 4394 | |||
| 4395 | Returns cluster state information. | ||
| 4396 | |||
| 4397 | See L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-state.html> | ||
| 4398 | |||
| 4399 | =head3 cluster_health() | ||
| 4400 | |||
| 4401 | $result = $es->cluster_health( | ||
| 4402 | index => multi, | ||
| 4403 | level => 'cluster' | 'indices' | 'shards', | ||
| 4404 | timeout => $seconds | ||
| 4405 | wait_for_status => 'red' | 'yellow' | 'green', | ||
| 4406 | | wait_for_relocating_shards => $number_of_shards, | ||
| 4407 | | wait_for_nodes => eg '>=2', | ||
| 4408 | ); | ||
| 4409 | |||
| 4410 | Returns the status of the cluster, or index|indices or shards, where the | ||
| 4411 | returned status means: | ||
| 4412 | |||
| 4413 | =over | ||
| 4414 | |||
| 4415 | =item C<red>: Data not allocated | ||
| 4416 | |||
| 4417 | =item C<yellow>: Primary shard allocated | ||
| 4418 | |||
| 4419 | =item C<green>: All shards allocated | ||
| 4420 | |||
| 4421 | =back | ||
| 4422 | |||
| 4423 | It can block to wait for a particular status (or better), or can block to | ||
| 4424 | wait until the specified number of shards have been relocated (where 0 means | ||
| 4425 | all) or the specified number of nodes have been allocated. | ||
| 4426 | |||
| 4427 | If waiting, then a timeout can be specified. | ||
| 4428 | |||
| 4429 | For example: | ||
| 4430 | |||
| 4431 | $result = $es->cluster_health( wait_for_status => 'green', timeout => '10s') | ||
| 4432 | |||
| 4433 | See: L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-health.html> | ||
| 4434 | |||
| 4435 | =head3 cluster_settings() | ||
| 4436 | |||
| 4437 | $result = $es->cluster_settings() | ||
| 4438 | |||
| 4439 | Returns any cluster wide settings that have been set with | ||
| 4440 | L</"update_cluster_settings">. | ||
| 4441 | |||
| 4442 | See L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html> | ||
| 4443 | |||
| 4444 | =head3 update_cluster_settings() | ||
| 4445 | |||
| 4446 | $result = $es->update_cluster_settings( | ||
| 4447 | persistent => {...}, | ||
| 4448 | transient => {...}, | ||
| 4449 | ) | ||
| 4450 | |||
| 4451 | For example: | ||
| 4452 | |||
| 4453 | $result = $es->update_cluster_settings( | ||
| 4454 | persistent => { | ||
| 4455 | "discovery.zen.minimum_master_nodes" => 2 | ||
| 4456 | }, | ||
| 4457 | ) | ||
| 4458 | |||
| 4459 | C<persistent> settings will survive a full cluster restart. C<transient> | ||
| 4460 | settings won't. | ||
| 4461 | |||
| 4462 | See L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html> | ||
| 4463 | |||
| 4464 | =head3 nodes() | ||
| 4465 | |||
| 4466 | $result = $es->nodes( | ||
| 4467 | nodes => multi, | ||
| 4468 | settings => 0 | 1, | ||
| 4469 | http => 0 | 1, | ||
| 4470 | jvm => 0 | 1, | ||
| 4471 | network => 0 | 1, | ||
| 4472 | os => 0 | 1, | ||
| 4473 | process => 0 | 1, | ||
| 4474 | thread_pool => 0 | 1, | ||
| 4475 | transport => 0 | 1 | ||
| 4476 | ); | ||
| 4477 | |||
| 4478 | Returns information about one or more nodes or servers in the cluster. | ||
| 4479 | |||
| 4480 | See: L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info.html> | ||
| 4481 | |||
| 4482 | =head3 nodes_stats() | ||
| 4483 | |||
| 4484 | $result = $es->nodes_stats( | ||
| 4485 | node => multi, | ||
| 4486 | |||
| 4487 | indices => 1 | 0, | ||
| 4488 | clear => 0 | 1, | ||
| 4489 | all => 0 | 1, | ||
| 4490 | fs => 0 | 1, | ||
| 4491 | http => 0 | 1, | ||
| 4492 | jvm => 0 | 1, | ||
| 4493 | network => 0 | 1, | ||
| 4494 | os => 0 | 1, | ||
| 4495 | process => 0 | 1, | ||
| 4496 | thread_pool => 0 | 1, | ||
| 4497 | transport => 0 | 1, | ||
| 4498 | |||
| 4499 | ); | ||
| 4500 | |||
| 4501 | Returns various statistics about one or more nodes in the cluster. | ||
| 4502 | |||
| 4503 | See: L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats.html> | ||
| 4504 | |||
| 4505 | =head3 cluster_reroute() | ||
| 4506 | |||
| 4507 | $result = $es->cluster_reroute( | ||
| 4508 | commands => [ | ||
| 4509 | { move => { | ||
| 4510 | index => 'test', | ||
| 4511 | shard => 0, | ||
| 4512 | from_node => 'node1', | ||
| 4513 | to_node => 'node2', | ||
| 4514 | }}, | ||
| 4515 | { allocate => { | ||
| 4516 | index => 'test', | ||
| 4517 | shard => 1, | ||
| 4518 | node => 'node3', | ||
| 4519 | allow_primary => 0 | 1 | ||
| 4520 | }}, | ||
| 4521 | { cancel => { | ||
| 4522 | index => 'test', | ||
| 4523 | shard => 2, | ||
| 4524 | node => 'node4', | ||
| 4525 | allow_primary => 0 | 1 | ||
| 4526 | }}, | ||
| 4527 | ], | ||
| 4528 | dry_run => 0 | 1 | ||
| 4529 | ); | ||
| 4530 | |||
| 4531 | The L</cluster_reroute> command allows you to explicitly affect shard allocation | ||
| 4532 | within a cluster. For example, a shard can be moved from one node to another, | ||
| 4533 | an allocation can be cancelled, or an unassigned shard can be explicitly | ||
| 4534 | allocated on a specific node. | ||
| 4535 | |||
| 4536 | B<NOTE:> after executing the commands, the cluster will automatically | ||
| 4537 | rebalance itself if it is out of balance. Use the C<dry_run> parameter | ||
| 4538 | to see what the final outcome will be after automatic rebalancing, before | ||
| 4539 | executing the real L</cluster_reroute> call. | ||
| 4540 | |||
| 4541 | Without any C<\@commands>, the current cluster routing will be returned. | ||
| 4542 | |||
| 4543 | See L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-reroute.html> | ||
| 4544 | |||
| 4545 | =head3 shutdown() | ||
| 4546 | |||
| 4547 | $result = $es->shutdown( | ||
| 4548 | node => multi, | ||
| 4549 | delay => '5s' | '10m' # optional | ||
| 4550 | ); | ||
| 4551 | |||
| 4552 | Shuts down one or more nodes (or the whole cluster if no nodes specified), | ||
| 4553 | optionally with a delay. | ||
| 4554 | |||
| 4555 | C<node> can also have the values C<_local>, C<_master> or C<_all>. | ||
| 4556 | |||
| 4557 | See: L<http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown.html> | ||
| 4558 | |||
| 4559 | =head3 restart() | ||
| 4560 | |||
| 4561 | $result = $es->restart( | ||
| 4562 | node => multi, | ||
| 4563 | delay => '5s' | '10m' # optional | ||
| 4564 | ); | ||
| 4565 | |||
| 4566 | Restarts one or more nodes (or the whole cluster if no nodes specified), | ||
| 4567 | optionally with a delay. | ||
| 4568 | |||
| 4569 | C<node> can also have the values C<_local>, C<_master> or C<_all>. | ||
| 4570 | |||
| 4571 | See: L</"KNOWN ISSUES"> | ||
| 4572 | |||
| 4573 | =head3 current_server_version() | ||
| 4574 | |||
| 4575 | $version = $es->current_server_version() | ||
| 4576 | |||
| 4577 | Returns a HASH containing the version C<number> string and | ||
| 4578 | whether or not the current server is a C<snapshot_build>. | ||
| 4579 | |||
| 4580 | =head2 Other methods | ||
| 4581 | |||
| 4582 | =head3 use_index()/use_type() | ||
| 4583 | |||
| 4584 | C<use_index()> and C<use_type()> can be used to set default values for | ||
| 4585 | any C<index> or C<type> parameter. The default value can be overridden | ||
| 4586 | by passing a parameter (including C<undef>) to any request. | ||
| 4587 | |||
| 4588 | $es->use_index('one'); | ||
| 4589 | $es->use_type(['foo','bar']); | ||
| 4590 | |||
| 4591 | $es->index( # index: one, types: foo,bar | ||
| 4592 | data=>{ text => 'my text' } | ||
| 4593 | ); | ||
| 4594 | |||
| 4595 | $es->index( # index: two, type: foo,bar | ||
| 4596 | index=>'two', | ||
| 4597 | data=>{ text => 'my text' } | ||
| 4598 | ) | ||
| 4599 | |||
| 4600 | $es->search( type => undef ); # index: one, type: all | ||
| 4601 | |||
| 4602 | =head3 trace_calls() | ||
| 4603 | |||
| 4604 | $es->trace_calls(1); # log to STDERR | ||
| 4605 | $es->trace_calls($filename); # log to $filename.$PID | ||
| 4606 | $es->trace_calls(\*STDOUT); # log to STDOUT | ||
| 4607 | $es->trace_calls($fh); # log to given filehandle | ||
| 4608 | $es->trace_calls(0 | undef); # disable logging | ||
| 4609 | |||
| 4610 | C<trace_calls()> is used for debugging. All requests to the cluster | ||
| 4611 | are logged either to C<STDERR>, or the specified filehandle, | ||
| 4612 | or the specified filename, with the | ||
| 4613 | current C<$PID> appended, in a form that can be rerun with curl. | ||
| 4614 | |||
| 4615 | The cluster response will also be logged, and commented out. | ||
| 4616 | |||
| 4617 | Example: C<< $es->cluster_health >> is logged as: | ||
| 4618 | |||
| 4619 | # [Tue Oct 19 15:32:31 2010] Protocol: http, Server: 127.0.0.1:9200 | ||
| 4620 | curl -XGET 'http://127.0.0.1:9200/_cluster/health' | ||
| 4621 | |||
| 4622 | # [Tue Oct 19 15:32:31 2010] Response: | ||
| 4623 | # { | ||
| 4624 | # "relocating_shards" : 0, | ||
| 4625 | # "active_shards" : 0, | ||
| 4626 | # "status" : "green", | ||
| 4627 | # "cluster_name" : "elasticsearch", | ||
| 4628 | # "active_primary_shards" : 0, | ||
| 4629 | # "timed_out" : false, | ||
| 4630 | # "initializing_shards" : 0, | ||
| 4631 | # "number_of_nodes" : 1, | ||
| 4632 | # "unassigned_shards" : 0 | ||
| 4633 | # } | ||
| 4634 | |||
| 4635 | =head3 query_parser() | ||
| 4636 | |||
| 4637 | $qp = $es->query_parser(%opts); | ||
| 4638 | |||
| 4639 | Returns an L<Search::Elasticsearch::Compat::QueryParser> object for tidying up | ||
| 4640 | query strings so that they won't cause an error when passed to Elasticsearch. | ||
| 4641 | |||
| 4642 | See L<Search::Elasticsearch::Compat::QueryParser> for more information. | ||
| 4643 | |||
| 4644 | =head3 transport() | ||
| 4645 | |||
| 4646 | $transport = $es->transport | ||
| 4647 | |||
| 4648 | Returns the Transport object, eg L<Search::Elasticsearch::Compat::Transport::HTTP>. | ||
| 4649 | |||
| 4650 | =head3 timeout() | ||
| 4651 | |||
| 4652 | $timeout = $es->timeout($timeout) | ||
| 4653 | |||
| 4654 | Convenience method which does the same as: | ||
| 4655 | |||
| 4656 | $es->transport->timeout($timeout) | ||
| 4657 | |||
| 4658 | =head3 refresh_servers() | ||
| 4659 | |||
| 4660 | $es->refresh_servers() | ||
| 4661 | |||
| 4662 | Convenience method which does the same as: | ||
| 4663 | |||
| 4664 | $es->transport->refresh_servers() | ||
| 4665 | |||
| 4666 | This tries to retrieve a list of all known live servers in the Elasticsearch | ||
| 4667 | cluster by connecting to each of the last known live servers (and the initial | ||
| 4668 | list of servers passed to C<new()>) until it succeeds. | ||
| 4669 | |||
| 4670 | This list of live servers is then used in a round-robin fashion. | ||
| 4671 | |||
| 4672 | C<refresh_servers()> is called on the first request and every C<max_requests>. | ||
| 4673 | This automatic refresh can be disabled by setting C<max_requests> to C<0>: | ||
| 4674 | |||
| 4675 | $es->transport->max_requests(0) | ||
| 4676 | |||
| 4677 | Or: | ||
| 4678 | |||
| 4679 | $es = Search::Elasticsearch::Compat->new( | ||
| 4680 | servers => '127.0.0.1:9200', | ||
| 4681 | max_requests => 0, | ||
| 4682 | ); | ||
| 4683 | |||
| 4684 | =head3 builder_class() | builder() | ||
| 4685 | |||
| 4686 | The C<builder_class> is set to L<ElasticSearch::SearchBuilder> by default. | ||
| 4687 | This can be changed, eg: | ||
| 4688 | |||
| 4689 | $es = Search::Elasticsearch::Compat->new( | ||
| 4690 | servers => '127.0.0.1:9200', | ||
| 4691 | builder_class => 'My::Builder' | ||
| 4692 | ); | ||
| 4693 | |||
| 4694 | C<builder()> will C<require> the module set in C<builder_class()>, create | ||
| 4695 | an instance, and store that instance for future use. The C<builder_class> | ||
| 4696 | should implement the C<filter()> and C<query()> methods. | ||
| 4697 | |||
| 4698 | =head3 camel_case() | ||
| 4699 | |||
| 4700 | $bool = $es->camel_case($bool) | ||
| 4701 | |||
| 4702 | Gets/sets the camel_case flag. If true, then all JSON keys returned by | ||
| 4703 | Elasticsearch are in camelCase, instead of with_underscores. This flag | ||
| 4704 | does not apply to the source document being indexed or fetched. | ||
| 4705 | |||
| 4706 | Defaults to false. | ||
| 4707 | |||
| 4708 | =head3 error_trace() | ||
| 4709 | |||
| 4710 | $bool = $es->error_trace($bool) | ||
| 4711 | |||
| 4712 | If the Elasticsearch server is returning an error, setting C<error_trace> | ||
| 4713 | to true will return some internal information about where the error originates. | ||
| 4714 | Mostly useful for debugging. | ||
| 4715 | |||
| 4716 | =head1 AUTHOR | ||
| 4717 | |||
| 4718 | Clinton Gormley, C<< <drtech at cpan.org> >> | ||
| 4719 | |||
| 4720 | =head1 KNOWN ISSUES | ||
| 4721 | |||
| 4722 | =over | ||
| 4723 | |||
| 4724 | =item L</"get()"> | ||
| 4725 | |||
| 4726 | The C<_source> key that is returned from a L</"get()"> contains the original JSON | ||
| 4727 | string that was used to index the document initially. Elasticsearch parses | ||
| 4728 | JSON more leniently than L<JSON::XS>, so if invalid JSON is used to index the | ||
| 4729 | document (eg unquoted keys) then C<< $es->get(....) >> will fail with a | ||
| 4730 | JSON exception. | ||
| 4731 | |||
| 4732 | Any documents indexed via this module will be not susceptible to this problem. | ||
| 4733 | |||
| 4734 | =item L</"restart()"> | ||
| 4735 | |||
| 4736 | C<restart()> is currently disabled in Elasticsearch as it doesn't work | ||
| 4737 | correctly. Instead you can L</"shutdown()"> one or all nodes and then | ||
| 4738 | start them up from the command line. | ||
| 4739 | |||
| 4740 | =back | ||
| 4741 | |||
| 4742 | =head1 AUTHOR | ||
| 4743 | |||
| 4744 | Clinton Gormley <drtech@cpan.org> | ||
| 4745 | |||
| 4746 | =head1 COPYRIGHT AND LICENSE | ||
| 4747 | |||
| 4748 | This software is Copyright (c) 2014 by Elasticsearch BV. | ||
| 4749 | |||
| 4750 | This is free software, licensed under: | ||
| 4751 | |||
| 4752 | The Apache License, Version 2.0, January 2004 | ||
| 4753 | |||
| 4754 | =cut | ||
| 4755 | |||
| 4756 | __END__ |