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