blib/lib/ElasticSearchX/Model/Index.pm

filter: Statementstmt 37.5% 24 / 64 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 4 / 12 Podpod 40.0% 2 / 5 totaltotal 34.4% 30 / 87 CC 8 SCAR 27.2
CC
8
Cov
34%
deployment_statement
42.0
_build_types
6.0
BUILD
6.0
CRAP 15.2
« blib/lib/ElasticSearchX/Model/Document/Trait/Class.pm ↑ Summary blib/lib/ElasticSearchX/Model/Document/Role.pm »
1 #
2 # This file is part of ElasticSearchX-Model
3 #
4 # This software is Copyright (c) 2018 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 14 $ElasticSearchX::Model::Index::VERSION = '1.0.3';
12 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Module::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use ElasticSearchX::Model::Document::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 has name => ( is => 'ro' );
18  
19 14 has path => ( is => 'ro' );
20  
21 14 has namespace => ( is => 'ro', lazy_build => 1 );
22  
23 14 has [qw(shards replicas)] => ( is => 'ro', default => 1 );
24  
25 14 has model => ( is => 'ro', required => 1, handles => [qw(es bulk)] );
26  
27 0 ▶has traits => ( isa => 'ArrayRef', is => 'ro', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
28  
29 14 has refresh_interval => ( is => 'ro', default => '1s' );
30  
31 14 has dynamic => ( is => 'ro', isa => 'Bool', default => 0 );
32  
33 14 has alias_for => ( is => 'ro', isa => 'Str' );
34  
35 14 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 0 ▶ my $self = shift;
Subroutine _build_types: not called CC=2 SCAR=17.9
52 0 my $namespace = $self->namespace;
53 0 my %stash = Class::MOP::get_all_metaclasses;
54 my @found = (
55 Module::Find::findallmod($namespace),
56 0 grep {/^\Q$namespace\E::/} keys %stash
57 );
58 0 map { Class::Load::load_class($_) } @found;
59 @found = grep {
60 0 ▶ $_->isa('Moose::Object')
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("Moose::Object") and $_->does("ElasticSearchX::Model::Document::Role") 0 0 0
61 && $_->does('ElasticSearchX::Model::Document::Role')
62 } @found;
63 0 return { map { $_->meta->short_name => $_->meta } @found };
64 }
65  
66 sub BUILD {
67 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod undocumented
68 0 foreach my $trait ( @{ $self->traits } ) {
69 0 Moose::Util::ensure_all_roles( $self, $trait );
70 }
71 0 return $self;
72 }
73  
74 sub _build_namespace {
75 0 ▶ ref shift->model;
Subroutine _build_namespace: not called CC=1 SCAR=6.9
76 }
77  
78 sub type {
79 0 ▶ my ( $self, $type ) = @_;
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
80 0 my $class = $self->get_type($type)->set_class;
81 0 Class::Load::load_class($class);
82 0 return $class->new(
83 index => $self,
84 type => $self->get_type($type),
85 );
86 }
87  
88 sub deployment_statement {
89 0 ▶ my $self = shift;
Subroutine deployment_statement: not called CC=6 SCAR=37.4
Pod undocumented
90 0 my $deploy = {};
91 0 foreach my $type ( $self->get_types ) {
92 next
93 0 ▶ if $type->does_role(
Branch: 1 branch
BranchTrueFalseTotal
if $type->does_role("ElasticSearchX::Model::Document::EmbeddedRole") 0 0 0
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 ▶ $deploy->{settings}->{index}->{mapper}->{dynamic} = \0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dynamic 0 0 0
113 unless ( $self->dynamic );
114  
115 0 return $deploy;
116 }
117  
118 sub refresh {
119 0 ▶ my $self = shift;
Subroutine refresh: not called CC=1 SCAR=6.9
Pod documented
120 0 $self->es->indices->refresh( index => $self->name );
121 }
122  
123 sub delete {
124 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
125 0 $self->es->indices->delete( index => $self->name );
126 }
127  
128 14 __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 1.0.3
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) 2018 by Moritz Onken.
234  
235 This is free software, licensed under:
236  
237 The (three-clause) BSD License
238  
239 =cut
« blib/lib/ElasticSearchX/Model/Document/Trait/Class.pm ↑ Summary blib/lib/ElasticSearchX/Model/Document/Role.pm »