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