Model/Role/Index.pm

filter: Statementstmt 26.7% 19 / 71 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 27.7% 5 / 18 Podpod 100% 12 / 12 totaltotal 30.5% 36 / 118 CC 9 SCAR 19.7
CC
9
Cov
23%
index_config
20.0
update_mapping
12.0
delete_mapping
12.0
CRAP 7.2
« Model/Results/Cached.pm ↑ Summary Model/Domain.pm »
1 package Elastic::Model::Role::Index;
2 23 $Elastic::Model::Role::Index::VERSION = '0.52';
3 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use MooseX::Types::Moose qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #===================================
10 23 has 'name' => (
11 #===================================
12 is => 'ro',
13 isa => Str,
14 required => 1,
15 );
16  
17 #===================================
18 23 has 'namespace' => (
19 #===================================
20 is => 'ro',
21 isa => 'Elastic::Model::Namespace',
22 handles => [ 'model', 'mappings' ]
23 );
24  
25 23 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #===================================
28 0 ▶sub index_config {
Subroutine index_config: not called CC=4 SCAR=30.0
Pod documented
29 #===================================
30 0 my ( $self, %args ) = @_;
31 0 ▶ my %settings = %{ $args{settings} || {} };
Condition0%0/2
$args{'settings'} || {}
l!l
00
MC/DC0%0/1
$args{'settings'} || {}
$args{'settings'}
Truth table0%0/2
A
A= $args{'settings'}
Aresult
11
00
32  
33 0 ▶ my @types = @{ $args{types} || [] };
Condition0%0/2
$args{'types'} || []
l!l
00
MC/DC0%0/1
$args{'types'} || []
$args{'types'}
Truth table0%0/2
A
A= $args{'types'}
Aresult
11
00
34 0 my $mappings = $self->mappings(@types);
35 0 my $meta = Class::MOP::class_of( $self->model );
36 0 ▶ if ( my $analysis = $meta->analysis_for_mappings($mappings) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $analysis = $meta->analysis_for_mappings($mappings)) 0 0 0
37 0 $settings{analysis} = $analysis;
38 }
39  
40 return {
41 0 index => $self->name,
42 settings => \%settings,
43 mappings => $mappings
44 };
45 }
46  
47 #===================================
48 0 ▶sub delete { shift->_index_action( 'delete_index', @_ ) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
49 0 ▶sub refresh { shift->_index_action( 'refresh_index', @_ ) }
Subroutine refresh: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub open { shift->_index_action( 'open_index', @_ ) }
Subroutine open: not called CC=1 SCAR=6.9
Pod documented
51 0 ▶sub close { shift->_index_action( 'close_index', @_ ) }
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
52 0 ▶sub exists { !!$_[0]->model->store->index_exists( index => $_[0]->name ) }
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
53 #===================================
54  
55 #===================================
56 0 ▶sub _index_action {
Subroutine _index_action: not called CC=1 SCAR=6.9
57 #===================================
58 0 my $self = shift;
59 0 my $action = shift;
60 0 my %args = @_;
61 0 $self->model->store->$action( %args, index => $self->name );
62 0 return $self;
63 }
64  
65 #===================================
66 0 ▶sub update_settings {
Subroutine update_settings: not called CC=1 SCAR=6.9
Pod documented
67 #===================================
68 0 my $self = shift;
69 0 $self->model->store->update_index_settings(
70 index => $self->name,
71 settings => {@_}
72 );
73 0 return $self;
74 }
75  
76 #===================================
77 0 ▶sub update_analyzers {
Subroutine update_analyzers: not called CC=1 SCAR=6.9
Pod documented
78 #===================================
79 0 my $self = shift;
80 0 my $params = $self->index_config(@_);
81 0 delete $params->{mappings};
82 0 $self->model->store->update_index_settings(%$params);
83 0 return $self;
84 }
85  
86 #===================================
87 0 ▶sub is_alias {
Subroutine is_alias: not called CC=2 SCAR=17.9
Pod documented
88 #===================================
89 0 my $self = shift;
90 0 my $name = $self->name;
91 0 my $indices = $self->model->store->get_aliases( index => $name );
92 0 ▶ return !!( %$indices && !$indices->{$name} );
Condition0%0/3
%$indices && !$indices->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
%$indices && !$indices->{$name}
%$indices !$indices->{$name}
Truth table0%0/3
A && not(B)
A= %$indicesB= !$indices->{$name}
ABresult
0X0
100
111
93 }
94  
95 #===================================
96 0 ▶sub is_index {
Subroutine is_index: not called CC=1 SCAR=6.9
Pod documented
97 #===================================
98 0 my $self = shift;
99 0 my $name = $self->name;
100 0 my $indices = $self->model->store->get_aliases( index => $name );
101 0 return !!$indices->{$name};
102 }
103  
104 #===================================
105 0 ▶sub update_mapping {
Subroutine update_mapping: not called CC=3 SCAR=24.8
Pod documented
106 #===================================
107 0 my $self = shift;
108 0 ▶ my %args = ref $_[-1] eq 'HASH' ? %{ pop() } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
109 0 my $mappings = $self->mappings(@_);
110 0 my $store = $self->model->store;
111 0 my $name = $self->name;
112 0 for my $type ( keys %$mappings ) {
113 $store->put_mapping(
114 index => $name,
115 type => $type,
116 0 mapping => $mappings->{$type},
117 %args,
118 );
119 }
120 0 return $self;
121 }
122  
123 #===================================
124 0 ▶sub delete_mapping {
Subroutine delete_mapping: not called CC=3 SCAR=24.8
Pod documented
125 #===================================
126 0 my $self = shift;
127 0 ▶ my %args = ref $_[-1] eq 'HASH' ? %{ pop() } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
128 0 my $store = $self->model->store;
129 0 my $name = $self->name;
130 0 $store->delete_mapping( index => $name, type => $_, %args ) for @_;
131 0 return $self;
132 }
133  
134 23 1;
135  
136 =pod
137  
138 =encoding UTF-8
139  
140 =head1 NAME
141  
142 Elastic::Model::Role::Index - Provides admin methods common to indices and aliases
143  
144 =head1 VERSION
145  
146 version 0.52
147  
148 =head1 SYNOPSIS
149  
150 $admin->close();
151 $admin->open();
152 $admin->delete();
153 $admin->refresh();
154  
155 $admin->update_mapping(@types);
156 $admin->delete_mapping(@types);
157  
158 $admin->update_analyzers();
159 $admin->update_settings(%settings);
160  
161 $bool = $admin->is_alias;
162 $bool = $admin->is_index;
163 $bool = $admin->exists;
164  
165 =head1 DESCRIPTION
166  
167 L<Elastic::Model::Role::Index> is a role which provides admin methods
168 common to indices and aliases. It is consumed by L<Elastic::Model::Index>
169 and L<Elastic::Model::Alias>.
170  
171 See L<Elastic::Manual::Scaling> for more about how domains, indices and aliases
172 relate to each other.
173  
174 =head1 ATTRIBUTES
175  
176 =head2 name
177  
178 $name = $admin->name;
179  
180 The name of the index or alias to be administered. This defaults to the
181 L<name|Elastic::Model::Namespace/name>
182 of the L</namespace> but can be overridden when creating a new
183 L<Elastic::Model::Index> or L<Elastic::Model::Alias> object, eg:
184  
185 $index = $namesapace->index('index_name')
186  
187 =head2 namespace
188  
189 The L<Elastic::Model::Namespace> object used to create the
190 L<Elastic::Model::Index> or L<Elastic::Model::Alias> object.
191  
192 =head2 es
193  
194 The same L<Search::Elasticsearch> connection as L<Elastic::Model::Role::Model/es>.
195  
196 =head1 METHODS
197  
198 =head2 delete()
199  
200 $admin = $admin->delete();
201 $admin = $admin->delete( %args );
202  
203 Deletes the index (or indices pointed to by alias ) L</name>. Any
204 C<%args> are passed directly to L<Search::Elasticsearch::Client::Direct::Indices/delete()>.
205 For example:
206  
207 $admin->delete( ignore => 404 );
208  
209 =head2 refresh()
210  
211 $admin = $admin->refresh();
212  
213 Forces the the index (or indices pointed to by alias ) L</name> to be refreshed,
214 ie all changes to the docs in the index become visible to search. By default,
215 indices are refreshed once every second anyway. You shouldn't abuse this option
216 as it will have a performance impact.
217  
218 =head2 open()
219  
220 $admin = $admin->open();
221  
222 Opens the index (or the SINGLE index pointed to by alias ) L</name>.
223  
224 =head2 close()
225  
226 $admin = $admin->close();
227  
228 Closes the index (or the SINGLE index pointed to by alias ) L</name>.
229  
230 =head2 index_config()
231  
232 $config = $admin->index_config( settings=> \%settings, types=> \@types );
233  
234 Returns a hashref containing the index/alias L</name>, the settings, and the
235 mappings for the current namespace. The generated analysis settings are merged
236 into any C<%settings> that you provide. Mappings and analysis settings will be
237 for all C<@types> known to the L</namespace> unless specified.
238  
239 This method is used by L</update_analyzers()> and
240 L<Elastic::Model::Index/create_index()>.
241  
242 =head2 update_settings()
243  
244 $admin = $admin->update_settings( %settings );
245  
246 Updates the L<index settings|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html>
247 for the the index (or indices pointed to by alias ) L</name>.
248  
249 For example, if you want to rebuild an index, you could disable refresh
250 until you are finished indexing:
251  
252 $admin->update_settings( refresh_interval => -1 );
253 populate_index();
254 $admin->update_settings( refresh_interval => '1s' );
255  
256 =head2 update_analyzers()
257  
258 $admin = $admin->update_analyzers( types => \@types );
259  
260 Mostly, analyzers can't be changed on an existing index, but new analyzers
261 can be added. L</update_analyzers()> will generate a new analyzer configuration
262 and try to update index (or the indices pointed to by alias) L</name>.
263  
264 You can limit the analyzers to those required for a specific list of C<@types>,
265 otherwise it calculates the analyzer configuration for all types known to the
266 L</namespace>.
267  
268 =head2 update_mapping()
269  
270 $admin = $admin->update_mapping();
271 $admin = $admin->update_mapping( @type_names );
272 $admin = $admin->update_mapping( @type_names, { ignore_conflicts=> 1 } );
273  
274 Type mappings B<cannot be changed> on an existing index, but they B<can be
275 added to>. L</update_mapping()> will generate a new type mapping from your
276 doc classes, and try to update index (or the indices pointed to by alias)
277 L</name>.
278  
279 You can optionally specify a list of types to update, otherwise it will
280 update all types known to the L</namespace>.
281  
282 $admin->update_mapping( 'user','post');
283  
284 Any optional args passed
285 as a hashref as the final parameter will be passed to
286 L<Search::Elasticsearch::Client::Direct::Indices/put_mapping()>
287  
288 =head2 delete_mapping();
289  
290 $admin = $admin->delete_mapping( @types );
291 $admin = $admin->delete_mapping( @types, { ignore => 404 });
292  
293 Deletes the type mapping B<AND THE DOCUMENTS> for the listed types in the index
294 (or the indices pointed to by alias) L</name>. Any optional args passed
295 as a hashref as the final parameter will be passed to
296 L<Search::Elasticsearch::Client::Direct::Indices/delete_mapping()>.
297  
298 =head2 exists()
299  
300 $bool = $admin->exists();
301  
302 Checks whether the index (or ALL the indices pointed to by alias ) L</name>
303 exist.
304  
305 =head2 is_alias()
306  
307 $bool = $admin->is_alias();
308  
309 Returns true if L</name> is an alias.
310  
311 =head2 is_index()
312  
313 $bool = $admin->is_index();
314  
315 Returns true if L</name> is an index.
316  
317 =head1 SEE ALSO
318  
319 =over
320  
321 =item *
322  
323 L<Elastic::Model::Index>
324  
325 =item *
326  
327 L<Elastic::Model::Alias>
328  
329 =item *
330  
331 L<Elastic::Model::Namespace>
332  
333 =item *
334  
335 L<Elastic::Manual::Scaling>
336  
337 =back
338  
339 =head1 AUTHOR
340  
341 Clinton Gormley <drtech@cpan.org>
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 This software is copyright (c) 2015 by Clinton Gormley.
346  
347 This is free software; you can redistribute it and/or modify it under
348 the same terms as the Perl 5 programming language system itself.
349  
350 =cut
351  
352 __END__
« Model/Results/Cached.pm ↑ Summary Model/Domain.pm »