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 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 ▶use Moo 2.001000 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 67 ▶use Search::Elasticsearch::Util qw(parse_params load_plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 67 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 67 our $VERSION = '8.12';
26  
27 67 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 67 my @Load_Order = qw(
37 serializer
38 logger
39 cxn_factory
40 cxn_pool
41 transport
42 client
43 );
44  
45 #===================================
46 100 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod undocumented
47 #===================================
48 100 my ( $class, $params ) = parse_params(@_);
49  
50 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
51 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
52 100 ▶ $plugins = [$plugins] unless ref $plugins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $plugins eq "ARRAY" 0 100 100
53  
54 100 for my $name (@Load_Order) {
55 600 my ( $base, $default ) = @{ $Default_Plugins{$name} };
56 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
57 600 my $plugin_class = load_plugin( $base, $sub_class );
58 600 $params->{$name} = $plugin_class->new($params);
59 }
60  
61 100 for my $name (@$plugins) {
62 0 my $plugin_class
63 = load_plugin( 'Search::Elasticsearch::Plugin', $name );
64 0 $plugin_class->_init_plugin($params);
65 }
66  
67 100 return $params->{client};
68 }
69  
70 67 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<Search::Elasticsearch> is the official Perl client for Elasticsearch,
154 supported by L<elastic.co|http://elastic.co>. 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<elastic.co|http://www.elastic.co>.
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<Search::Elasticsearch::Client::6_0>
172  
173 =item *
174  
175 L<Search::Elasticsearch::Client::5_0>
176  
177 =item *
178  
179 L<Search::Elasticsearch::Client::2_0>
180  
181 =item *
182  
183 L<Search::Elasticsearch::Client::1_0>
184  
185 =item *
186  
187 L<Search::Elasticsearch::Client::0_90>
188  
189 =back
190  
191 =head2 Motivation
192  
193 =over
194  
195 I<The greatest deception men suffer is from their own opinions.>
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<Elasticsearch reference documentation|http://www.elastic.co/guide>
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<Promises>, see
226 L<Search::Elasticsearch::Async>)
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<bulk indexing|Search::Elasticsearch::Client::7_0::Bulk>, and
241 L<scrolled searches|Search::Elasticsearch::Client::7_0::Scroll>
242  
243 =item *
244  
245 Logging support via L<Log::Any>
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<http://www.elastic.co/download>. See the
261 L<installation instructions|https://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html>
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</new()> method returns a new L<client|Search::Elasticsearch::Client::6_0::Direct>
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</new()> are the following:
274  
275 =head2 C<nodes>
276  
277 The C<nodes> 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<localhost:9200>:
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<node> 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<Search::Elasticsearch::Role::Cxn/node> for more on node specification.
301  
302 =head2 C<cxn_pool>
303  
304 The L<CxnPool|Search::Elasticsearch::Role::CxnPool> 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<CxnPool> you should use depends on
307 where your cluster is. There are three choices:
308  
309 =over
310  
311 =item * C<Static>
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<Static|Search::Elasticsearch::CxnPool::Static> 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<Search::Elasticsearch::CxnPool::Static> for more.
325  
326 =item * C<Sniff>
327  
328 $e = Search::Elasticsearch->new(
329 cxn_pool => 'Sniff',
330 nodes => [
331 'search1:9200',
332 'search2:9200'
333 ],
334 );
335  
336 The L<Sniff|Search::Elasticsearch::CxnPool::Sniff> connection pool should be used
337 when you B<do> 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<discover> the cluster, which is
340 then I<sniffed> to find the current list of live nodes that the cluster
341 knows about. See L<Search::Elasticsearch::CxnPool::Sniff>.
342  
343 =item * C<Static::NoPing>
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<Static::NoPing|Search::Elasticsearch::CxnPool::Static::NoPing> 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<HEAD /> request.
356  
357 See L<Search::Elasticsearch::CxnPool::Static::NoPing> for more.
358  
359 =back
360  
361 =head2 C<trace_to>
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<trace_to> 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<Log::Any>. See L<Search::Elasticsearch::Logger::LogAny>
378 for more information.
379  
380 =head2 Other
381  
382 Other arguments are explained in the respective L<module docs|/MODULES>.
383  
384 =head1 RUNNING REQUESTS
385  
386 When you create a new instance of Search::Elasticsearch, it returns a
387 L<client|Search::Elasticsearch::Client::6_0::Direct> 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<Search::Elasticsearch::Client::6_0::Direct> 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<new()> as shown in L<cxn_pool> above.
415 For instance, the following will use the L<Search::Elasticsearch::CxnPool::Sniff>
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<Search::Elasticsearch::CxnPool::>, 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</new()>:
432  
433 =head2 C<client>
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<search()>, C<index()> or C<delete()>. The client parses the user's
438 requests and passes them to the L</transport> 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<transport>
448  
449 The Transport class accepts a parsed request from the L</client> class,
450 fetches a L</cxn> from its L</cxn_pool> and tries to execute the request,
451 retrying after failure where appropriate. See:
452  
453 =over
454  
455 =item * L<Search::Elasticsearch::Transport>
456  
457 =back
458  
459 =head2 C<cxn>
460  
461 The class which handles raw requests to Elasticsearch nodes.
462 See:
463  
464 =over
465  
466 =item * L<Search::Elasticsearch::Cxn::HTTPTiny> (default)
467  
468 =item * L<Search::Elasticsearch::Cxn::LWP>
469  
470 =item * L<Search::Elasticsearch::Cxn::NetCurl>
471  
472 =back
473  
474 =head2 C<cxn_factory>
475  
476 The class which the L</cxn_pool> uses to create new L</cxn> objects.
477 See:
478  
479 =over
480  
481 =item * L<Search::Elasticsearch::Cxn::Factory>
482  
483 =back
484  
485 =head2 C<cxn_pool> (2)
486  
487 The class to use for the L<connection pool|/cxn_pool> functionality.
488 It calls the L</cxn_factory> class to create new L</cxn> objects when
489 appropriate. See:
490  
491 =over
492  
493 =item * L<Search::Elasticsearch::CxnPool::Static> (default)
494  
495 =item * L<Search::Elasticsearch::CxnPool::Sniff>
496  
497 =item * L<Search::Elasticsearch::CxnPool::Static::NoPing>
498  
499 =back
500  
501 =head2 C<logger>
502  
503 The class to use for logging events and tracing HTTP requests/responses. See:
504  
505 =over
506  
507 =item * L<Search::Elasticsearch::Logger::LogAny>
508  
509 =back
510  
511 =head2 C<serializer>
512  
513 The class to use for serializing request bodies and deserializing response
514 bodies. See:
515  
516 =over
517  
518 =item * L<Search::Elasticsearch::Serializer::JSON> (default)
519  
520 =item * L<Search::Elasticsearch::Serializer::JSON::Cpanel>
521  
522 =item * L<Search::Elasticsearch::Serializer::JSON::XS>
523  
524 =item * L<Search::Elasticsearch::Serializer::JSON::PP>
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<http://github.com/elasticsearch/elasticsearch-perl/issues>.
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<http://github.com/elasticsearch/elasticsearch-perl>
551  
552 =item * CPAN Ratings
553  
554 L<http://cpanratings.perl.org/d/Search::Elasticsearch>
555  
556 =item * Search MetaCPAN
557  
558 L<https://metacpan.org/module/Search::Elasticsearch>
559  
560 =item * IRC
561  
562 The L<#elasticsearch|irc://irc.freenode.net/elasticsearch> channel on
563 C<irc.freenode.net>.
564  
565 =item * Mailing list
566  
567 The main L<Elasticsearch mailing list|http://discuss.elastic.co>.
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<TESTS RUN IN THIS WAY ARE DESTRUCTIVE! DO NOT RUN AGAINST A CLUSTER WITH
580 DATA YOU WANT TO KEEP!>
581  
582 You can change the Cxn class which is used by setting the C<ES_CXN>
583 environment variable:
584  
585 ES_CXN=NetCurl ES=localhost:9200 make test
586  
587 =head1 AUTHOR
588  
589 Enrico Zimuel <enrico.zimuel@elastic.co>
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__
« Elasticsearch/CxnPool/Static.pm ↑ Summary Elasticsearch/CxnPool/Sniff.pm »