File Coverage

blib/lib/Elastic/Model/Role/Index.pm
Criterion Covered Total %
statement 25 67 37.3
branch 6 10 60.0
condition 0 3 0.0
subroutine 6 18 33.3
pod 12 12 100.0
total 49 110 44.5


line stmt bran cond sub pod time code
1             package Elastic::Model::Role::Index;
2             $Elastic::Model::Role::Index::VERSION = '0.52';
3 23     23   19014 use Moose::Role;
  23         51  
  23         219  
4 23     23   123208 use MooseX::Types::Moose qw(Str);
  23         59  
  23         297  
5 23     23   118372 use Carp;
  23         59  
  23         1595  
6              
7 23     23   139 use namespace::autoclean;
  23         49  
  23         213  
8              
9             #===================================
10             has 'name' => (
11             #===================================
12                 is => 'ro',
13                 isa => Str,
14                 required => 1,
15             );
16              
17             #===================================
18             has 'namespace' => (
19             #===================================
20                 is => 'ro',
21                 isa => 'Elastic::Model::Namespace',
22                 handles => [ 'model', 'mappings' ]
23             );
24              
25 23     23   2794 no Moose::Role;
  23         64  
  23         138  
26              
27             #===================================
28             sub index_config {
29             #===================================
30 5     5 1 91     my ( $self, %args ) = @_;
31 5 100       13     my %settings = %{ $args{settings} || {} };
  5         41  
32              
33 5 100       10     my @types = @{ $args{types} || [] };
  5         27  
34 5         31     my $mappings = $self->mappings(@types);
35 5         25     my $meta = Class::MOP::class_of( $self->model );
36 5 100       62     if ( my $analysis = $meta->analysis_for_mappings($mappings) ) {
37 2         7         $settings{analysis} = $analysis;
38                 }
39              
40                 return {
41 3         108         index => $self->name,
42                     settings => \%settings,
43                     mappings => $mappings
44                 };
45             }
46              
47             #===================================
48 0     0 1   sub delete { shift->_index_action( 'delete_index', @_ ) }
49 0     0 1   sub refresh { shift->_index_action( 'refresh_index', @_ ) }
50 0     0 1   sub open { shift->_index_action( 'open_index', @_ ) }
51 0     0 1   sub close { shift->_index_action( 'close_index', @_ ) }
52 0     0 1   sub exists { !!$_[0]->model->store->index_exists( index => $_[0]->name ) }
53             #===================================
54              
55             #===================================
56             sub _index_action {
57             #===================================
58 0     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             sub update_settings {
67             #===================================
68 0     0 1       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             sub update_analyzers {
78             #===================================
79 0     0 1       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             sub is_alias {
88             #===================================
89 0     0 1       my $self = shift;
90 0               my $name = $self->name;
91 0               my $indices = $self->model->store->get_aliases( index => $name );
92 0   0           return !!( %$indices && !$indices->{$name} );
93             }
94              
95             #===================================
96             sub is_index {
97             #===================================
98 0     0 1       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             sub update_mapping {
106             #===================================
107 0     0 1       my $self = shift;
108 0 0             my %args = ref $_[-1] eq 'HASH' ? %{ pop() } : ();
  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             sub delete_mapping {
125             #===================================
126 0     0 1       my $self = shift;
127 0 0             my %args = ref $_[-1] eq 'HASH' ? %{ pop() } : ();
  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             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__
353            
354             # ABSTRACT: Provides admin methods common to indices and aliases
355            
356