File Coverage

blib/lib/Search/Elasticsearch.pm
Criterion Covered Total %
statement 21 23 91.3
branch 1 2 50.0
condition 6 7 85.7
subroutine 4 4 100.0
pod 0 1 0.0
total 32 37 86.4


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V. under one or more contributor
2             # license agreements. See the NOTICE file distributed with
3             # this work for additional information regarding copyright
4             # ownership. Elasticsearch B.V. licenses this file to you under
5             # the Apache License, Version 2.0 (the "License"); you may
6             # not use this file except in compliance with the License.
7             # You may obtain a copy of the License at
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing,
12             # software distributed under the License is distributed on an
13             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14             # KIND, either express or implied. See the License for the
15             # specific language governing permissions and limitations
16             # under the License.
17              
18             package Search::Elasticsearch;
19              
20 67     67   12775381 use Moo 2.001000 ();
  67         597738  
  67         2517  
21              
22 67     67   34890 use Search::Elasticsearch::Util qw(parse_params load_plugin);
  67         329  
  67         699  
23 67     67   64401 use namespace::clean;
  67         1090193  
  67         573  
24              
25             our $VERSION = '8.12';
26              
27             my %Default_Plugins = (
28             client => [ 'Search::Elasticsearch::Client', '8_0::Direct' ],
29             cxn_factory => [ 'Search::Elasticsearch::Cxn::Factory', '' ],
30             cxn_pool => [ 'Search::Elasticsearch::CxnPool', 'Static' ],
31             logger => [ 'Search::Elasticsearch::Logger', 'LogAny' ],
32             serializer => [ 'Search::Elasticsearch::Serializer', 'JSON' ],
33             transport => [ 'Search::Elasticsearch::Transport', '' ],
34             );
35              
36             my @Load_Order = qw(
37             serializer
38             logger
39             cxn_factory
40             cxn_pool
41             transport
42             client
43             );
44              
45             #===================================
46             sub new {
47             #===================================
48 100     100 0 11913763 my ( $class, $params ) = parse_params(@_);
49              
50 100   100     722 $params->{cxn} ||= 'HTTPTiny';
51 100   50     664 my $plugins = delete $params->{plugins} || [];
52 100 50       468 $plugins = [$plugins] unless ref $plugins eq 'ARRAY';
53              
54 100         310 for my $name (@Load_Order) {
55 600         132908 my ( $base, $default ) = @{ $Default_Plugins{$name} };
  600         2678  
56 600   100     3296 my $sub_class = $params->{$name} || $default;
57 600         2475 my $plugin_class = load_plugin( $base, $sub_class );
58 600         22793 $params->{$name} = $plugin_class->new($params);
59             }
60              
61 100         132398 for my $name (@$plugins) {
62 0         0 my $plugin_class
63             = load_plugin( 'Search::Elasticsearch::Plugin', $name );
64 0         0 $plugin_class->_init_plugin($params);
65             }
66              
67 100         2331 return $params->{client};
68             }
69              
70             1;
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Search::Elasticsearch - The official client for Elasticsearch
79              
80             =head1 VERSION
81              
82             version 8.12
83              
84             =head1 SYNOPSIS
85              
86             use Search::Elasticsearch;
87              
88             # Connect to localhost:9200:
89              
90             my $e = Search::Elasticsearch->new();
91              
92             # Round-robin between two nodes:
93              
94             my $e = Search::Elasticsearch->new(
95             nodes => [
96             'search1:9200',
97             'search2:9200'
98             ]
99             );
100              
101             # Connect to cluster at search1:9200, sniff all nodes and round-robin between them:
102              
103             my $e = Search::Elasticsearch->new(
104             nodes => 'search1:9200',
105             cxn_pool => 'Sniff'
106             );
107              
108             # Index a document:
109              
110             $e->index(
111             index => 'my_app',
112             type => 'blog_post',
113             id => 1,
114             body => {
115             title => 'Elasticsearch clients',
116             content => 'Interesting content...',
117             date => '2013-09-24'
118             }
119             );
120              
121             # Get the document:
122              
123             my $doc = $e->get(
124             index => 'my_app',
125             type => 'blog_post',
126             id => 1
127             );
128              
129             # Search:
130              
131             my $results = $e->search(
132             index => 'my_app',
133             body => {
134             query => {
135             match => { title => 'elasticsearch' }
136             }
137             }
138             );
139              
140             # Cluster requests:
141              
142             $info = $e->cluster->info;
143             $health = $e->cluster->health;
144             $node_stats = $e->cluster->node_stats;
145              
146             # Index requests:
147              
148             $e->indices->create(index=>'my_index');
149             $e->indices->delete(index=>'my_index');
150              
151             =head1 DESCRIPTION
152              
153             L is the official Perl client for Elasticsearch,
154             supported by L. Elasticsearch
155             itself is a flexible and powerful open source, distributed real-time
156             search and analytics engine for the cloud. You can read more about it
157             on L.
158              
159             =head1 PREVIOUS VERSIONS OF ELASTICSEARCH
160              
161             This version of the client supports the Elasticsearch 7.0 branch,
162             which is not backwards compatible with earlier branches.
163              
164             If you need to talk to a version of Elasticsearch before 7.0.0, please
165             install one of the following packages:
166              
167             =over
168              
169             =item *
170              
171             L
172              
173             =item *
174              
175             L
176              
177             =item *
178              
179             L
180              
181             =item *
182              
183             L
184              
185             =item *
186              
187             L
188              
189             =back
190              
191             =head2 Motivation
192              
193             =over
194              
195             I
196              
197             Leonardo da Vinci
198              
199             =back
200              
201             All of us have opinions, especially when it comes to designing APIs.
202             Unfortunately, the opinions of programmers seldom coincide. The intention of
203             this client, and of the officially supported clients available for other
204             languages, is to provide robust support for the full native Elasticsearch API
205             with as few opinions as possible: you should be able to read the
206             L
207             and understand how to use this client, or any of the other official clients.
208              
209             Should you decide that you want to customize the API, then this client
210             provides the basis for your code. It does the hard stuff for you,
211             allowing you to build on top of it.
212              
213             =head2 Features
214              
215             This client provides:
216              
217             =over
218              
219             =item *
220              
221             Full support for all Elasticsearch APIs
222              
223             =item *
224              
225             HTTP backend (for an async backend using L, see
226             L)
227              
228             =item *
229              
230             Robust networking support which handles load balancing, failure detection
231             and failover
232              
233             =item *
234              
235             Good defaults
236              
237             =item *
238              
239             Helper utilities for more complex operations, such as
240             L, and
241             L
242              
243             =item *
244              
245             Logging support via L
246              
247             =item *
248              
249             Compatibility with the official clients for Python, Ruby, PHP, and Javascript
250              
251             =item *
252              
253             Easy extensibility
254              
255             =back
256              
257             =head1 INSTALLING ELASTICSEARCH
258              
259             You can download the latest version of Elasticsearch from
260             L. See the
261             L
262             for details. You will need to have a recent version of Java installed,
263             preferably the Java v8 from Sun.
264              
265             =head1 CREATING A NEW INSTANCE
266              
267             The L method returns a new L
268             which can be used to run requests against the Elasticsearch cluster.
269              
270             use Search::Elasticsearch;
271             my $e = Search::Elasticsearch->new( %params );
272              
273             The most important arguments to L are the following:
274              
275             =head2 C
276              
277             The C parameter tells the client which Elasticsearch nodes it should
278             talk to. It can be a single node, multiples nodes or, if not
279             specified, will default to C:
280              
281             # default: localhost:9200
282             $e = Search::Elasticsearch->new();
283              
284             # single
285             $e = Search::Elasticsearch->new( nodes => 'search_1:9200');
286              
287             # multiple
288             $e = Search::Elasticsearch->new(
289             nodes => [
290             'search_1:9200',
291             'search_2:9200'
292             ]
293             );
294              
295             Each C can be a URL including a scheme, host, port, path and userinfo
296             (for authentication). For instance, this would be a valid node:
297              
298             https://username:password@search.domain.com:443/prefix/path
299              
300             See L for more on node specification.
301              
302             =head2 C
303              
304             The L modules manage connections to
305             nodes in the Elasticsearch cluster. They handle the load balancing between
306             nodes and failover when nodes fail. Which C you should use depends on
307             where your cluster is. There are three choices:
308              
309             =over
310              
311             =item * C
312              
313             $e = Search::Elasticsearch->new(
314             cxn_pool => 'Static' # default
315             nodes => [
316             'search1.domain.com:9200',
317             'search2.domain.com:9200'
318             ],
319             );
320              
321             The L connection pool, which is the
322             default, should be used when you don't have direct access to the Elasticsearch
323             cluster, eg when you are accessing the cluster through a proxy. See
324             L for more.
325              
326             =item * C
327              
328             $e = Search::Elasticsearch->new(
329             cxn_pool => 'Sniff',
330             nodes => [
331             'search1:9200',
332             'search2:9200'
333             ],
334             );
335              
336             The L connection pool should be used
337             when you B have direct access to the Elasticsearch cluster, eg when
338             your web servers and Elasticsearch servers are on the same network.
339             The nodes that you specify are used to I the cluster, which is
340             then I to find the current list of live nodes that the cluster
341             knows about. See L.
342              
343             =item * C
344              
345             $e = Search::Elasticsearch->new(
346             cxn_pool => 'Static::NoPing'
347             nodes => [
348             'proxy1.domain.com:80',
349             'proxy2.domain.com:80'
350             ],
351             );
352              
353             The L connection
354             pool should be used when your access to a remote cluster is so limited
355             that you cannot ping individual nodes with a C request.
356              
357             See L for more.
358              
359             =back
360              
361             =head2 C
362              
363             For debugging purposes, it is useful to be able to dump the actual HTTP
364             requests which are sent to the cluster, and the response that is received.
365             This can be enabled with the C parameter, as follows:
366              
367             # To STDERR
368             $e = Search::Elasticsearch->new(
369             trace_to => 'Stderr'
370             );
371              
372             # To a file
373             $e = Search::Elasticsearch->new(
374             trace_to => ['File','/path/to/filename']
375             );
376              
377             Logging is handled by L. See L
378             for more information.
379              
380             =head2 Other
381              
382             Other arguments are explained in the respective L.
383              
384             =head1 RUNNING REQUESTS
385              
386             When you create a new instance of Search::Elasticsearch, it returns a
387             L object, which can be used for
388             running requests.
389              
390             use Search::Elasticsearch;
391             my $e = Search::Elasticsearch->new( %params );
392              
393             # create an index
394             $e->indices->create( index => 'my_index' );
395              
396             # index a document
397             $e->index(
398             index => 'my_index',
399             type => 'blog_post',
400             id => 1,
401             body => {
402             title => 'Elasticsearch clients',
403             content => 'Interesting content...',
404             date => '2013-09-24'
405             }
406             );
407              
408             See L for more details about the requests that
409             can be run.
410              
411             =head1 MODULES
412              
413             Each chunk of functionality is handled by a different module,
414             which can be specified in the call to L as shown in L above.
415             For instance, the following will use the L
416             module for the connection pool.
417              
418             $e = Search::Elasticsearch->new(
419             cxn_pool => 'Sniff'
420             );
421              
422             Custom modules can be named with the appropriate prefix,
423             eg C, or by prefixing the full class name
424             with C<+>:
425              
426             $e = Search::Elasticsearch->new(
427             cxn_pool => '+My::Custom::CxnClass'
428             );
429              
430             The modules that you can override are specified with the following
431             arguments to L:
432              
433             =head2 C
434              
435             The class to use for the client functionality, which provides
436             methods that can be called to execute requests, such as
437             C, C or C. The client parses the user's
438             requests and passes them to the L class to be executed.
439              
440             The default version of the client is C<7_0::Direct>, which can
441             be explicitly specified as follows:
442              
443             $e = Search::Elasticsearch->new(
444             client => '7_0::Direct'
445             );
446              
447             =head2 C
448              
449             The Transport class accepts a parsed request from the L class,
450             fetches a L from its L and tries to execute the request,
451             retrying after failure where appropriate. See:
452              
453             =over
454              
455             =item * L
456              
457             =back
458              
459             =head2 C
460              
461             The class which handles raw requests to Elasticsearch nodes.
462             See:
463              
464             =over
465              
466             =item * L (default)
467              
468             =item * L
469              
470             =item * L
471              
472             =back
473              
474             =head2 C
475              
476             The class which the L uses to create new L objects.
477             See:
478              
479             =over
480              
481             =item * L
482              
483             =back
484              
485             =head2 C (2)
486              
487             The class to use for the L functionality.
488             It calls the L class to create new L objects when
489             appropriate. See:
490              
491             =over
492              
493             =item * L (default)
494              
495             =item * L
496              
497             =item * L
498              
499             =back
500              
501             =head2 C
502              
503             The class to use for logging events and tracing HTTP requests/responses. See:
504              
505             =over
506              
507             =item * L
508              
509             =back
510              
511             =head2 C
512              
513             The class to use for serializing request bodies and deserializing response
514             bodies. See:
515              
516             =over
517              
518             =item * L (default)
519              
520             =item * L
521              
522             =item * L
523              
524             =item * L
525              
526             =back
527              
528             =head1 BUGS
529              
530             This is a stable API but this implementation is new. Watch this space
531             for new releases.
532              
533             If you have any suggestions for improvements, or find any bugs, please report
534             them to L.
535             I will be notified, and then you'll automatically be notified of progress on
536             your bug as I make changes.
537              
538             =head1 SUPPORT
539              
540             You can find documentation for this module with the perldoc command.
541              
542             perldoc Search::Elasticsearch
543              
544             You can also look for information at:
545              
546             =over 4
547              
548             =item * GitHub
549              
550             L
551              
552             =item * CPAN Ratings
553              
554             L
555              
556             =item * Search MetaCPAN
557              
558             L
559              
560             =item * IRC
561              
562             The L<#elasticsearch|irc://irc.freenode.net/elasticsearch> channel on
563             C.
564              
565             =item * Mailing list
566              
567             The main L.
568              
569             =back
570              
571             =head1 TEST SUITE
572              
573             The full test suite requires a live Elasticsearch node to run, and should
574             be run as :
575              
576             perl Makefile.PL
577             ES=localhost:9200 make test
578              
579             B
580             DATA YOU WANT TO KEEP!>
581              
582             You can change the Cxn class which is used by setting the C
583             environment variable:
584              
585             ES_CXN=NetCurl ES=localhost:9200 make test
586              
587             =head1 AUTHOR
588              
589             Enrico Zimuel
590              
591             =head1 COPYRIGHT AND LICENSE
592              
593             This software is Copyright (c) 2024 by Elasticsearch BV.
594              
595             This is free software, licensed under:
596              
597             The Apache License, Version 2.0, January 2004
598              
599             =cut
600              
601             __END__