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   3436122 use Moo 2.001000 ();
  67         668326  
  67         1771  
21              
22 67     67   24548 use Search::Elasticsearch::Util qw(parse_params load_plugin);
  67         197  
  67         446  
23 67     67   48392 use namespace::clean;
  67         669993  
  67         425  
24              
25             our $VERSION = '8.00';
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 90627 my ( $class, $params ) = parse_params(@_);
49              
50 100   100     578 $params->{cxn} ||= 'HTTPTiny';
51 100   50     492 my $plugins = delete $params->{plugins} || [];
52 100 50       382 $plugins = [$plugins] unless ref $plugins eq 'ARRAY';
53              
54 100         285 for my $name (@Load_Order) {
55 600         106088 my ( $base, $default ) = @{ $Default_Plugins{$name} };
  600         1917  
56 600   100     2509 my $sub_class = $params->{$name} || $default;
57 600         1942 my $plugin_class = load_plugin( $base, $sub_class );
58 600         16125 $params->{$name} = $plugin_class->new($params);
59             }
60              
61 100         111693 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         1787 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.00
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             =back
182              
183             =head2 Motivation
184              
185             =over
186              
187             I
188              
189             Leonardo da Vinci
190              
191             =back
192              
193             All of us have opinions, especially when it comes to designing APIs.
194             Unfortunately, the opinions of programmers seldom coincide. The intention of
195             this client, and of the officially supported clients available for other
196             languages, is to provide robust support for the full native Elasticsearch API
197             with as few opinions as possible: you should be able to read the
198             L
199             and understand how to use this client, or any of the other official clients.
200              
201             Should you decide that you want to customize the API, then this client
202             provides the basis for your code. It does the hard stuff for you,
203             allowing you to build on top of it.
204              
205             =head2 Features
206              
207             This client provides:
208              
209             =over
210              
211             =item *
212              
213             Full support for all Elasticsearch APIs
214              
215             =item *
216              
217             HTTP backend (for an async backend using L, see
218             L)
219              
220             =item *
221              
222             Robust networking support which handles load balancing, failure detection
223             and failover
224              
225             =item *
226              
227             Good defaults
228              
229             =item *
230              
231             Helper utilities for more complex operations, such as
232             L, and
233             L
234              
235             =item *
236              
237             Logging support via L
238              
239             =item *
240              
241             Compatibility with the official clients for Python, Ruby, PHP, and Javascript
242              
243             =item *
244              
245             Easy extensibility
246              
247             =back
248              
249             =head1 INSTALLING ELASTICSEARCH
250              
251             You can download the latest version of Elasticsearch from
252             L. See the
253             L
254             for details. You will need to have a recent version of Java installed,
255             preferably the Java v8 from Sun.
256              
257             =head1 CREATING A NEW INSTANCE
258              
259             The L method returns a new L
260             which can be used to run requests against the Elasticsearch cluster.
261              
262             use Search::Elasticsearch;
263             my $e = Search::Elasticsearch->new( %params );
264              
265             The most important arguments to L are the following:
266              
267             =head2 C
268              
269             The C parameter tells the client which Elasticsearch nodes it should
270             talk to. It can be a single node, multiples nodes or, if not
271             specified, will default to C:
272              
273             # default: localhost:9200
274             $e = Search::Elasticsearch->new();
275              
276             # single
277             $e = Search::Elasticsearch->new( nodes => 'search_1:9200');
278              
279             # multiple
280             $e = Search::Elasticsearch->new(
281             nodes => [
282             'search_1:9200',
283             'search_2:9200'
284             ]
285             );
286              
287             Each C can be a URL including a scheme, host, port, path and userinfo
288             (for authentication). For instance, this would be a valid node:
289              
290             https://username:password@search.domain.com:443/prefix/path
291              
292             See L for more on node specification.
293              
294             =head2 C
295              
296             The L modules manage connections to
297             nodes in the Elasticsearch cluster. They handle the load balancing between
298             nodes and failover when nodes fail. Which C you should use depends on
299             where your cluster is. There are three choices:
300              
301             =over
302              
303             =item * C
304              
305             $e = Search::Elasticsearch->new(
306             cxn_pool => 'Static' # default
307             nodes => [
308             'search1.domain.com:9200',
309             'search2.domain.com:9200'
310             ],
311             );
312              
313             The L connection pool, which is the
314             default, should be used when you don't have direct access to the Elasticsearch
315             cluster, eg when you are accessing the cluster through a proxy. See
316             L for more.
317              
318             =item * C
319              
320             $e = Search::Elasticsearch->new(
321             cxn_pool => 'Sniff',
322             nodes => [
323             'search1:9200',
324             'search2:9200'
325             ],
326             );
327              
328             The L connection pool should be used
329             when you B have direct access to the Elasticsearch cluster, eg when
330             your web servers and Elasticsearch servers are on the same network.
331             The nodes that you specify are used to I the cluster, which is
332             then I to find the current list of live nodes that the cluster
333             knows about. See L.
334              
335             =item * C
336              
337             $e = Search::Elasticsearch->new(
338             cxn_pool => 'Static::NoPing'
339             nodes => [
340             'proxy1.domain.com:80',
341             'proxy2.domain.com:80'
342             ],
343             );
344              
345             The L connection
346             pool should be used when your access to a remote cluster is so limited
347             that you cannot ping individual nodes with a C request.
348              
349             See L for more.
350              
351             =back
352              
353             =head2 C
354              
355             For debugging purposes, it is useful to be able to dump the actual HTTP
356             requests which are sent to the cluster, and the response that is received.
357             This can be enabled with the C parameter, as follows:
358              
359             # To STDERR
360             $e = Search::Elasticsearch->new(
361             trace_to => 'Stderr'
362             );
363              
364             # To a file
365             $e = Search::Elasticsearch->new(
366             trace_to => ['File','/path/to/filename']
367             );
368              
369             Logging is handled by L. See L
370             for more information.
371              
372             =head2 Other
373              
374             Other arguments are explained in the respective L.
375              
376             =head1 RUNNING REQUESTS
377              
378             When you create a new instance of Search::Elasticsearch, it returns a
379             L object, which can be used for
380             running requests.
381              
382             use Search::Elasticsearch;
383             my $e = Search::Elasticsearch->new( %params );
384              
385             # create an index
386             $e->indices->create( index => 'my_index' );
387              
388             # index a document
389             $e->index(
390             index => 'my_index',
391             type => 'blog_post',
392             id => 1,
393             body => {
394             title => 'Elasticsearch clients',
395             content => 'Interesting content...',
396             date => '2013-09-24'
397             }
398             );
399              
400             See L for more details about the requests that
401             can be run.
402              
403             =head1 MODULES
404              
405             Each chunk of functionality is handled by a different module,
406             which can be specified in the call to L as shown in L above.
407             For instance, the following will use the L
408             module for the connection pool.
409              
410             $e = Search::Elasticsearch->new(
411             cxn_pool => 'Sniff'
412             );
413              
414             Custom modules can be named with the appropriate prefix,
415             eg C, or by prefixing the full class name
416             with C<+>:
417              
418             $e = Search::Elasticsearch->new(
419             cxn_pool => '+My::Custom::CxnClass'
420             );
421              
422             The modules that you can override are specified with the following
423             arguments to L:
424              
425             =head2 C
426              
427             The class to use for the client functionality, which provides
428             methods that can be called to execute requests, such as
429             C, C or C. The client parses the user's
430             requests and passes them to the L class to be executed.
431              
432             The default version of the client is C<7_0::Direct>, which can
433             be explicitly specified as follows:
434              
435             $e = Search::Elasticsearch->new(
436             client => '7_0::Direct'
437             );
438              
439             =head2 C
440              
441             The Transport class accepts a parsed request from the L class,
442             fetches a L from its L and tries to execute the request,
443             retrying after failure where appropriate. See:
444              
445             =over
446              
447             =item * L
448              
449             =back
450              
451             =head2 C
452              
453             The class which handles raw requests to Elasticsearch nodes.
454             See:
455              
456             =over
457              
458             =item * L (default)
459              
460             =item * L
461              
462             =item * L
463              
464             =back
465              
466             =head2 C
467              
468             The class which the L uses to create new L objects.
469             See:
470              
471             =over
472              
473             =item * L
474              
475             =back
476              
477             =head2 C (2)
478              
479             The class to use for the L functionality.
480             It calls the L class to create new L objects when
481             appropriate. See:
482              
483             =over
484              
485             =item * L (default)
486              
487             =item * L
488              
489             =item * L
490              
491             =back
492              
493             =head2 C
494              
495             The class to use for logging events and tracing HTTP requests/responses. See:
496              
497             =over
498              
499             =item * L
500              
501             =back
502              
503             =head2 C
504              
505             The class to use for serializing request bodies and deserializing response
506             bodies. See:
507              
508             =over
509              
510             =item * L (default)
511              
512             =item * L
513              
514             =item * L
515              
516             =item * L
517              
518             =back
519              
520             =head1 BUGS
521              
522             This is a stable API but this implementation is new. Watch this space
523             for new releases.
524              
525             If you have any suggestions for improvements, or find any bugs, please report
526             them to L.
527             I will be notified, and then you'll automatically be notified of progress on
528             your bug as I make changes.
529              
530             =head1 SUPPORT
531              
532             You can find documentation for this module with the perldoc command.
533              
534             perldoc Search::Elasticsearch
535              
536             You can also look for information at:
537              
538             =over 4
539              
540             =item * GitHub
541              
542             L
543              
544             =item * CPAN Ratings
545              
546             L
547              
548             =item * Search MetaCPAN
549              
550             L
551              
552             =item * IRC
553              
554             The L<#elasticsearch|irc://irc.freenode.net/elasticsearch> channel on
555             C.
556              
557             =item * Mailing list
558              
559             The main L.
560              
561             =back
562              
563             =head1 TEST SUITE
564              
565             The full test suite requires a live Elasticsearch node to run, and should
566             be run as :
567              
568             perl Makefile.PL
569             ES=localhost:9200 make test
570              
571             B
572             DATA YOU WANT TO KEEP!>
573              
574             You can change the Cxn class which is used by setting the C
575             environment variable:
576              
577             ES_CXN=NetCurl ES=localhost:9200 make test
578              
579             =head1 AUTHOR
580              
581             Enrico Zimuel
582              
583             =head1 COPYRIGHT AND LICENSE
584              
585             This software is Copyright (c) 2022 by Elasticsearch BV.
586              
587             This is free software, licensed under:
588              
589             The Apache License, Version 2.0, January 2004
590              
591             =cut
592              
593             __END__