line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::ElasticSearch; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Dancer2::Plugin::ElasticSearch::VERSION = '0.002'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Dancer2 plugin for obtaining Search::Elasticsearch handles |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
528265
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
9
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
34
|
use 5.012; |
|
1
|
|
|
|
|
8
|
|
11
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
62
|
|
12
|
1
|
|
|
1
|
|
5
|
use autodie; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
13
|
1
|
|
|
1
|
|
4456
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
1802
|
use Search::Elasticsearch; |
|
1
|
|
|
|
|
41993
|
|
|
1
|
|
|
|
|
33
|
|
16
|
1
|
|
|
1
|
|
10
|
use Try::Tiny; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
81
|
|
17
|
1
|
|
|
1
|
|
754
|
use Dancer2::Plugin qw/:no_dsl/; |
|
1
|
|
|
|
|
2408
|
|
|
1
|
|
|
|
|
6
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $handles = {}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
register 'elastic' => sub { |
22
|
1
|
|
|
1
|
|
81534
|
my ($dsl, $name) = @_; |
23
|
1
|
|
50
|
|
|
9
|
$name //= 'default'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# the classic fork/thread-safety mantra |
26
|
1
|
50
|
|
|
|
7
|
my $pid_tid = $$ . ($INC{'threads.pm'} ? '_' . threads->tid : ''); |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
2
|
my $elastic; |
29
|
1
|
50
|
|
|
|
6
|
if ($elastic = $handles->{$pid_tid}{$name}) { |
30
|
|
|
|
|
|
|
# got one from the cache. done |
31
|
|
|
|
|
|
|
} else { |
32
|
|
|
|
|
|
|
# no handle in the cache, create one and stash it |
33
|
1
|
|
|
|
|
5
|
my $plugin_config = plugin_setting(); |
34
|
1
|
50
|
|
|
|
107
|
unless (exists $plugin_config->{$name}) { |
35
|
0
|
|
|
|
|
0
|
die "No config for ElasticSearch client '$name'"; |
36
|
|
|
|
|
|
|
} |
37
|
1
|
|
|
|
|
4
|
my $config = $plugin_config->{$name}; |
38
|
1
|
|
50
|
|
|
4
|
my $params = $config->{params} // {}; |
39
|
|
|
|
|
|
|
try { |
40
|
1
|
|
|
1
|
|
38
|
$elastic = Search::Elasticsearch->new(%{$params}); |
|
1
|
|
|
|
|
11
|
|
41
|
|
|
|
|
|
|
# S::E does not actually connect until it needs to, but |
42
|
|
|
|
|
|
|
# we're already not creating the S::E object until we need |
43
|
|
|
|
|
|
|
# one! |
44
|
1
|
|
|
|
|
471457
|
$elastic->ping; |
45
|
|
|
|
|
|
|
} catch { |
46
|
1
|
|
|
1
|
|
8793
|
my $error = $_; |
47
|
1
|
|
|
|
|
9
|
die "Could not connect to ElasticSearch: $error"; |
48
|
1
|
|
|
|
|
9
|
}; |
49
|
0
|
|
|
|
|
|
$handles->{$pid_tid}{$name} = $elastic; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return $elastic; |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
register_plugin; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Dancer2::Plugin::ElasticSearch - Dancer2 plugin for obtaining Search::Elasticsearch handles |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 VERSION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
version 0.002 |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 SYNOPSIS |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
use Dancer2::Plugin::ElasticSearch; |
73
|
|
|
|
|
|
|
get '/count_all_docs' => sub { |
74
|
|
|
|
|
|
|
return elastic->search(search_type => 'count', |
75
|
|
|
|
|
|
|
body => { query => { match_all => {} } }); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 DESCRIPTION |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
This Dancer2 plugin handles connection configuration and |
81
|
|
|
|
|
|
|
fork-and-thread safety for ElasticSearch connectors. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 KEYWORDS |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 elastic |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
elastic->ping; |
88
|
|
|
|
|
|
|
elastic('other')->ping; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Return a L subclass suitable for |
91
|
|
|
|
|
|
|
running queries against an ElasticSearch instance. Each thread is |
92
|
|
|
|
|
|
|
guaranteed to have its own client instances. If a connection already |
93
|
|
|
|
|
|
|
exists for a given configuration name, it is returned instead of being |
94
|
|
|
|
|
|
|
re-created. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
If a configuration name is not passed, "default" is assumed. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 CONFIGURATION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
plugins: |
101
|
|
|
|
|
|
|
ElasticSearch: |
102
|
|
|
|
|
|
|
default: |
103
|
|
|
|
|
|
|
params: |
104
|
|
|
|
|
|
|
nodes: localhost:9200 |
105
|
|
|
|
|
|
|
other: |
106
|
|
|
|
|
|
|
params: etc |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
The C hashref must contain a map of parameters that can be |
109
|
|
|
|
|
|
|
passed directly to the L constructor. In the |
110
|
|
|
|
|
|
|
above example, calling C (or C) will |
111
|
|
|
|
|
|
|
result in |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Search::Elasticsearch->new(nodes => 'localhost:9200'); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 INSTALLATION |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
You can install this module as you would any Perl module. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
During installation, the unit tests will run queries against a local |
120
|
|
|
|
|
|
|
ElasticSearch instance if there is one (read-only queries, of |
121
|
|
|
|
|
|
|
course!). If you have no local ElasticSearch instance, but still wish |
122
|
|
|
|
|
|
|
to run the unit tests, set the C variable to |
123
|
|
|
|
|
|
|
"$host:$port". If no instance can be reached, the tests will be |
124
|
|
|
|
|
|
|
safely skipped. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SEE ALSO |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L, L |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Fabrice Gabolde |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This software is copyright (c) 2015 by Weborama. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
139
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |