1_0/Direct.pm untested

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