| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| 3 | # See the LICENSE file in the project root for more information | ||
| 4 | |||
| 5 | package Search::Elasticsearch::Client::0_90::Direct; | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | with 'Search::Elasticsearch::Client::0_90::Role::API'; | ||
| 9 | with 'Search::Elasticsearch::Role::Client::Direct'; | ||
| 10 | |||
| 11 | our $VERSION='7.30_1'; | ||
| 12 | use Search::Elasticsearch 7.00 (); | ||
| 13 | |||
| 14 | use Search::Elasticsearch::Util qw(parse_params is_compat); | ||
| 15 | use namespace::clean; | ||
| 16 | |||
| 17 | sub _namespace {__PACKAGE__} | ||
| 18 | |||
| 19 | has 'cluster' => ( is => 'lazy' ); | ||
| 20 | has 'indices' => ( is => 'lazy' ); | ||
| 21 | has 'bulk_helper_class' => ( is => 'rw' ); | ||
| 22 | has 'scroll_helper_class' => ( is => 'rw' ); | ||
| 23 | has '_bulk_class' => ( is => 'lazy' ); | ||
| 24 | has '_scroll_class' => ( is => 'lazy' ); | ||
| 25 | |||
| 26 | __PACKAGE__->_install_api(''); | ||
| 27 | |||
| 28 | #=================================== | ||
| 29 | sub create { | ||
| 30 | #=================================== | ||
| 31 | my ( $self, $params ) = parse_params(@_); | ||
| 32 | $params->{op_type} = 'create'; | ||
| 33 | $self->_index( 'create', $params ); | ||
| 34 | } | ||
| 35 | |||
| 36 | #=================================== | ||
| 37 | sub index { | ||
| 38 | #=================================== | ||
| 39 | my ( $self, $params ) = parse_params(@_); | ||
| 40 | $self->_index( 'index', $params ); | ||
| 41 | } | ||
| 42 | |||
| 43 | #=================================== | ||
| 44 | sub _index { | ||
| 45 | #=================================== | ||
| 46 | my ( $self, $name, $params ) = @_; | ||
| 47 | my $defn = $self->api->{index}; | ||
| 48 | unless ( defined $params->{id} and length $params->{id} ) { | ||
| 49 | $defn = { %$defn, method => 'POST' }; | ||
| 50 | } | ||
| 51 | $self->perform_request( { %$defn, name => $name }, $params ); | ||
| 52 | } | ||
| 53 | |||
| 54 | #=================================== | ||
| 55 | around 'clear_scroll' => sub { | ||
| 56 | #=================================== | ||
| 57 | my $orig = shift; | ||
| 58 | my ( $self, $params ) = parse_params(@_); | ||
| 59 | $params->{scroll_id} ||= delete $params->{body}; | ||
| 60 | $orig->( $self, $params ); | ||
| 61 | }; | ||
| 62 | |||
| 63 | #=================================== | ||
| 64 | sub _build__bulk_class { | ||
| 65 | #=================================== | ||
| 66 | my $self = shift; | ||
| 67 | my $bulk_class = $self->bulk_helper_class | ||
| 68 | || 'Client::' . $self->api_version . '::Bulk'; | ||
| 69 | $self->_build_helper( 'bulk', $bulk_class ); | ||
| 70 | } | ||
| 71 | |||
| 72 | #=================================== | ||
| 73 | sub _build__scroll_class { | ||
| 74 | #=================================== | ||
| 75 | my $self = shift; | ||
| 76 | my $scroll_class = $self->scroll_helper_class | ||
| 77 | || 'Client::' . $self->api_version . '::Scroll'; | ||
| 78 | $self->_build_helper( 'scroll', $scroll_class ); | ||
| 79 | } | ||
| 80 | |||
| 81 | #=================================== | ||
| 82 | sub bulk_helper { | ||
| 83 | #=================================== | ||
| 84 | my ( $self, $params ) = parse_params(@_); | ||
| 85 | $params->{es} ||= $self; | ||
| 86 | $self->_bulk_class->new($params); | ||
| 87 | } | ||
| 88 | |||
| 89 | #=================================== | ||
| 90 | sub scroll_helper { | ||
| 91 | #=================================== | ||
| 92 | my ( $self, $params ) = parse_params(@_); | ||
| 93 | $params->{es} ||= $self; | ||
| 94 | $self->_scroll_class->new($params); | ||
| 95 | } | ||
| 96 | |||
| 97 | #=================================== | ||
| 98 | sub _build_cluster { shift->_build_namespace('Cluster') } | ||
| 99 | sub _build_indices { shift->_build_namespace('Indices') } | ||
| 100 | #=================================== | ||
| 101 | |||
| 102 | 1; | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =encoding UTF-8 | ||
| 107 | |||
| 108 | =head1 NAME | ||
| 109 | |||
| 110 | Search::Elasticsearch::Client::0_90::Direct - Thin client with full support for Elasticsearch 0.90 APIs | ||
| 111 | |||
| 112 | =head1 VERSION | ||
| 113 | |||
| 114 | version 7.30_1 | ||
| 115 | |||
| 116 | =head1 SYNOPSIS | ||
| 117 | |||
| 118 | Create a client: | ||
| 119 | |||
| 120 | use Search::Elasticsearch; | ||
| 121 | my $e = Search::Elasticsearch->new( | ||
| 122 | client => '0_90::Direct' # for the 0.90 branch | ||
| 123 | ); | ||
| 124 | |||
| 125 | Index a doc: | ||
| 126 | |||
| 127 | $e->index( | ||
| 128 | index => 'my_index', | ||
| 129 | type => 'blog_post', | ||
| 130 | id => 123, | ||
| 131 | body => { | ||
| 132 | title => "Elasticsearch clients", | ||
| 133 | content => "Interesting content...", | ||
| 134 | date => "2013-09-23" | ||
| 135 | } | ||
| 136 | ); | ||
| 137 | |||
| 138 | Get a doc: | ||
| 139 | |||
| 140 | $e->get( | ||
| 141 | index => 'my_index', | ||
| 142 | type => 'my_type', | ||
| 143 | id => 123 | ||
| 144 | ); | ||
| 145 | |||
| 146 | Search for docs: | ||
| 147 | |||
| 148 | $results = $e->search( | ||
| 149 | index => 'my_index', | ||
| 150 | body => { | ||
| 151 | query => { | ||
| 152 | match => { | ||
| 153 | title => "elasticsearch" | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | ); | ||
| 158 | |||
| 159 | Index-level requests: | ||
| 160 | |||
| 161 | $e->indices->create( index => 'my_index' ); | ||
| 162 | $e->indices->delete( index => 'my_index' ) | ||
| 163 | |||
| 164 | Cluster-level requests: | ||
| 165 | |||
| 166 | $state = $e->cluster->state; | ||
| 167 | $stats = $e->cluster->node_stats; | ||
| 168 | |||
| 169 | =head1 DESCRIPTION | ||
| 170 | |||
| 171 | The L<Search::Elasticsearch::Client::0_90::Direct> class provides the | ||
| 172 | Elasticsearch 0.90.x compatible client that is returned by: | ||
| 173 | |||
| 174 | $e = Search::Elasticsearch->new( client => '0_90::Direct' ); | ||
| 175 | |||
| 176 | It is intended to be as close as possible to the native REST API that | ||
| 177 | Elasticsearch uses, so that it is easy to translate the | ||
| 178 | L<Elasticsearch reference documentation|http://www.elasticsearch/guide> | ||
| 179 | for an API to the equivalent in this client. | ||
| 180 | |||
| 181 | This class provides the methods for L<document CRUD|/DOCUMENT CRUD METHODS>, | ||
| 182 | L<bulk document CRUD|/BULK DOCUMENT CRUD METHODS> and L<search|/SEARCH METHODS>. | ||
| 183 | It also provides access to clients for managing L<indices|/indices()> | ||
| 184 | and the L<cluster|/cluster()>. | ||
| 185 | |||
| 186 | =head1 CONVENTIONS | ||
| 187 | |||
| 188 | =head2 Parameter passing | ||
| 189 | |||
| 190 | Parameters can be passed to any request method as a list or as a hash | ||
| 191 | reference. The following two statements are equivalent: | ||
| 192 | |||
| 193 | $e->search( size => 10 ); | ||
| 194 | $e->search({size => 10}); | ||
| 195 | |||
| 196 | =head2 Path parameters | ||
| 197 | |||
| 198 | Any values that should be included in the URL path, eg C</{index}/{type}> | ||
| 199 | should be passed as top level parameters: | ||
| 200 | |||
| 201 | $e->search( index => 'my_index', type => 'my_type' ); | ||
| 202 | |||
| 203 | Alternatively, you can specify a C<path> parameter directly: | ||
| 204 | |||
| 205 | $e->search( path => '/my_index/my_type' ); | ||
| 206 | |||
| 207 | =head2 Query-string parameters | ||
| 208 | |||
| 209 | Any values that should be included in the query string should be passed | ||
| 210 | as top level parameters: | ||
| 211 | |||
| 212 | $e->search( size => 10 ); | ||
| 213 | |||
| 214 | If you pass in a C<\%params> hash, then it will be included in the | ||
| 215 | query string parameters without any error checking. The following: | ||
| 216 | |||
| 217 | $e->search( size => 10, params => { from => 5, size => 5 }) | ||
| 218 | |||
| 219 | would result in this query string: | ||
| 220 | |||
| 221 | ?from=5&size=10 | ||
| 222 | |||
| 223 | =head2 Body parameter | ||
| 224 | |||
| 225 | The request body should be passed in the C<body> key: | ||
| 226 | |||
| 227 | $e->search( | ||
| 228 | body => { | ||
| 229 | query => {...} | ||
| 230 | } | ||
| 231 | ); | ||
| 232 | |||
| 233 | The body can also be a UTF8-decoded string, which will be converted into | ||
| 234 | UTF-8 bytes and passed as is: | ||
| 235 | |||
| 236 | $e->indices->analyze( body => "The quick brown fox"); | ||
| 237 | |||
| 238 | =head2 Ignore parameter | ||
| 239 | |||
| 240 | Normally, any HTTP status code outside the 200-299 range will result in | ||
| 241 | an error being thrown. To suppress these errors, you can specify which | ||
| 242 | status codes to ignore in the C<ignore> parameter. | ||
| 243 | |||
| 244 | $e->indices->delete( | ||
| 245 | index => 'my_index', | ||
| 246 | ignore => 404 | ||
| 247 | ); | ||
| 248 | |||
| 249 | This is most useful for | ||
| 250 | L<Missing|Search::Elasticsearch::Error/Search::Elasticsearch::Error::Missing> errors, which | ||
| 251 | are triggered by a C<404> status code when some requested resource does | ||
| 252 | not exist. | ||
| 253 | |||
| 254 | Multiple error codes can be specified with an array: | ||
| 255 | |||
| 256 | $e->indices->delete( | ||
| 257 | index => 'my_index', | ||
| 258 | ignore => [404,409] | ||
| 259 | ); | ||
| 260 | |||
| 261 | =head1 CONFIGURATION | ||
| 262 | |||
| 263 | =head2 C<bulk_helper_class> | ||
| 264 | |||
| 265 | The class to use for the L</bulk_helper()> method. Defaults to | ||
| 266 | L<Search::Elasticsearch::Client::0_90::Bulk>. | ||
| 267 | |||
| 268 | =head2 C<scroll_helper_class> | ||
| 269 | |||
| 270 | The class to use for the L</scroll_helper()> method. Defaults to | ||
| 271 | L<Search::Elasticsearch::Client::0_90::Scroll>. | ||
| 272 | |||
| 273 | =head1 GENERAL METHODS | ||
| 274 | |||
| 275 | =head2 C<info()> | ||
| 276 | |||
| 277 | $info = $e->info | ||
| 278 | |||
| 279 | Returns information about the version of Elasticsearch that the responding node | ||
| 280 | is running. | ||
| 281 | |||
| 282 | =head2 C<ping()> | ||
| 283 | |||
| 284 | $e->ping | ||
| 285 | |||
| 286 | Pings a node in the cluster and returns C<1> if it receives a C<200> | ||
| 287 | response, otherwise it throws an error. | ||
| 288 | |||
| 289 | =head2 C<indices()> | ||
| 290 | |||
| 291 | $indices_client = $e->indices; | ||
| 292 | |||
| 293 | Returns an L<Search::Elasticsearch::Client::0_90::Direct::Indices> object which can be used | ||
| 294 | for managing indices, eg creating, deleting indices, managing mapping, | ||
| 295 | index settings etc. | ||
| 296 | |||
| 297 | =head2 C<cluster()> | ||
| 298 | |||
| 299 | $cluster_client = $e->cluster; | ||
| 300 | |||
| 301 | Returns an L<Search::Elasticsearch::Client::0_90::Direct::Cluster> object which can be used | ||
| 302 | for managing the cluster, eg cluster-wide settings, cluster health, | ||
| 303 | node information and stats. | ||
| 304 | |||
| 305 | =head1 DOCUMENT CRUD METHODS | ||
| 306 | |||
| 307 | These methods allow you to perform create, index, update and delete requests | ||
| 308 | for single documents: | ||
| 309 | |||
| 310 | =head2 C<index()> | ||
| 311 | |||
| 312 | $response = $e->index( | ||
| 313 | index => 'index_name', # required | ||
| 314 | type => 'type_name', # required | ||
| 315 | id => 'doc_id', # optional, otherwise auto-generated | ||
| 316 | |||
| 317 | body => { document } # required | ||
| 318 | ); | ||
| 319 | |||
| 320 | The C<index()> method is used to index a new document or to reindex | ||
| 321 | an existing document. | ||
| 322 | |||
| 323 | Query string parameters: | ||
| 324 | C<consistency>, | ||
| 325 | C<op_type>, | ||
| 326 | C<parent>, | ||
| 327 | C<percolate>, | ||
| 328 | C<refresh>, | ||
| 329 | C<replication>, | ||
| 330 | C<routing>, | ||
| 331 | C<timeout>, | ||
| 332 | C<timestamp>, | ||
| 333 | C<ttl>, | ||
| 334 | C<version>, | ||
| 335 | C<version_type> | ||
| 336 | |||
| 337 | See the L<index docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-index_.html> | ||
| 338 | for more information. | ||
| 339 | |||
| 340 | =head2 C<create()> | ||
| 341 | |||
| 342 | $response = $e->create( | ||
| 343 | index => 'index_name', # required | ||
| 344 | type => 'type_name', # required | ||
| 345 | id => 'doc_id', # optional, otherwise auto-generated | ||
| 346 | |||
| 347 | body => { document } # required | ||
| 348 | ); | ||
| 349 | |||
| 350 | The C<create()> method works exactly like the L</index()> method, except | ||
| 351 | that it will throw a C<Conflict> error if a document with the same | ||
| 352 | C<index>, C<type> and C<id> already exists. | ||
| 353 | |||
| 354 | Query string parameters: | ||
| 355 | C<consistency>, | ||
| 356 | C<op_type>, | ||
| 357 | C<parent>, | ||
| 358 | C<percolate>, | ||
| 359 | C<refresh>, | ||
| 360 | C<replication>, | ||
| 361 | C<routing>, | ||
| 362 | C<timeout>, | ||
| 363 | C<timestamp>, | ||
| 364 | C<ttl>, | ||
| 365 | C<version>, | ||
| 366 | C<version_type> | ||
| 367 | |||
| 368 | See the L<create docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-create.html> | ||
| 369 | for more information. | ||
| 370 | |||
| 371 | =head2 C<get()> | ||
| 372 | |||
| 373 | $response = $e->get( | ||
| 374 | index => 'index_name', # required | ||
| 375 | type => 'type_name', # required | ||
| 376 | id => 'doc_id', # required | ||
| 377 | ); | ||
| 378 | |||
| 379 | The C<get()> method will retrieve the document with the specified | ||
| 380 | C<index>, C<type> and C<id>, or will throw a C<Missing> error. | ||
| 381 | |||
| 382 | Query string parameters: | ||
| 383 | C<_source>, | ||
| 384 | C<_source_exclude>, | ||
| 385 | C<_source_include>, | ||
| 386 | C<fields>, | ||
| 387 | C<parent>, | ||
| 388 | C<preference>, | ||
| 389 | C<realtime>, | ||
| 390 | C<refresh>, | ||
| 391 | C<routing> | ||
| 392 | |||
| 393 | See the L<get docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-get.html> | ||
| 394 | for more information. | ||
| 395 | |||
| 396 | =head2 C<get_source()> | ||
| 397 | |||
| 398 | $response = $e->get_source( | ||
| 399 | index => 'index_name', # required | ||
| 400 | type => 'type_name', # required | ||
| 401 | id => 'doc_id', # required | ||
| 402 | ); | ||
| 403 | |||
| 404 | The C<get_source()> method works just like the L</get()> method except that | ||
| 405 | it returns just the C<_source> field (the value of the C<body> parameter | ||
| 406 | in the L</index()> method) instead of returning the C<_source> field | ||
| 407 | plus the document metadata, ie the C<_index>, C<_type> etc. | ||
| 408 | |||
| 409 | Query string parameters: | ||
| 410 | C<_source_exclude>, | ||
| 411 | C<_source_include>, | ||
| 412 | C<parent>, | ||
| 413 | C<preference>, | ||
| 414 | C<realtime>, | ||
| 415 | C<refresh>, | ||
| 416 | C<routing> | ||
| 417 | |||
| 418 | See the L<get_source docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-get.html> | ||
| 419 | for more information. | ||
| 420 | |||
| 421 | =head2 C<exists()> | ||
| 422 | |||
| 423 | $response = $e->exists( | ||
| 424 | index => 'index_name', # required | ||
| 425 | type => 'type_name', # required | ||
| 426 | id => 'doc_id', # required | ||
| 427 | ); | ||
| 428 | |||
| 429 | The C<exists()> method returns C<1> if a document with the specified | ||
| 430 | C<index>, C<type> and C<id> exists, or an empty string if it doesn't. | ||
| 431 | |||
| 432 | Query string parameters: | ||
| 433 | C<parent>, | ||
| 434 | C<preference>, | ||
| 435 | C<realtime>, | ||
| 436 | C<refresh>, | ||
| 437 | C<routing> | ||
| 438 | |||
| 439 | See the L<exists docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-get.html> | ||
| 440 | for more information. | ||
| 441 | |||
| 442 | =head2 C<delete()> | ||
| 443 | |||
| 444 | $response = $e->delete( | ||
| 445 | index => 'index_name', # required | ||
| 446 | type => 'type_name', # required | ||
| 447 | id => 'doc_id', # required | ||
| 448 | ); | ||
| 449 | |||
| 450 | The C<delete()> method will delete the document with the specified | ||
| 451 | C<index>, C<type> and C<id>, or will throw a C<Missing> error. | ||
| 452 | |||
| 453 | Query string parameters: | ||
| 454 | C<consistency>, | ||
| 455 | C<parent>, | ||
| 456 | C<refresh>, | ||
| 457 | C<replication>, | ||
| 458 | C<routing>, | ||
| 459 | C<timeout>, | ||
| 460 | C<version>, | ||
| 461 | C<version_type> | ||
| 462 | |||
| 463 | See the L<delete docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-delete.html> | ||
| 464 | for more information. | ||
| 465 | |||
| 466 | =head2 C<update()> | ||
| 467 | |||
| 468 | $response = $e->update( | ||
| 469 | index => 'index_name', # required | ||
| 470 | type => 'type_name', # required | ||
| 471 | id => 'doc_id', # required | ||
| 472 | |||
| 473 | body => { update } # required | ||
| 474 | ); | ||
| 475 | |||
| 476 | The C<update()> method updates a document with the corresponding | ||
| 477 | C<index>, C<type> and C<id> if it exists. Updates can be performed either by: | ||
| 478 | |||
| 479 | =over | ||
| 480 | |||
| 481 | =item * providing a partial document to be merged in to the existing document: | ||
| 482 | |||
| 483 | $response = $e->update( | ||
| 484 | ..., | ||
| 485 | body => { | ||
| 486 | doc => { new_field => 'new_value'}, | ||
| 487 | } | ||
| 488 | ); | ||
| 489 | |||
| 490 | =item * or with a script: | ||
| 491 | |||
| 492 | $response = $e->update( | ||
| 493 | ..., | ||
| 494 | body => { | ||
| 495 | script => "ctx._source.counter += incr", | ||
| 496 | params => { incr => 5 } | ||
| 497 | } | ||
| 498 | ); | ||
| 499 | |||
| 500 | =back | ||
| 501 | |||
| 502 | Query string parameters: | ||
| 503 | C<consistency>, | ||
| 504 | C<fields>, | ||
| 505 | C<lang>, | ||
| 506 | C<parent>, | ||
| 507 | C<percolate>, | ||
| 508 | C<realtime>, | ||
| 509 | C<refresh>, | ||
| 510 | C<replication>, | ||
| 511 | C<retry_on_conflict>, | ||
| 512 | C<routing>, | ||
| 513 | C<script>, | ||
| 514 | C<timeout>, | ||
| 515 | C<timestamp>, | ||
| 516 | C<ttl>, | ||
| 517 | C<version>, | ||
| 518 | C<version_type> | ||
| 519 | |||
| 520 | See the L<update docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-update.html> | ||
| 521 | for more information. | ||
| 522 | |||
| 523 | =head1 BULK DOCUMENT CRUD METHODS | ||
| 524 | |||
| 525 | The bulk document CRUD methods are used for running multiple CRUD actions | ||
| 526 | within a single request. By reducing the number of network requests | ||
| 527 | that need to be made, bulk requests greatly improve performance. | ||
| 528 | |||
| 529 | =head2 C<bulk()> | ||
| 530 | |||
| 531 | $response = $e->bulk( | ||
| 532 | index => 'index_name', # required if type specified | ||
| 533 | type => 'type_name', # optional | ||
| 534 | |||
| 535 | body => [ actions ] # required | ||
| 536 | ); | ||
| 537 | |||
| 538 | See L<Search::Elasticsearch::Client::0_90::Bulk> and L</bulk_helper()> for a helper module that makes | ||
| 539 | bulk indexing simpler to use. | ||
| 540 | |||
| 541 | The C<bulk()> method can perform multiple L</index()>, L</create()>, | ||
| 542 | L</delete()> or L</update()> actions with a single request. The C<body> | ||
| 543 | parameter expects an array containing the list of actions to perform. | ||
| 544 | |||
| 545 | An I<action> consists of an initial metadata hash ref containing the action | ||
| 546 | type, plus the associated metadata, eg : | ||
| 547 | |||
| 548 | { delete => { _index => 'index', _type => 'type', _id => 123 }} | ||
| 549 | |||
| 550 | The C<index> and C<create> actions then expect a hashref containing | ||
| 551 | the document itself: | ||
| 552 | |||
| 553 | { create => { _index => 'index', _type => 'type', _id => 123 }}, | ||
| 554 | { title => "A newly created document" } | ||
| 555 | |||
| 556 | And the C<update> action expects a hashref containing the update commands, | ||
| 557 | eg: | ||
| 558 | |||
| 559 | { update => { _index => 'index', _type => 'type', _id => 123 }}, | ||
| 560 | { script => "ctx._source.counter+=1" } | ||
| 561 | |||
| 562 | Each action can include the same parameters that you would pass to | ||
| 563 | the equivalent L</index()>, L</create()>, L</delete()> or L</update()> | ||
| 564 | request, except that C<_index>, C<_type> and C<_id> must be specified with | ||
| 565 | the preceding underscore. All other parameters can be specified with or | ||
| 566 | without the underscore. | ||
| 567 | |||
| 568 | For instance: | ||
| 569 | |||
| 570 | $response = $e->bulk( | ||
| 571 | index => 'index_name', # default index name | ||
| 572 | type => 'type_name', # default type name | ||
| 573 | body => [ | ||
| 574 | |||
| 575 | # create action | ||
| 576 | { create => { | ||
| 577 | _index => 'not_the_default_index', | ||
| 578 | _type => 'not_the_default_type', | ||
| 579 | _id => 123 | ||
| 580 | }}, | ||
| 581 | { title => 'Foo' }, | ||
| 582 | |||
| 583 | # index action | ||
| 584 | { index => { _id => 124 }}, | ||
| 585 | { title => 'Foo' }, | ||
| 586 | |||
| 587 | # delete action | ||
| 588 | { delete => { _id => 125 }}, | ||
| 589 | |||
| 590 | # update action | ||
| 591 | { update => { _id => 126 }}, | ||
| 592 | { script => "ctx._source.counter+1" } | ||
| 593 | ] | ||
| 594 | ); | ||
| 595 | |||
| 596 | Each action is performed separately. One failed action will not | ||
| 597 | cause the others to fail as well. | ||
| 598 | |||
| 599 | Query string parameters: | ||
| 600 | C<consistency>, | ||
| 601 | C<refresh>, | ||
| 602 | C<replication>, | ||
| 603 | C<timeout>, | ||
| 604 | C<type> | ||
| 605 | |||
| 606 | See the L<bulk docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-bulk.html> | ||
| 607 | for more information. | ||
| 608 | |||
| 609 | =head2 C<bulk_helper()> | ||
| 610 | |||
| 611 | $bulk_helper = $e->bulk_helper( @args ); | ||
| 612 | |||
| 613 | Returns a new instance of the class specified in the L</bulk_helper_class>, | ||
| 614 | which defaults to L<Search::Elasticsearch::Client::0_90::Bulk>. | ||
| 615 | |||
| 616 | =head2 C<mget()> | ||
| 617 | |||
| 618 | $results = $e->mget( | ||
| 619 | index => 'default_index', # optional, required when type specified | ||
| 620 | type => 'default_type', # optional | ||
| 621 | |||
| 622 | body => { docs or ids } # required | ||
| 623 | ); | ||
| 624 | |||
| 625 | The C<mget()> method will retrieve multiple documents with a single request. | ||
| 626 | The C<body> consists of an array of documents to retrieve: | ||
| 627 | |||
| 628 | $results = $e->mget( | ||
| 629 | index => 'default_index', | ||
| 630 | type => 'default_type', | ||
| 631 | body => { | ||
| 632 | docs => [ | ||
| 633 | { _id => 1}, | ||
| 634 | { _id => 2, _type => 'not_the_default_type' } | ||
| 635 | ] | ||
| 636 | } | ||
| 637 | ); | ||
| 638 | |||
| 639 | You can also pass any of the other parameters that the L</get()> request | ||
| 640 | accepts. | ||
| 641 | |||
| 642 | If you have specified an C<index> and C<type>, you can just include the | ||
| 643 | C<ids> of the documents to retrieve: | ||
| 644 | |||
| 645 | $results = $e->mget( | ||
| 646 | index => 'default_index', | ||
| 647 | type => 'default_type', | ||
| 648 | body => { | ||
| 649 | ids => [ 1, 2, 3] | ||
| 650 | } | ||
| 651 | ); | ||
| 652 | |||
| 653 | Query string parameters: | ||
| 654 | C<_source>, | ||
| 655 | C<_source_exclude>, | ||
| 656 | C<_source_include>, | ||
| 657 | C<fields>, | ||
| 658 | C<preference>, | ||
| 659 | C<realtime>, | ||
| 660 | C<refresh> | ||
| 661 | |||
| 662 | See the L<mget docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-multi-get.html> | ||
| 663 | for more information. | ||
| 664 | |||
| 665 | =head2 C<delete_by_query()> | ||
| 666 | |||
| 667 | $result = $e->delete_by_query( | ||
| 668 | index => 'index' | \@indices, # optional | ||
| 669 | type => 'type' | \@types, # optional | ||
| 670 | |||
| 671 | body => { query } # required | ||
| 672 | |||
| 673 | ); | ||
| 674 | |||
| 675 | The C<delete_by_query()> method deletes all documents which match the | ||
| 676 | query. For instance, to delete all documents from 2012: | ||
| 677 | |||
| 678 | $result = $e->delete_by_query( | ||
| 679 | body => { | ||
| 680 | query => { | ||
| 681 | range => { | ||
| 682 | date => { | ||
| 683 | gte => '2012-01-01', | ||
| 684 | lt => '2013-01-01' | ||
| 685 | } | ||
| 686 | } | ||
| 687 | } | ||
| 688 | } | ||
| 689 | ); | ||
| 690 | |||
| 691 | Query string parameters: | ||
| 692 | C<allow_no_indices>, | ||
| 693 | C<analyzer>, | ||
| 694 | C<consistency>, | ||
| 695 | C<default_operator>, | ||
| 696 | C<df>, | ||
| 697 | C<expand_wildcards>, | ||
| 698 | C<ignore_indices>, | ||
| 699 | C<ignore_unavailable>, | ||
| 700 | C<q>, | ||
| 701 | C<replication>, | ||
| 702 | C<routing>, | ||
| 703 | C<source>, | ||
| 704 | C<timeout> | ||
| 705 | |||
| 706 | See the L<delete_by_query docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/docs-delete-by-query.html> | ||
| 707 | for more information. | ||
| 708 | |||
| 709 | =head1 SEARCH METHODS | ||
| 710 | |||
| 711 | The search methods are used for querying documents in one, more or all indices | ||
| 712 | and of one, more or all types: | ||
| 713 | |||
| 714 | =head2 C<search()> | ||
| 715 | |||
| 716 | $results = $e->search( | ||
| 717 | index => 'index' | \@indices, # optional | ||
| 718 | type => 'type' | \@types, # optional | ||
| 719 | |||
| 720 | body => { search params } # optional | ||
| 721 | ); | ||
| 722 | |||
| 723 | The C<search()> method searches for matching documents in one or more | ||
| 724 | indices. It is just as easy to search a single index as it is to search | ||
| 725 | all the indices in your cluster. It can also return | ||
| 726 | L<facets|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-facets.thml> | ||
| 727 | (aggregations on particular fields), | ||
| 728 | L<highlighted snippets|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-highlighting.html> | ||
| 729 | and L<did-you-mean|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-suggesters-phrase.html> | ||
| 730 | or L<search-as-you-type|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-suggesters-completion.html> | ||
| 731 | suggestions. | ||
| 732 | |||
| 733 | The I<lite> L<version of search|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-uri-request.html> | ||
| 734 | allows you to specify a query string in the C<q> parameter, using the | ||
| 735 | Lucene query string syntax: | ||
| 736 | |||
| 737 | $results = $e->search( q => 'title:(elasticsearch clients)'); | ||
| 738 | |||
| 739 | However, the preferred way to search is by using the | ||
| 740 | L<Query DSL|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl.html> | ||
| 741 | to create a query, and passing that C<query> in the | ||
| 742 | L<request body|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-request-body.html>: | ||
| 743 | |||
| 744 | $results = $e->search( | ||
| 745 | body => { | ||
| 746 | query => { | ||
| 747 | match => { title => 'Elasticsearch clients'} | ||
| 748 | } | ||
| 749 | } | ||
| 750 | ); | ||
| 751 | |||
| 752 | Query string parameters: | ||
| 753 | C<_source>, | ||
| 754 | C<_source_exclude>, | ||
| 755 | C<_source_include>, | ||
| 756 | C<allow_no_indices>, | ||
| 757 | C<analyze_wildcard>, | ||
| 758 | C<analyzer>, | ||
| 759 | C<default_operator>, | ||
| 760 | C<df>, | ||
| 761 | C<expand_wildcards>, | ||
| 762 | C<explain>, | ||
| 763 | C<fields>, | ||
| 764 | C<from>, | ||
| 765 | C<ignore_indices>, | ||
| 766 | C<ignore_indices>, | ||
| 767 | C<ignore_unavailable>, | ||
| 768 | C<lenient>, | ||
| 769 | C<lowercase_expanded_terms>, | ||
| 770 | C<preference>, | ||
| 771 | C<q>, | ||
| 772 | C<routing>, | ||
| 773 | C<scroll>, | ||
| 774 | C<search_type>, | ||
| 775 | C<size>, | ||
| 776 | C<sort>, | ||
| 777 | C<source>, | ||
| 778 | C<stats>, | ||
| 779 | C<suggest_field>, | ||
| 780 | C<suggest_mode>, | ||
| 781 | C<suggest_size>, | ||
| 782 | C<suggest_text>, | ||
| 783 | C<timeout>, | ||
| 784 | C<version> | ||
| 785 | |||
| 786 | See the L<search reference|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-request-body.html> | ||
| 787 | for more information. | ||
| 788 | |||
| 789 | Also see L<Search::Elasticsearch::Transport/send_get_body_as>. | ||
| 790 | |||
| 791 | =head2 C<count()> | ||
| 792 | |||
| 793 | $results = $e->count( | ||
| 794 | index => 'index' | \@indices, # optional | ||
| 795 | type => 'type' | \@types, # optional | ||
| 796 | |||
| 797 | body => { query } # optional | ||
| 798 | ) | ||
| 799 | |||
| 800 | The C<count()> method returns the total count of all documents matching the | ||
| 801 | query: | ||
| 802 | |||
| 803 | $results = $e->count( | ||
| 804 | body => { | ||
| 805 | query => { | ||
| 806 | match => { title => 'Elasticsearch clients' } | ||
| 807 | } | ||
| 808 | } | ||
| 809 | ); | ||
| 810 | |||
| 811 | Query string parameters: | ||
| 812 | C<allow_no_indices>, | ||
| 813 | C<expand_wildcards>, | ||
| 814 | C<ignore_indices>, | ||
| 815 | C<ignore_unavailable>, | ||
| 816 | C<min_score>, | ||
| 817 | C<preference>, | ||
| 818 | C<routing>, | ||
| 819 | C<source> | ||
| 820 | |||
| 821 | See the L<count docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-count.html> | ||
| 822 | for more information. | ||
| 823 | |||
| 824 | =head2 C<scroll()> | ||
| 825 | |||
| 826 | $results = $e->scroll( | ||
| 827 | scroll => '1m', | ||
| 828 | scroll_id => $id | ||
| 829 | ); | ||
| 830 | |||
| 831 | When a L</search()> has been performed with the | ||
| 832 | C<scroll> parameter, the C<scroll()> | ||
| 833 | method allows you to keep pulling more results until the results | ||
| 834 | are exhausted. | ||
| 835 | |||
| 836 | B<NOTE:> you will almost always want to set the | ||
| 837 | C<search_type> to C<scan> in your | ||
| 838 | original C<search()> request. | ||
| 839 | |||
| 840 | See L</scroll_helper()> and L<Search::Elasticsearch::Client::0_90::Scroll> for a helper utility | ||
| 841 | which makes managing scroll requests much easier. | ||
| 842 | |||
| 843 | Query string parameters: | ||
| 844 | C<scroll>, | ||
| 845 | C<scroll_id> | ||
| 846 | |||
| 847 | See the L<scroll docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-request-scroll.html> | ||
| 848 | and the L<search_type docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search.html/search-request-search-type.html> | ||
| 849 | for more information. | ||
| 850 | |||
| 851 | =head2 C<clear_scroll()> | ||
| 852 | |||
| 853 | $response = $e->clear_scroll( | ||
| 854 | scroll_id => $id | \@ids # required | ||
| 855 | ); | ||
| 856 | |||
| 857 | The C<clear_scroll()> method can clear unfinished scroll requests, freeing | ||
| 858 | up resources on the server. | ||
| 859 | |||
| 860 | =head2 C<scroll_helper()> | ||
| 861 | |||
| 862 | $scroll_helper = $e->scroll_helper( @args ); | ||
| 863 | |||
| 864 | Returns a new instance of the class specified in the L</scroll_helper_class>, | ||
| 865 | which defaults to L<Search::Elasticsearch::Client::0_90::Scroll>. | ||
| 866 | |||
| 867 | =head2 C<msearch()> | ||
| 868 | |||
| 869 | $results = $e->msearch( | ||
| 870 | index => 'default_index' | \@indices, # optional | ||
| 871 | type => 'default_type' | \@types, # optional | ||
| 872 | |||
| 873 | body => [ searches ] # required | ||
| 874 | ); | ||
| 875 | |||
| 876 | The C<msearch()> method allows you to perform multiple searches in a single | ||
| 877 | request. Similar to the L</bulk()> request, each search request in the | ||
| 878 | C<body> consists of two hashes: the metadata hash then the search request | ||
| 879 | hash (the same data that you'd specify in the C<body> of a L</search()> | ||
| 880 | request). For instance: | ||
| 881 | |||
| 882 | $results = $e->msearch( | ||
| 883 | index => 'default_index', | ||
| 884 | type => ['default_type_1', 'default_type_2'], | ||
| 885 | body => [ | ||
| 886 | # uses defaults | ||
| 887 | {}, | ||
| 888 | { query => { match_all => {} }}, | ||
| 889 | |||
| 890 | # uses a custom index | ||
| 891 | { index => 'not_the_default_index' }, | ||
| 892 | { query => { match_all => {} }} | ||
| 893 | ] | ||
| 894 | ); | ||
| 895 | |||
| 896 | Query string parameters: | ||
| 897 | C<search_type> | ||
| 898 | |||
| 899 | See the L<msearch docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-multi-search.html> | ||
| 900 | for more information. | ||
| 901 | |||
| 902 | =head2 C<explain()> | ||
| 903 | |||
| 904 | $response = $e->explain( | ||
| 905 | index => 'my_index', # required | ||
| 906 | type => 'my_type', # required | ||
| 907 | id => 123, # required | ||
| 908 | |||
| 909 | body => { search } # required | ||
| 910 | ); | ||
| 911 | |||
| 912 | The C<explain()> method explains why the specified document did or | ||
| 913 | did not match a query, and how the relevance score was calculated. | ||
| 914 | For instance: | ||
| 915 | |||
| 916 | $response = $e->explain( | ||
| 917 | index => 'my_index', | ||
| 918 | type => 'my_type', | ||
| 919 | id => 123, | ||
| 920 | body => { | ||
| 921 | query => { | ||
| 922 | match => { title => 'Elasticsearch clients' } | ||
| 923 | } | ||
| 924 | } | ||
| 925 | ); | ||
| 926 | |||
| 927 | Query string parameters: | ||
| 928 | C<_source>, | ||
| 929 | C<_source_exclude>, | ||
| 930 | C<_source_include>, | ||
| 931 | C<analyze_wildcard>, | ||
| 932 | C<analyzer>, | ||
| 933 | C<default_operator>, | ||
| 934 | C<df>, | ||
| 935 | C<fields>, | ||
| 936 | C<lenient>, | ||
| 937 | C<lowercase_expanded_terms>, | ||
| 938 | C<parent>, | ||
| 939 | C<preference>, | ||
| 940 | C<q>, | ||
| 941 | C<routing>, | ||
| 942 | C<source> | ||
| 943 | |||
| 944 | See the L<explain docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-explain.html> | ||
| 945 | for more information. | ||
| 946 | |||
| 947 | =head2 C<percolate()> | ||
| 948 | |||
| 949 | $results = $e->percolate( | ||
| 950 | index => 'my_index', # required | ||
| 951 | type => 'my_type', # required | ||
| 952 | |||
| 953 | body => { percolation } # required | ||
| 954 | ); | ||
| 955 | |||
| 956 | Percolation is search inverted: instead of finding docs which match a | ||
| 957 | particular query, it finds queries which match a particular document, eg | ||
| 958 | for I<alert-me-when> functionality. | ||
| 959 | |||
| 960 | The C<percolate()> method runs a percolation request to find the | ||
| 961 | queries matching a particular document. In the C<body> you should pass the | ||
| 962 | C<_source> field of the document under the C<doc> key: | ||
| 963 | |||
| 964 | $results = $e->percolate( | ||
| 965 | index => 'my_index', | ||
| 966 | type => 'my_type', | ||
| 967 | body => { | ||
| 968 | doc => { | ||
| 969 | title => 'Elasticsearch rocks' | ||
| 970 | } | ||
| 971 | } | ||
| 972 | ); | ||
| 973 | |||
| 974 | Query string parameters: | ||
| 975 | C<prefer_local> | ||
| 976 | |||
| 977 | See the L<percolate docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-percolate.html> | ||
| 978 | for more information. | ||
| 979 | |||
| 980 | =head2 C<suggest()> | ||
| 981 | |||
| 982 | $results = $e->suggest( | ||
| 983 | index => 'index' | \@indices, # optional | ||
| 984 | type => 'type' | \@types, # optional | ||
| 985 | |||
| 986 | body => { suggest request } # required | ||
| 987 | ); | ||
| 988 | |||
| 989 | The C<suggest()> method is used to run | ||
| 990 | L<did-you-mean|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-suggesteres-phrase.html> | ||
| 991 | or L<search-as-you-type|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-suggesters-completion.html> | ||
| 992 | suggestion requests, which can also be run as part of a L</search()> request. | ||
| 993 | |||
| 994 | $results = $e->suggest( | ||
| 995 | index => 'my_index', | ||
| 996 | type => 'my_type', | ||
| 997 | body => { | ||
| 998 | my_suggestions => { | ||
| 999 | phrase => { | ||
| 1000 | text => 'johnny walker', | ||
| 1001 | field => 'title' | ||
| 1002 | } | ||
| 1003 | } | ||
| 1004 | } | ||
| 1005 | ); | ||
| 1006 | |||
| 1007 | Query string parameters: | ||
| 1008 | C<allow_no_indices>, | ||
| 1009 | C<expand_wildcards>, | ||
| 1010 | C<ignore_indices>, | ||
| 1011 | C<ignore_unavailable>, | ||
| 1012 | C<preference>, | ||
| 1013 | C<routing>, | ||
| 1014 | C<source> | ||
| 1015 | |||
| 1016 | =head2 C<mlt()> | ||
| 1017 | |||
| 1018 | $results = $e->mlt( | ||
| 1019 | index => 'my_index', # required | ||
| 1020 | type => 'my_type', # required | ||
| 1021 | id => 123, # required | ||
| 1022 | |||
| 1023 | body => { search } # optional | ||
| 1024 | ); | ||
| 1025 | |||
| 1026 | The C<mlt()> method runs a | ||
| 1027 | L<more-like-this query|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl-mlt-query.html> | ||
| 1028 | to find other documents which are similar to the specified document. | ||
| 1029 | |||
| 1030 | Query string parameters: | ||
| 1031 | C<boost_terms>, | ||
| 1032 | C<max_doc_freq>, | ||
| 1033 | C<max_query_terms>, | ||
| 1034 | C<max_word_len>, | ||
| 1035 | C<min_doc_freq>, | ||
| 1036 | C<min_term_freq>, | ||
| 1037 | C<min_word_len>, | ||
| 1038 | C<mlt_fields>, | ||
| 1039 | C<percent_terms_to_match>, | ||
| 1040 | C<routing>, | ||
| 1041 | C<search_from>, | ||
| 1042 | C<search_indices>, | ||
| 1043 | C<search_scroll>, | ||
| 1044 | C<search_size>, | ||
| 1045 | C<search_source>, | ||
| 1046 | C<search_type>, | ||
| 1047 | C<search_types>, | ||
| 1048 | C<stop_words> | ||
| 1049 | |||
| 1050 | See the L<mlt docs|http://www.elastic.co/guide/en/elasticsearch/reference/0.90/search-more-like-this.html> | ||
| 1051 | for more information. | ||
| 1052 | |||
| 1053 | =head1 AUTHOR | ||
| 1054 | |||
| 1055 | Enrico Zimuel <enrico.zimuel@elastic.co> | ||
| 1056 | |||
| 1057 | =head1 COPYRIGHT AND LICENSE | ||
| 1058 | |||
| 1059 | This software is Copyright (c) 2020 by Elasticsearch BV. | ||
| 1060 | |||
| 1061 | This is free software, licensed under: | ||
| 1062 | |||
| 1063 | The Apache License, Version 2.0, January 2004 | ||
| 1064 | |||
| 1065 | =cut | ||
| 1066 | |||
| 1067 | __END__ |