2_0/Direct.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran n/a Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 90 CC 1 SCAR 6.9
CC
1
Cov
0%
_namespace
2.0
create
2.0
_build__bulk_class
2.0
CRAP 2.0
1 package Search::Elasticsearch::Client::2_0::Direct;
2  
3 use Moo;
4 with 'Search::Elasticsearch::Client::2_0::Role::API';
5 with 'Search::Elasticsearch::Role::Client::Direct';
6  
7 our $VERSION='6.80';
8 use Search::Elasticsearch 6.00 ();
9  
10 use Search::Elasticsearch::Util qw(parse_params is_compat);
11 use namespace::clean;
12  
13 sub _namespace {__PACKAGE__}
14  
15 has 'cluster' => ( is => 'lazy', init_arg => undef );
16 has 'nodes' => ( is => 'lazy', init_arg => undef );
17 has 'indices' => ( is => 'lazy', init_arg => undef );
18 has 'snapshot' => ( is => 'lazy', init_arg => undef );
19 has 'cat' => ( is => 'lazy', init_arg => undef );
20 has 'tasks' => ( is => 'lazy', init_arg => undef );
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 #===================================
27 sub create {
28 #===================================
29 my ( $self, $params ) = parse_params(@_);
30 my $defn = $self->api->{index};
31 $params->{op_type} = 'create';
32 $self->perform_request( { %$defn, name => 'create' }, $params );
33 }
34  
35 #===================================
36 sub _build__bulk_class {
37 #===================================
38 my $self = shift;
39 my $bulk_class = $self->bulk_helper_class
40 || 'Client::' . $self->api_version . '::Bulk';
41 $self->_build_helper( 'bulk', $bulk_class );
42 }
43  
44 #===================================
45 sub _build__scroll_class {
46 #===================================
47 my $self = shift;
48 my $scroll_class = $self->scroll_helper_class
49 || 'Client::' . $self->api_version . '::Scroll';
50 $self->_build_helper( 'scroll', $scroll_class );
51 }
52  
53 #===================================
54 sub bulk_helper {
55 #===================================
56 my ( $self, $params ) = parse_params(@_);
57 $params->{es} ||= $self;
58 $self->_bulk_class->new($params);
59 }
60  
61 #===================================
62 sub scroll_helper {
63 #===================================
64 my ( $self, $params ) = parse_params(@_);
65 $params->{es} ||= $self;
66 $self->_scroll_class->new($params);
67 }
68  
69 #===================================
70 sub _build_cluster { shift->_build_namespace('Cluster') }
71 sub _build_nodes { shift->_build_namespace('Nodes') }
72 sub _build_indices { shift->_build_namespace('Indices') }
73 sub _build_snapshot { shift->_build_namespace('Snapshot') }
74 sub _build_cat { shift->_build_namespace('Cat') }
75 sub _build_tasks { shift->_build_namespace('Tasks') }
76 #===================================
77  
78 __PACKAGE__->_install_api('');
79  
80 1;
81  
82 =pod
83  
84 =encoding UTF-8
85  
86 =head1 NAME
87  
88 Search::Elasticsearch::Client::2_0::Direct - Thin client with full support for Elasticsearch 2.x APIs
89  
90 =head1 VERSION
91  
92 version 6.80
93  
94 =head1 SYNOPSIS
95  
96 Create a client:
97  
98 use Search::Elasticsearch;
99 my $e = Search::Elasticsearch->new(
100 client => '2_0::Direct'
101 );
102  
103 Index a doc:
104  
105 $e->index(
106 index => 'my_index',
107 type => 'blog_post',
108 id => 123,
109 body => {
110 title => "Elasticsearch clients",
111 content => "Interesting content...",
112 date => "2013-09-23"
113 }
114 );
115  
116 Get a doc:
117  
118 $e->get(
119 index => 'my_index',
120 type => 'my_type',
121 id => 123
122 );
123  
124 Search for docs:
125  
126 $results = $e->search(
127 index => 'my_index',
128 body => {
129 query => {
130 match => {
131 title => "elasticsearch"
132 }
133 }
134 }
135 );
136  
137 Index-level requests:
138  
139 $e->indices->create( index => 'my_index' );
140 $e->indices->delete( index => 'my_index' )
141  
142 Cluster-level requests:
143  
144 $health = $e->cluster->health;
145  
146 Node-level requests:
147  
148 $info = $e->nodes->info;
149 $stats = $e->nodes->stats;
150  
151 Snapshot and restore:
152  
153 $e->snapshot->create_repository(
154 repository => 'my_backups',
155 type => 'fs',
156 settings => {
157 location => '/mnt/backups'
158 }
159 );
160  
161 $e->snapshot->create(
162 repository => 'my_backups',
163 snapshot => 'backup_2014'
164 );
165  
166 Task management:
167  
168 $e->tasks->list;
169  
170 `cat` debugging:
171  
172 say $e->cat->allocation;
173 say $e->cat->health;
174  
175 =head1 DESCRIPTION
176  
177 The L<Search::Elasticsearch::Client::2_0::Direct> class provides the
178 Elasticsearch 2.x compatible client returned by:
179  
180 $e = Search::Elasticsearch->new(
181 client => "2_0::Direct"
182 );
183  
184 It is intended to be as close as possible to the native REST API that
185 Elasticsearch uses, so that it is easy to translate the
186 L<Elasticsearch reference documentation|http://www.elasticsearch/guide>
187 for an API to the equivalent in this client.
188  
189 This class provides the methods for L<document CRUD|/DOCUMENT CRUD METHODS>,
190 L<bulk document CRUD|/BULK DOCUMENT CRUD METHODS> and L<search|/SEARCH METHODS>.
191 It also provides access to clients for managing L<indices|/indices()>
192 and the L<cluster|/cluster()>.
193  
194 =head1 CONVENTIONS
195  
196 =head2 Parameter passing
197  
198 Parameters can be passed to any request method as a list or as a hash
199 reference. The following two statements are equivalent:
200  
201 $e->search( size => 10 );
202 $e->search({size => 10});
203  
204 =head2 Path parameters
205  
206 Any values that should be included in the URL path, eg C</{index}/{type}>
207 should be passed as top level parameters:
208  
209 $e->search( index => 'my_index', type => 'my_type' );
210  
211 Alternatively, you can specify a C<path> parameter directly:
212  
213 $e->search( path => '/my_index/my_type' );
214  
215 =head2 Query-string parameters
216  
217 Any values that should be included in the query string should be passed
218 as top level parameters:
219  
220 $e->search( size => 10 );
221  
222 If you pass in a C<\%params> hash, then it will be included in the
223 query string parameters without any error checking. The following:
224  
225 $e->search( size => 10, params => { from => 5, size => 5 })
226  
227 would result in this query string:
228  
229 ?from=5&size=10
230  
231 =head2 Body parameter
232  
233 The request body should be passed in the C<body> key:
234  
235 $e->search(
236 body => {
237 query => {...}
238 }
239 );
240  
241 The body can also be a UTF8-decoded string, which will be converted into
242 UTF-8 bytes and passed as is:
243  
244 $e->indices->analyze( body => "The quick brown fox");
245  
246 =head2 Filter path parameter
247  
248 Any API which returns a JSON body accepts a C<filter_path> parameter
249 which will filter the JSON down to only the specified paths. For instance,
250 if you are running a search request and only want the C<total> hits and
251 the C<_source> field for each hit (without the C<_id>, C<_index> etc),
252 you can do:
253  
254 $e->search(
255 query => {...},
256 filter_path => [ 'hits.total', 'hits.hits._source' ]
257 );
258  
259 =head2 Ignore parameter
260  
261 Normally, any HTTP status code outside the 200-299 range will result in
262 an error being thrown. To suppress these errors, you can specify which
263 status codes to ignore in the C<ignore> parameter.
264  
265 $e->indices->delete(
266 index => 'my_index',
267 ignore => 404
268 );
269  
270 This is most useful for
271 L<Missing|Search::Elasticsearch::Error/Search::Elasticsearch::Error::Missing> errors, which
272 are triggered by a C<404> status code when some requested resource does
273 not exist.
274  
275 Multiple error codes can be specified with an array:
276  
277 $e->indices->delete(
278 index => 'my_index',
279 ignore => [404,409]
280 );
281  
282 =head1 CONFIGURATION
283  
284 =head2 C<bulk_helper_class>
285  
286 The class to use for the L</bulk_helper()> method. Defaults to
287 L<Search::Elasticsearch::Client::2_0::Bulk>.
288  
289 =head2 C<scroll_helper_class>
290  
291 The class to use for the L</scroll_helper()> method. Defaults to
292 L<Search::Elasticsearch::Client::2_0::Scroll>.
293  
294 =head1 GENERAL METHODS
295  
296 =head2 C<info()>
297  
298 $info = $e->info
299  
300 Returns information about the version of Elasticsearch that the responding node
301 is running.
302  
303 =head2 C<ping()>
304  
305 $e->ping
306  
307 Pings a node in the cluster and returns C<1> if it receives a C<200>
308 response, otherwise it throws an error.
309  
310 =head2 C<indices()>
311  
312 $indices_client = $e->indices;
313  
314 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Indices> object which can be used
315 for managing indices, eg creating, deleting indices, managing mapping,
316 index settings etc.
317  
318 =head2 C<cluster()>
319  
320 $cluster_client = $e->cluster;
321  
322 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Cluster> object which can be used
323 for managing the cluster, eg cluster-wide settings and cluster health.
324  
325 =head2 C<nodes()>
326  
327 $node_client = $e->nodes;
328  
329 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Nodes> object which can be used
330 to retrieve node info and stats.
331  
332 =head2 C<snapshot()>
333  
334 $snapshot_client = $e->snapshot;
335  
336 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Snapshot> object which
337 is used for managing backup repositories and creating and restoring
338 snapshots.
339  
340 =head2 C<tasks()>
341  
342 $tasks_client = $e->tasks;
343  
344 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Tasks> object which
345 is used for accessing the task management API.
346  
347 =head2 C<cat()>
348  
349 $cat_client = $e->cat;
350  
351 Returns an L<Search::Elasticsearch::Client::2_0::Direct::Cat> object which can be used
352 to retrieve simple to read text info for debugging and monitoring an
353 Elasticsearch cluster.
354  
355 =head1 DOCUMENT CRUD METHODS
356  
357 These methods allow you to perform create, index, update and delete requests
358 for single documents:
359  
360 =head2 C<index()>
361  
362 $response = $e->index(
363 index => 'index_name', # required
364 type => 'type_name', # required
365 id => 'doc_id', # optional, otherwise auto-generated
366  
367 body => { document } # required
368 );
369  
370 The C<index()> method is used to index a new document or to reindex
371 an existing document.
372  
373 Query string parameters:
374 C<consistency>,
375 C<op_type>,
376 C<parent>,
377 C<refresh>,
378 C<routing>,
379 C<timeout>,
380 C<timestamp>,
381 C<ttl>,
382 C<version>,
383 C<version_type>
384  
385 See the L<index docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html>
386 for more information.
387  
388 =head2 C<create()>
389  
390 $response = $e->create(
391 index => 'index_name', # required
392 type => 'type_name', # required
393 id => 'doc_id', # optional, otherwise auto-generated
394  
395 body => { document } # required
396 );
397  
398 The C<create()> method works exactly like the L</index()> method, except
399 that it will throw a C<Conflict> error if a document with the same
400 C<index>, C<type> and C<id> already exists.
401  
402 Query string parameters:
403 C<consistency>,
404 C<op_type>,
405 C<parent>,
406 C<refresh>,
407 C<routing>,
408 C<timeout>,
409 C<timestamp>,
410 C<ttl>,
411 C<version>,
412 C<version_type>
413  
414 See the L<create docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-create.html>
415 for more information.
416  
417 =head2 C<get()>
418  
419 $response = $e->get(
420 index => 'index_name', # required
421 type => 'type_name', # required
422 id => 'doc_id', # required
423 );
424  
425 The C<get()> method will retrieve the document with the specified
426 C<index>, C<type> and C<id>, or will throw a C<Missing> error.
427  
428 Query string parameters:
429 C<_source>,
430 C<_source_exclude>,
431 C<_source_include>,
432 C<fields>,
433 C<parent>,
434 C<preference>,
435 C<realtime>,
436 C<refresh>,
437 C<routing>,
438 C<version>,
439 C<version_type>
440  
441 See the L<get docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html>
442 for more information.
443  
444 =head2 C<get_source()>
445  
446 $response = $e->get_source(
447 index => 'index_name', # required
448 type => 'type_name', # required
449 id => 'doc_id', # required
450 );
451  
452 The C<get_source()> method works just like the L</get()> method except that
453 it returns just the C<_source> field (the value of the C<body> parameter
454 in the L</index()> method) instead of returning the C<_source> field
455 plus the document metadata, ie the C<_index>, C<_type> etc.
456  
457 Query string parameters:
458 C<_source>,
459 C<_source_exclude>,
460 C<_source_include>,
461 C<parent>,
462 C<preference>,
463 C<realtime>,
464 C<refresh>,
465 C<routing>,
466 C<version>,
467 C<version_type>
468  
469 See the L<get_source docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html>
470 for more information.
471  
472 =head2 C<exists()>
473  
474 $response = $e->exists(
475 index => 'index_name', # required
476 type => 'type_name', # required
477 id => 'doc_id', # required
478 );
479  
480 The C<exists()> method returns C<1> if a document with the specified
481 C<index>, C<type> and C<id> exists, or an empty string if it doesn't.
482  
483 Query string parameters:
484 C<parent>,
485 C<preference>,
486 C<realtime>,
487 C<refresh>,
488 C<routing>
489  
490 See the L<exists docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html>
491 for more information.
492  
493 =head2 C<delete()>
494  
495 $response = $e->delete(
496 index => 'index_name', # required
497 type => 'type_name', # required
498 id => 'doc_id', # required
499 );
500  
501 The C<delete()> method will delete the document with the specified
502 C<index>, C<type> and C<id>, or will throw a C<Missing> error.
503  
504 Query string parameters:
505 C<consistency>,
506 C<parent>,
507 C<refresh>,
508 C<routing>,
509 C<timeout>,
510 C<version>,
511 C<version_type>
512  
513 See the L<delete docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html>
514 for more information.
515  
516 =head2 C<update()>
517  
518 $response = $e->update(
519 index => 'index_name', # required
520 type => 'type_name', # required
521 id => 'doc_id', # required
522  
523 body => { update } # required
524 );
525  
526 The C<update()> method updates a document with the corresponding
527 C<index>, C<type> and C<id> if it exists. Updates can be performed either by:
528  
529 =over
530  
531 =item * providing a partial document to be merged in to the existing document:
532  
533 $response = $e->update(
534 ...,
535 body => {
536 doc => { new_field => 'new_value'},
537 }
538 );
539  
540 =item * with an inline script:
541  
542 $response = $e->update(
543 ...,
544 body => {
545 script => {
546 inline => "ctx._source.counter += incr",
547 params => { incr => 5 }
548 }
549 }
550 );
551  
552 Make sure you enable
553 L<dynamic scripting|https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#enable-dynamic-scripting>
554 and know its implications.
555  
556 =item * with an indexed script:
557  
558 $response = $e->update(
559 ...,
560 body => {
561 script => {
562 id => $id,
563 params => { incr => 5 }
564 }
565 }
566 );
567  
568 See L<indexed scripts|https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_indexed_scripts>
569 for more information.
570  
571 =item * with a script stored as a file:
572  
573 $response = $e->update(
574 ...,
575 body => {
576 script => {
577 file => 'counter',
578 lang => 'groovy',
579 params => { incr => 5 }
580 }
581 }
582 );
583  
584 See L<scripting docs|https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html>
585 for more information.
586  
587 =back
588  
589 Query string parameters:
590 C<consistency>,
591 C<fields>,
592 C<lang>,
593 C<parent>,
594 C<realtime>,
595 C<refresh>,
596 C<retry_on_conflict>,
597 C<routing>,
598 C<script>,
599 C<script_id>,
600 C<scripted_upsert>,
601 C<timeout>,
602 C<timestamp>,
603 C<ttl>,
604 C<version>,
605 C<version_type>
606  
607 See the L<update docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html>
608 for more information.
609  
610 =head2 C<termvectors()>
611  
612 $results = $e->termvectors(
613 index => $index, # required
614 type => $type, # required
615  
616 id => $id, # optional
617 body => {...} # optional
618 )
619  
620 The C<termvectors()> method retrieves term and field statistics, positions,
621 offsets and payloads for the specified document, assuming that termvectors
622 have been enabled.
623  
624 Query string parameters:
625 C<dfs>,
626 C<field_statistics>,
627 C<fields>,
628 C<offsets>,
629 C<parent>,
630 C<payloads>,
631 C<positions>,
632 C<preference>,
633 C<realtime>,
634 C<routing>,
635 C<term_statistics>,
636 C<version>,
637 C<version_type>
638  
639 See the L<termvector docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html>
640 for more information.
641  
642 =head1 BULK DOCUMENT CRUD METHODS
643  
644 The bulk document CRUD methods are used for running multiple CRUD actions
645 within a single request. By reducing the number of network requests
646 that need to be made, bulk requests greatly improve performance.
647  
648 =head2 C<bulk()>
649  
650 $response = $e->bulk(
651 index => 'index_name', # required if type specified
652 type => 'type_name', # optional
653  
654 body => [ actions ] # required
655 );
656  
657 See L<Search::Elasticsearch::Client::2_0::Bulk> and L</bulk_helper()> for a helper module that makes
658 bulk indexing simpler to use.
659  
660 The C<bulk()> method can perform multiple L</index()>, L</create()>,
661 L</delete()> or L</update()> actions with a single request. The C<body>
662 parameter expects an array containing the list of actions to perform.
663  
664 An I<action> consists of an initial metadata hash ref containing the action
665 type, plus the associated metadata, eg :
666  
667 { delete => { _index => 'index', _type => 'type', _id => 123 }}
668  
669 The C<index> and C<create> actions then expect a hashref containing
670 the document itself:
671  
672 { create => { _index => 'index', _type => 'type', _id => 123 }},
673 { title => "A newly created document" }
674  
675 And the C<update> action expects a hashref containing the update commands,
676 eg:
677  
678 { update => { _index => 'index', _type => 'type', _id => 123 }},
679 { script => "ctx._source.counter+=1" }
680  
681 Each action can include the same parameters that you would pass to
682 the equivalent L</index()>, L</create()>, L</delete()> or L</update()>
683 request, except that C<_index>, C<_type> and C<_id> must be specified with
684 the preceding underscore. All other parameters can be specified with or
685 without the underscore.
686  
687 For instance:
688  
689 $response = $e->bulk(
690 index => 'index_name', # default index name
691 type => 'type_name', # default type name
692 body => [
693  
694 # create action
695 { create => {
696 _index => 'not_the_default_index',
697 _type => 'not_the_default_type',
698 _id => 123
699 }},
700 { title => 'Foo' },
701  
702 # index action
703 { index => { _id => 124 }},
704 { title => 'Foo' },
705  
706 # delete action
707 { delete => { _id => 125 }},
708  
709 # update action
710 { update => { _id => 126 }},
711 { script => "ctx._source.counter+1" }
712 ]
713 );
714  
715 Each action is performed separately. One failed action will not
716 cause the others to fail as well.
717  
718 Query string parameters:
719 C<consistency>,
720 C<fields>,
721 C<refresh>,
722 C<routing>,
723 C<timeout>
724  
725 See the L<bulk docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html>
726 for more information.
727  
728 =head2 C<bulk_helper()>
729  
730 $bulk_helper = $e->bulk_helper( @args );
731  
732 Returns a new instance of the class specified in the L</bulk_helper_class>,
733 which defaults to L<Search::Elasticsearch::Client::2_0::Bulk>.
734  
735 =head2 C<mget()>
736  
737 $results = $e->mget(
738 index => 'default_index', # optional, required when type specified
739 type => 'default_type', # optional
740  
741 body => { docs or ids } # required
742 );
743  
744 The C<mget()> method will retrieve multiple documents with a single request.
745 The C<body> consists of an array of documents to retrieve:
746  
747 $results = $e->mget(
748 index => 'default_index',
749 type => 'default_type',
750 body => {
751 docs => [
752 { _id => 1},
753 { _id => 2, _type => 'not_the_default_type' }
754 ]
755 }
756 );
757  
758 You can also pass any of the other parameters that the L</get()> request
759 accepts.
760  
761 If you have specified an C<index> and C<type>, you can just include the
762 C<ids> of the documents to retrieve:
763  
764 $results = $e->mget(
765 index => 'default_index',
766 type => 'default_type',
767 body => {
768 ids => [ 1, 2, 3]
769 }
770 );
771  
772 Query string parameters:
773 C<_source>,
774 C<_source_exclude>,
775 C<_source_include>,
776 C<fields>,
777 C<preference>,
778 C<realtime>,
779 C<refresh>
780  
781 See the L<mget docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html>
782 for more information.
783  
784 =head2 C<mtermvectors()>
785  
786 $results = $e->mtermvectors(
787 index => $index, # required if type specified
788 type => $type, # optional
789  
790 body => { } # optional
791 )
792  
793 Runs multiple L</termvector()> requests in a single request, eg:
794  
795 $results = $e->mtermvectors(
796 index => 'test',
797 body => {
798 docs => [
799 { _type => 'test', _id => 1, fields => ['text'] },
800 { _type => 'test', _id => 2, payloads => 1 },
801 ]
802 }
803 );
804  
805 Query string parameters:
806 C<field_statistics>,
807 C<fields>,
808 C<ids>,
809 C<offsets>,
810 C<parent>,
811 C<payloads>,
812 C<positions>,
813 C<preference>,
814 C<realtime>,
815 C<routing>,
816 C<term_statistics>,
817 C<version>,
818 C<version_type>
819  
820 See the L<mtermvectors docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-termvectors.html>
821 for more information.
822  
823 =head1 SEARCH METHODS
824  
825 The search methods are used for querying documents in one, more or all indices
826 and of one, more or all types:
827  
828 =head2 C<search()>
829  
830 $results = $e->search(
831 index => 'index' | \@indices, # optional
832 type => 'type' | \@types, # optional
833  
834 body => { search params } # optional
835 );
836  
837 The C<search()> method searches for matching documents in one or more
838 indices. It is just as easy to search a single index as it is to search
839 all the indices in your cluster. It can also return
840 L<aggregations|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html>
841 L<highlighted snippets|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-highlighting.html>
842 and L<did-you-mean|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html>
843 or L<search-as-you-type|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html>
844 suggestions.
845  
846 The I<lite> L<version of search|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html>
847 allows you to specify a query string in the C<q> parameter, using the
848 Lucene query string syntax:
849  
850 $results = $e->search( q => 'title:(elasticsearch clients)');
851  
852 However, the preferred way to search is by using the
853 L<Query DSL|http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html>
854 to create a query, and passing that C<query> in the
855 L<request body|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html>:
856  
857 $results = $e->search(
858 body => {
859 query => {
860 match => { title => 'Elasticsearch clients'}
861 }
862 }
863 );
864  
865 Query string parameters:
866 C<_source>,
867 C<_source_exclude>,
868 C<_source_include>,
869 C<allow_no_indices>,
870 C<analyze_wildcard>,
871 C<analyzer>,
872 C<default_operator>,
873 C<df>,
874 C<expand_wildcards>,
875 C<explain>,
876 C<fielddata_fields>,
877 C<fields>,
878 C<from>,
879 C<ignore_unavailable>,
880 C<lenient>,
881 C<lowercase_expanded_terms>,
882 C<preference>,
883 C<q>,
884 C<request_cache>,
885 C<routing>,
886 C<scroll>,
887 C<search_type>,
888 C<size>,
889 C<sort>,
890 C<stats>,
891 C<suggest_field>,
892 C<suggest_mode>,
893 C<suggest_size>,
894 C<suggest_text>,
895 C<terminate_after>,
896 C<timeout>,
897 C<track_scores>,
898 C<version>
899  
900 See the L<search reference|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html>
901 for more information.
902  
903 Also see L<Search::Elasticsearch::Transport/send_get_body_as>.
904  
905 =head2 C<search_exists()>
906  
907 The C<search_exists()> method is a quick version of search which can be
908 used to find out whether there are matching search results or not.
909 It doesn't return any results itself.
910  
911 $results = $e->search_exists(
912 index => 'index' | \@indices, # optional
913 type => 'type' | \@types, # optional
914  
915 body => { search params } # optional
916 );
917  
918 Query string parameters:
919 C<allow_no_indices>,
920 C<analyze_wildcard>,
921 C<analyzer>,
922 C<default_operator>,
923 C<df>,
924 C<expand_wildcards>,
925 C<ignore_unavailable>,
926 C<lenient>,
927 C<lowercase_expanded_terms>
928 C<min_score>,
929 C<preference>,
930 C<q>,
931 C<routing>
932  
933 See the L<search exists reference|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-exists.html>
934 for more information.
935  
936 =head2 C<count()>
937  
938 $results = $e->count(
939 index => 'index' | \@indices, # optional
940 type => 'type' | \@types, # optional
941  
942 body => { query } # optional
943 )
944  
945 The C<count()> method returns the total count of all documents matching the
946 query:
947  
948 $results = $e->count(
949 body => {
950 query => {
951 match => { title => 'Elasticsearch clients' }
952 }
953 }
954 );
955  
956 Query string parameters:
957 C<allow_no_indices>,
958 C<analyze_wildcard>,
959 C<analyzer>,
960 C<default_operator>,
961 C<df>,
962 C<expand_wildcards>,
963 C<ignore_unavailable>,
964 C<lenient>,
965 C<lowercase_expanded_terms>
966 C<min_score>,
967 C<preference>,
968 C<q>,
969 C<routing>
970  
971 See the L<count docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html>
972 for more information.
973  
974 =head2 C<search_template()>
975  
976 $results = $e->search_template(
977 index => 'index' | \@indices, # optional
978 type => 'type' | \@types, # optional
979  
980 body => { search params } # optional
981 );
982  
983 Perform a search by specifying a template (either predefined or defined
984 within the C<body>) and parameters to use with the template, eg:
985  
986 $results = $e->search_template(
987 body => {
988 inline => {
989 query => {
990 match => {
991 "{{my_field}}" => "{{my_value}}"
992 }
993 },
994 size => "{{my_size}}"
995 },
996 params => {
997 my_field => 'foo',
998 my_value => 'bar',
999 my_size => 5
1000 }
1001 }
1002 );
1003  
1004 See the L<search template docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html>
1005 for more information.
1006  
1007 Query string parameters:
1008 C<allow_no_indices>,
1009 C<expand_wildcards>,
1010 C<ignore_unavailable>,
1011 C<preference>,
1012 C<scroll>,
1013 C<search_type>
1014  
1015 =head2 C<render_search_template()>
1016  
1017 $response = $e->render_search_template(
1018 id => 'id', # optional
1019 body => { template } # optional
1020 );
1021  
1022 Renders the template, filling in the passed-in parameters and returns the resulting JSON, eg:
1023  
1024 $results = $e->render_search_template(
1025 body => {
1026 inline => {
1027 query => {
1028 match => {
1029 "{{my_field}}" => "{{my_value}}"
1030 }
1031 },
1032 size => "{{my_size}}"
1033 },
1034 params => {
1035 my_field => 'foo',
1036 my_value => 'bar',
1037 my_size => 5
1038 }
1039 }
1040 );
1041  
1042 See the L<search template docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html>
1043 for more information.
1044  
1045 =head2 C<scroll()>
1046  
1047 $results = $e->scroll(
1048 scroll => '1m',
1049 scroll_id => $id
1050 );
1051  
1052 When a L</search()> has been performed with the
1053 C<scroll> parameter, the C<scroll()>
1054 method allows you to keep pulling more results until the results
1055 are exhausted.
1056  
1057 B<NOTE:> you will almost always want to set the
1058 C<search_type> to C<scan> in your
1059 original C<search()> request.
1060  
1061 See L</scroll_helper()> and L<Search::Elasticsearch::Client::2_0::Scroll> for a helper utility
1062 which makes managing scroll requests much easier.
1063  
1064 Query string parameters:
1065 C<scroll>,
1066 C<scroll_id>
1067  
1068 See the L<scroll docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html>
1069 and the L<search_type docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search.html/search-request-search-type.html>
1070 for more information.
1071  
1072 =head2 C<clear_scroll()>
1073  
1074 $response = $e->clear_scroll(
1075 scroll_id => $id | \@ids # required
1076 );
1077  
1078 Or
1079  
1080 $response = $e->clear_scroll(
1081 body => $id
1082 );
1083  
1084 The C<clear_scroll()> method can clear unfinished scroll requests, freeing
1085 up resources on the server.
1086  
1087 =head2 C<scroll_helper()>
1088  
1089 $scroll_helper = $e->scroll_helper( @args );
1090  
1091 Returns a new instance of the class specified in the L</scroll_helper_class>,
1092 which defaults to L<Search::Elasticsearch::Client::2_0::Scroll>.
1093  
1094 =head2 C<msearch()>
1095  
1096 $results = $e->msearch(
1097 index => 'default_index' | \@indices, # optional
1098 type => 'default_type' | \@types, # optional
1099  
1100 body => [ searches ] # required
1101 );
1102  
1103 The C<msearch()> method allows you to perform multiple searches in a single
1104 request. Similar to the L</bulk()> request, each search request in the
1105 C<body> consists of two hashes: the metadata hash then the search request
1106 hash (the same data that you'd specify in the C<body> of a L</search()>
1107 request). For instance:
1108  
1109 $results = $e->msearch(
1110 index => 'default_index',
1111 type => ['default_type_1', 'default_type_2'],
1112 body => [
1113 # uses defaults
1114 {},
1115 { query => { match_all => {} }},
1116  
1117 # uses a custom index
1118 { index => 'not_the_default_index' },
1119 { query => { match_all => {} }}
1120 ]
1121 );
1122  
1123 Query string parameters:
1124 C<search_type>
1125  
1126 See the L<msearch docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html>
1127 for more information.
1128  
1129 =head2 C<explain()>
1130  
1131 $response = $e->explain(
1132 index => 'my_index', # required
1133 type => 'my_type', # required
1134 id => 123, # required
1135  
1136 body => { search } # required
1137 );
1138  
1139 The C<explain()> method explains why the specified document did or
1140 did not match a query, and how the relevance score was calculated.
1141 For instance:
1142  
1143 $response = $e->explain(
1144 index => 'my_index',
1145 type => 'my_type',
1146 id => 123,
1147 body => {
1148 query => {
1149 match => { title => 'Elasticsearch clients' }
1150 }
1151 }
1152 );
1153  
1154 Query string parameters:
1155 C<_source>,
1156 C<_source_exclude>,
1157 C<_source_include>,
1158 C<analyze_wildcard>,
1159 C<analyzer>,
1160 C<default_operator>,
1161 C<df>,
1162 C<fields>,
1163 C<lenient>,
1164 C<lowercase_expanded_terms>,
1165 C<parent>,
1166 C<preference>,
1167 C<q>,
1168 C<routing>
1169  
1170 See the L<explain docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html>
1171 for more information.
1172  
1173 =head2 C<field_stats()>
1174  
1175 $response = $e->field_stats(
1176 index => 'index' | \@indices, # optional
1177 fields => 'field' | \@fields, # optional
1178 level => 'cluster' | 'indices', # optional
1179 body => { filters } # optional
1180 );
1181  
1182 The C<field-stats> API returns statistical properties of a field
1183 (such as min and max values) without executing a search.
1184  
1185 Query string parameters:
1186 C<allow_no_indices>,
1187 C<expand_wildcards>,
1188 C<fields>,
1189 C<ignore_unavailable>,
1190 C<level>
1191  
1192 See the L<field-stats docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-field-stats.html>
1193 for more information.
1194  
1195 =head2 C<search_shards()>
1196  
1197 $response = $e->search_shards(
1198 index => 'index' | \@indices, # optional
1199 type => 'type' | \@types, # optional
1200 )
1201  
1202 The C<search_shards()> method returns information about which shards on
1203 which nodes will execute a search request.
1204  
1205 Query string parameters:
1206 C<allow_no_indices>,
1207 C<expand_wildcards>,
1208 C<ignore_unavailable>,
1209 C<local>,
1210 C<preference>,
1211 C<routing>
1212  
1213 See the L<search-shards docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-shards.html>
1214 for more information.
1215  
1216 =head1 CRUD-BY-QUERY METHODS
1217  
1218 =head2 C<delete_by_query()>
1219  
1220 $response = $e->delete_by_query(
1221 index => 'index' | \@indices, # optional
1222 type => 'type' | \@types, # optional,
1223 body => { delete-by-query } # optional
1224 );
1225  
1226 The C<delete_by_query()> method (available with the
1227 L<delete-by-query plugin|https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html>)
1228 deletes all documents which match the specified query.
1229  
1230 Query string parameters:
1231 C<allow_no_indices>,
1232 C<analyzer>,
1233 C<default_operator>,
1234 C<df>,
1235 C<expand_wildcards>,
1236 C<filter_path>,
1237 C<ignore_unavailable>,
1238 C<q>,
1239 C<routing>,
1240 C<timeout>
1241  
1242 See the L<delete-by-query docs|https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html>
1243 for more information.
1244  
1245 =head2 C<reindex()>
1246  
1247 $response = $e->reindex(
1248 body => { reindex } # required
1249 );
1250  
1251 The C<reindex()> API is used to index documents from one index or multiple indices
1252 to a new index.
1253  
1254 Query string parameters:
1255 C<consistency>,
1256 C<refresh>,
1257 C<requests_per_second>,
1258 C<timeout>,
1259 C<wait_for_completion>
1260  
1261 See the L<reindex docs|https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html>
1262 for more information.
1263  
1264 =head2 C<update_by_query()>
1265  
1266 $response = $e->update_by_query(
1267 index => 'index' | \@indices, # optional
1268 type => 'type' | \@types, # optional,
1269 body => { update-by-query } # optional
1270 );
1271  
1272 The C<update_by_query()> API is used to bulk update documents from one index or
1273 multiple indices using a script.
1274  
1275 Query string parameters:
1276 C<_source>,
1277 C<_source_exclude>,
1278 C<_source_include>,
1279 C<allow_no_indices>,
1280 C<analyze_wildcard>,
1281 C<analyzer>,
1282 C<conflicts>,
1283 C<consistency>,
1284 C<default_operator>,
1285 C<df>,
1286 C<expand_wildcards>,
1287 C<explain>,
1288 C<fielddata_fields>,
1289 C<fields>,
1290 C<from>,
1291 C<ignore_unavailable>,
1292 C<lenient>,
1293 C<lowercase_expanded_terms>,
1294 C<preference>,
1295 C<q>,
1296 C<refresh>,
1297 C<request_cache>,
1298 C<requests_per_second>,
1299 C<routing>,
1300 C<scroll>,
1301 C<scroll_size>,
1302 C<search_timeout>,
1303 C<search_type>,
1304 C<size>,
1305 C<sort>,
1306 C<stats>,
1307 C<suggest_field>,
1308 C<suggest_mode>,
1309 C<suggest_size>,
1310 C<suggest_text>,
1311 C<terminate_after>,
1312 C<timeout>,
1313 C<track_scores>,
1314 C<version>,
1315 C<version_type>,
1316 C<wait_for_completion>
1317  
1318 See the L<update_by_query docs|https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html>
1319 for more information.
1320  
1321 =head2 C<reindex_rethrottle>
1322  
1323 $response = $e->reindex_rethrottle(
1324 task_id => 'task_id', # required
1325 requests_per_second => $req_per_second
1326 );
1327  
1328 The C<reindex_rethrottle()> API is used to dynamically update the throtting
1329 of an existing reindex request, identified by C<task_id>.
1330  
1331 Query string parameters:
1332 C<requests_per_second>
1333  
1334 See the L<reindex docs|https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html>
1335 for more information.
1336  
1337 =head1 PERCOLATION METHODS
1338  
1339 =head2 C<percolate()>
1340  
1341 $results = $e->percolate(
1342 index => 'my_index', # required
1343 type => 'my_type', # required
1344  
1345 body => { percolation } # required
1346 );
1347  
1348 Percolation is search inverted: instead of finding docs which match a
1349 particular query, it finds queries which match a particular document, eg
1350 for I<alert-me-when> functionality.
1351  
1352 The C<percolate()> method runs a percolation request to find the
1353 queries matching a particular document. In the C<body> you should pass the
1354 C<_source> field of the document under the C<doc> key:
1355  
1356 $results = $e->percolate(
1357 index => 'my_index',
1358 type => 'my_type',
1359 body => {
1360 doc => {
1361 title => 'Elasticsearch rocks'
1362 }
1363 }
1364 );
1365  
1366 Query string parameters:
1367 C<allow_no_indices>,
1368 C<expand_wildcards>,
1369 C<ignore_unavailable>,
1370 C<percolate_format>,
1371 C<percolate_index>,
1372 C<percolate_preference>,
1373 C<percolate_routing>,
1374 C<percolate_type>,
1375 C<preference>,
1376 C<routing>,
1377 C<version>,
1378 C<version_type>
1379  
1380 See the L<percolate docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html>
1381 for more information.
1382  
1383 =head2 C<count_percolate()>
1384  
1385 $results = $e->count_percolate(
1386 index => 'my_index', # required
1387 type => 'my_type', # required
1388  
1389 body => { percolation } # required
1390 );
1391  
1392 The L</count_percolate()> request works just like the L</percolate()>
1393 request except that it returns a count of all matching queries, instead
1394 of the queries themselves.
1395  
1396 $results = $e->count_percolate(
1397 index => 'my_index',
1398 type => 'my_type',
1399 body => {
1400 doc => {
1401 title => 'Elasticsearch rocks'
1402 }
1403 }
1404 );
1405  
1406 Query string parameters:
1407 C<allow_no_indices>,
1408 C<expand_wildcards>,
1409 C<ignore_unavailable>,
1410 C<percolate_index>,
1411 C<percolate_type>,
1412 C<preference>,
1413 C<routing>,
1414 C<version>,
1415 C<version_type>
1416  
1417 See the L<percolate docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html>
1418 for more information.
1419  
1420 =head2 C<mpercolate()>
1421  
1422 $results = $e->mpercolate(
1423 index => 'my_index', # required if type
1424 type => 'my_type', # optional
1425  
1426 body => [ percolation requests ] # required
1427 );
1428  
1429 Multi-percolation allows multiple L</percolate()> requests to be run
1430 in a single request.
1431  
1432 $results = $e->mpercolate(
1433 index => 'my_index',
1434 type => 'my_type',
1435 body => [
1436 # first request
1437 { percolate => {
1438 index => 'twitter',
1439 type => 'tweet'
1440 }},
1441 { doc => {message => 'some_text' }},
1442  
1443 # second request
1444 { percolate => {
1445 index => 'twitter',
1446 type => 'tweet',
1447 id => 1
1448 }},
1449 {},
1450 ]
1451 );
1452  
1453 Query string parameters:
1454 C<allow_no_indices>,
1455 C<expand_wildcards>,
1456 C<ignore_unavailable>
1457  
1458 See the L<mpercolate docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html>
1459 for more information.
1460  
1461 =head2 C<suggest()>
1462  
1463 $results = $e->suggest(
1464 index => 'index' | \@indices, # optional
1465  
1466 body => { suggest request } # required
1467 );
1468  
1469 The C<suggest()> method is used to run
1470 L<did-you-mean|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesteres-phrase.html>
1471 or L<search-as-you-type|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html>
1472 suggestion requests, which can also be run as part of a L</search()> request.
1473  
1474 $results = $e->suggest(
1475 index => 'my_index',
1476 body => {
1477 my_suggestions => {
1478 phrase => {
1479 text => 'johnny walker',
1480 field => 'title'
1481 }
1482 }
1483 }
1484 );
1485  
1486 Query string parameters:
1487 C<allow_no_indices>,
1488 C<expand_wildcards>,
1489 C<ignore_unavailable>,
1490 C<preference>,
1491 C<routing>
1492  
1493 =head1 INDEXED SCRIPT METHODS
1494  
1495 If dynamic scripting is enabled, Elasticsearch allows you to store scripts in an internal index known as
1496 C<.scripts> and reference them by id. The methods to manage indexed scripts are as follows:
1497  
1498 =head2 C<put_script()>
1499  
1500 $result = $e->put_script(
1501 lang => 'lang', # required
1502 id => 'id', # required
1503 body => { script } # required
1504 );
1505  
1506 The C<put_script()> method is used to store a script in the C<.scripts> index. For instance:
1507  
1508 $result = $e->put_scripts(
1509 lang => 'groovy',
1510 id => 'hello_world',
1511 body => {
1512 script => q(return "hello world");
1513 }
1514 );
1515  
1516 Query string parameters:
1517 C<op_type>,
1518 C<version>,
1519 C<version_type>
1520  
1521 See the L<indexed scripts docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_indexed_scripts> for more.
1522  
1523 =head2 C<get_script()>
1524  
1525 $script = $e->get_script(
1526 lang => 'lang', # required
1527 id => 'id', # required
1528 );
1529  
1530 Retrieve the indexed script from the C<.scripts> index.
1531  
1532 Query string parameters:
1533 C<version>,
1534 C<version_type>
1535  
1536 See the L<indexed scripts docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_indexed_scripts> for more.
1537  
1538 =head2 C<delete_script()>
1539  
1540 $script = $e->delete_script(
1541 lang => 'lang', # required
1542 id => 'id', # required
1543 );
1544  
1545 Delete the indexed script from the C<.scripts> index.
1546  
1547 Query string parameters:
1548 C<version>,
1549 C<version_type>
1550  
1551 See the L<indexed scripts docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_indexed_scripts> for more.
1552  
1553 =head1 INDEXED SEARCH TEMPLATE METHODS
1554  
1555 Mustache templates can be used to create search requests. These templates can
1556 be stored in the C<.scripts> index and retrieved by ID. The methods to
1557 manage indexed scripts are as follows:
1558  
1559 =head2 C<put_template()>
1560  
1561 $result = $e->put_template(
1562 id => 'id', # required
1563 body => { template } || "template" # required
1564 );
1565  
1566 The C<put_template()> method is used to store a template in the C<.scripts> index.
1567 For instance:
1568  
1569 $result = $e->put_template(
1570 id => 'hello_world',
1571 body => {
1572 template => {
1573 query => {
1574 match => {
1575 title => "hello world"
1576 }
1577 }
1578 }
1579 }
1580 );
1581  
1582 Query string parameters: None
1583  
1584 See the L<indexed search template docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_pre_registered_template> for more.
1585  
1586 =head2 C<get_template()>
1587  
1588 $script = $e->get_template(
1589 id => 'id', # required
1590 );
1591  
1592 Retrieve the indexed template from the C<.scripts> index.
1593  
1594 Query string parameters:
1595 C<version>,
1596 C<version_type>
1597  
1598 See the L<indexed search template docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_pre_registered_template> for more.
1599  
1600 =head2 C<delete_template()>
1601  
1602 $script = $e->delete_template(
1603 id => 'id', # required
1604 );
1605  
1606 Delete the indexed template from the C<.scripts> index.
1607  
1608 Query string parameters: None
1609  
1610 See the L<indexed search template docs|http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_pre_registered_template> for more.
1611  
1612 =head1 AUTHOR
1613  
1614 Enrico Zimuel <enrico.zimuel@elastic.co>
1615  
1616 =head1 COPYRIGHT AND LICENSE
1617  
1618 This software is Copyright (c) 2020 by Elasticsearch BV.
1619  
1620 This is free software, licensed under:
1621  
1622 The Apache License, Version 2.0, January 2004
1623  
1624 =cut
1625  
1626 __END__