File Coverage

blib/lib/ElasticSearchX/Model/Index.pm
Criterion Covered Total %
statement 33 51 64.7
branch 1 6 16.6
condition n/a
subroutine 8 11 72.7
pod 2 5 40.0
total 44 73 60.2


line stmt bran cond sub pod time code
1             #
2             # This file is part of ElasticSearchX-Model
3             #
4             # This software is Copyright (c) 2019 by Moritz Onken.
5             #
6             # This is free software, licensed under:
7             #
8             # The (three-clause) BSD License
9             #
10             package ElasticSearchX::Model::Index;
11             $ElasticSearchX::Model::Index::VERSION = '2.0.1';
12 11     11   76 use Moose;
  11         26  
  11         64  
13 11     11   66469 use Module::Find ();
  11         9559  
  11         256  
14 11     11   79 use Class::Load ();
  11         23  
  11         254  
15 11     11   3156 use ElasticSearchX::Model::Document::Types qw(:all);
  11         44  
  11         98  
16              
17             has name => ( is => 'ro' );
18              
19             has path => ( is => 'ro' );
20              
21             has namespace => ( is => 'ro', lazy_build => 1 );
22              
23             has [qw(shards replicas)] => ( is => 'ro', default => 1 );
24              
25             has model => ( is => 'ro', required => 1, handles => [qw(es bulk)] );
26              
27             has traits => ( isa => 'ArrayRef', is => 'ro', default => sub { [] } );
28              
29             has refresh_interval => ( is => 'ro', default => '1s' );
30              
31             has dynamic => ( is => 'ro', isa => 'Bool', default => 0 );
32              
33             has alias_for => ( is => 'ro', isa => 'Str' );
34              
35             has types => (
36             isa => Types,
37             coerce => 1,
38             traits => ['Hash'],
39             is => 'ro',
40             lazy_build => 1,
41             handles => {
42             get_types => 'values',
43             get_type_list => 'keys',
44             add_type => 'set',
45             remove_type => 'delete',
46             get_type => 'get',
47             }
48             );
49              
50             sub _build_types {
51 5     5   16 my $self = shift;
52 5         165 my $namespace = $self->namespace;
53 5         27 my %stash = Class::MOP::get_all_metaclasses;
54             my @found = (
55             Module::Find::findallmod($namespace),
56 5         1107 grep {/^\Q$namespace\E::/} keys %stash
  957         3596  
57             );
58 5         44 map { Class::Load::load_class($_) } @found;
  9         263  
59             @found = grep {
60 5 50       170 $_->isa('Moose::Object')
  9         1261  
61             && $_->does('ElasticSearchX::Model::Document::Role')
62             } @found;
63 5         1193 return { map { $_->meta->short_name => $_->meta } @found };
  9         123  
64             }
65              
66             sub BUILD {
67 10     10 0 19401 my $self = shift;
68 10         24 foreach my $trait ( @{ $self->traits } ) {
  10         329  
69 1         10 Moose::Util::ensure_all_roles( $self, $trait );
70             }
71 10         10566 return $self;
72             }
73              
74             sub _build_namespace {
75 3     3   95 ref shift->model;
76             }
77              
78             sub type {
79 3     3 0 1585 my ( $self, $type ) = @_;
80 3         126 my $class = $self->get_type($type)->set_class;
81 3         26 Class::Load::load_class($class);
82 3         1087 return $class->new(
83             index => $self,
84             type => $self->get_type($type),
85             );
86             }
87              
88             sub deployment_statement {
89 0     0 0   my $self = shift;
90 0           my $deploy = {};
91 0           foreach my $type ( $self->get_types ) {
92             next
93 0 0         if $type->does_role(
94             'ElasticSearchX::Model::Document::EmbeddedRole');
95 0           $deploy->{mappings}->{ $type->short_name } = $type->mapping;
96             }
97 0           my $model = $self->model->meta;
98 0           for (qw(filter analyzer tokenizer)) {
99 0           my $method = "get_${_}_list";
100 0           foreach my $name ( $model->$method ) {
101 0           my $get = "get_$_";
102 0           $deploy->{settings}->{analysis}->{$_}->{$name}
103             = $model->$get($name);
104             }
105             }
106             $deploy->{settings}->{index} = {
107 0           number_of_shards => $self->shards,
108             number_of_replicas => $self->replicas,
109             refresh_interval => $self->refresh_interval
110             };
111              
112 0 0         $deploy->{settings}->{index}->{mapper}->{dynamic} = \0
113             unless ( $self->dynamic );
114              
115 0           return $deploy;
116             }
117              
118             sub refresh {
119 0     0 1   my $self = shift;
120 0           $self->es->indices->refresh( index => $self->name );
121             }
122              
123             sub delete {
124 0     0 1   my $self = shift;
125 0           $self->es->indices->delete( index => $self->name );
126             }
127              
128             __PACKAGE__->meta->make_immutable( inline_constructor => 0 );
129              
130             __END__
131              
132             =pod
133              
134             =encoding UTF-8
135              
136             =head1 NAME
137              
138             ElasticSearchX::Model::Index
139              
140             =head1 VERSION
141              
142             version 2.0.1
143              
144             =head1 ATTRIBUTES
145              
146             =head2 name
147              
148             The name of the index.
149              
150             =head2 alias_for
151              
152             If given, this name is used as the index name in ElasticSearch. The
153             L</name> is added as alias to L</alias_for>.
154              
155             =head2 dynamic
156              
157             By default, indices are not dynamic. That means that fields that
158             are unknown to the ElasticSearch mapping will not be indexed and
159             no dynamic mapping will be auto generated. Since the mapping is
160             generated by this module, there is no need to have the dynamic
161             mapping enabled. If you want to enable it anyway, set this attribute
162             to C<1> value.
163              
164             =head2 namespace
165              
166             Types are loaded from this namespace if they are not explicitly
167             defined using L</types>. The namespace defaults to the package
168             name of the model.
169              
170             =head2 refresh_interval
171              
172             This string defines the interval in which the index is refreshed
173             and new documents are made available to the search. It defaults
174             to C<1s> and can be set to C<-1> to disable refreshing completely.
175              
176             Indices can be refreshed manually by calling L</refresh>.
177              
178             =head2 types
179              
180             A HashRef where the key is an indentifier for the value,
181             the L<ElasticSearchX::Model::Document> meta object. This attribute
182             is automatically built if it isn't provided. It uses L</namespace>
183             to look for L<ElasticSearchX::Model::Document> classes. The name is
184             derived from the class name by lowercasing the last name segment
185             (i.e. C<MyModel::Tweet> becomes C<tweet>).
186              
187             =head2 traits
188              
189             An ArrayRef of traits which are applied to the index object.
190             This is useful if you want to alter the behaviour of methods
191             like L</deploy>.
192              
193             =head2 model
194              
195             This attribute is set automatically by the model class when
196             you add an index. It can be used to access the model through
197             the index object.
198              
199             =head2 shards
200              
201             =head2 replicas
202              
203             Sets the number of shards and replicas. Both default to C<1>.
204              
205             =head2 refresh_interval
206              
207             Sets the refresh interval. Defaults to C<1s>.
208              
209             =head2 es
210              
211             The L<ElasticSearch> object.
212              
213             =head2 bulk
214              
215             Returns an instance of L<ElasticSearchX::Model::Bulk>.
216              
217             =head1 METHODS
218              
219             =head2 refresh
220              
221             Refresh index manually.
222              
223             =head2 delete
224              
225             Delete an index from ElasticSearch.
226              
227             =head1 AUTHOR
228              
229             Moritz Onken
230              
231             =head1 COPYRIGHT AND LICENSE
232              
233             This software is Copyright (c) 2019 by Moritz Onken.
234              
235             This is free software, licensed under:
236              
237             The (three-clause) BSD License
238              
239             =cut