Elasticsearch.pm

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