Model.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 100% 61 / 61 SCAR 11.0
CC
3
Cov
100%
index
2.0
_apply_namespace
2.0
BEGIN
1.0
CRAP 3.0
« Model/Types.pm ↑ Summary Model/Document.pm »
1 package Elasticsearch::Model;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Elasticsearch::Model::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.1.3'; # VERSION
8  
9 # ABSTRACT: Does one thing only: helps to deploy a Moose model and accompanying document classes to Elasticsearch.
10  
11 =head1 NAME
12  
13 Elasticsearch::Model
14  
15 =head1 DESCRIPTION
16  
17 This module is meant to be used very much like L<ElasticSearchX::Model>, before it was deprecated. In fact, most of the code is borrowed from that module.
18  
19 All the changes I have made are aimed at two things:
20  
21 =over
22  
23 =item 1
24  
25 Compatibility with L<Elasticsearch 6+|https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html>
26  
27 =item 2
28  
29 I<Mapping, period.> Classes to index is all that this distribution does. We do not take data from elasticsearch and wrap it in the document type classes. We do not handle index aliasing, either, which can be quite complicated. For aliasing, however, C<deploy_to> will allow you to specify the name of the index you will be creating, so that you can make the alias more directly.
30  
31 =back
32  
33 =head1 SYNOPSIS
34  
35 This is the simplest use case:
36  
37 my $model = MyApplication::Elasticsearch::Model->new;
38  
39 $model->deploy;
40  
41 This is probably the actual use case, in which you have an alias for a versioned index name:
42  
43 my $document_types = {
44 order => 'Order',
45 user => 'User',
46 note => 'Note',
47 };
48  
49 my $model = MyApplication::Elasticsearch::Model->new;
50  
51 for my $doc_type (keys %$document_types) {
52 my $index_object = $model->index($doc_type);
53 my $versioned_index_name = $doc_type . "_" . time(); # to get "order_12345859245"
54 $index_object->deploy_to(index_name => $versioned_index_name);
55  
56 # Get index currently point to by an alias
57 my $old_index_name = $model->es->cat->aliases(name => $doc_type, format => 'json')->[0]->{index};
58  
59 # Switch the alias to the new index, and remove the alias from the old one.
60 $model->es->indices->update_aliases(
61 body => {
62 actions => [
63 {add => {alias => $doc_type, index => $new_index_name}},
64 (
65 $old_index_name
66 ? ({remove => {alias => $doc_type, index => $old_index_name}})
67 : ()
68 ),
69 ],
70 },
71 );
72 }
73  
74  
75 To do either of the above, you start with a model.
76  
77 package MyApplication::Elasticsearch::Model;
78  
79 use Moose;
80 use Elasticsearch::Model;
81  
82 my $document_types = {
83 order => 'Order',
84 user => 'User',
85 note => 'Note',
86 };
87  
88 my $index_settings = {
89 refresh_interval => '2s',
90 number_of_shards => 4,
91 number_of_replicas => 3,
92 };
93  
94 for my $doc_type (keys %$document_types) {
95 index $doc_type => (
96 namespace => 'private',
97 type => $document_types->{$doc_type},
98 traits => ["MyApplication::Elasticsearch::DocTrait"],
99 index_settings => $index_settings,
100 );
101 }
102  
103 normalizer normie => (
104 type => "custom",
105 filter => ["lowercase"],
106 );
107  
108 1;
109  
110 Elasticsearch 6+ more or less requires one document type per index, so this makes things simple for us. We define one document class per document type, e.g., type C<user> gets document class C<User>.
111  
112 We will, by default, search for these document classes underneath the model class; in this case, we would search for C<MyApplication::Elasticsearch::Model::User>. You can, however, define a C<document_namespace> in your model class as follows:
113  
114 ...
115  
116 has_document_namespace "MyApplication::ESDocumentClasses";
117 ...
118  
119 This would mean that we look for C<MyApplication::ESDocumentClasses::User>.
120  
121 You use C<index> to define the indexes belonging to your model. Here is an example of the Note document class.
122  
123 package MyApplication::Elasticsearch::Model::Note;
124  
125 use Moose;
126 use Elasticsearch::Model::Document;
127  
128 has text => (
129 is => 'ro',
130 isa => 'Maybe[Str]',
131 type => 'text',
132 );
133  
134 has author_id => (
135 is => 'ro',
136 isa => 'Maybe[Int]',
137 type => 'integer',
138 );
139  
140 1;
141  
142 =cut
143  
144 1 Moose::Exporter->setup_import_methods(
145 with_meta => [qw(index analyzer tokenizer filter normalizer has_document_namespace)],
146 # The class itself does this role
147 base_class_roles => ["Elasticsearch::Model::Role"],
148  
149 # The class's meta_class does this role.
150 # We need class_metaroles here because we do not actually have an object instance
151 # at the time the indexes, analyzers, tokenizers, filters and normalizers are constructed.
152 # All we have at that time is the Moose::Meta::Class. So the functionality to support
153 # that sugar needs to be in a role applied to that Moose::Meta::Class, and not in a role
154 # applied to our base class, i.e, whatever is using this class.
155 class_metaroles => {class => ['Elasticsearch::Model::Role::Metaclass']},
156 );
157  
158  
159 =head1 PUBLIC METHODS
160  
161 =head2 index
162  
163 This is an overloaded method that has 2 modes of operation:
164  
165 =over
166  
167 =item 1 Adds one single index to the model's metaclass.
168  
169 This is a compile-time metaclass method that is called when you have the following in your model class:
170  
171 index addresses => (
172 type => 'Address',
173 );
174  
175 =item 2 Retrieves a single index object from the model instance.
176  
177 This is a run-time instance method that is called when you do the following:
178  
179 my $index_object = $model->index($doc_type);
180  
181 =back
182  
183 =cut
184  
185 sub index {
186 17 ▶ my ($self, $name, @args) = @_;
Subroutine index: called CC=2 SCAR=6.9
Pod documented
187 17 ▶ if (not ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $name) { } 5 12 17
188 # "Metaclass call to index";
189 5 _add_index_for_name($self, $name, {@args});
190 5 return;
191 } else {
192 # "Object method call to index";
193 # $name is our object instance
194 12 my $index_name = $args[0];
195 12 my $options = $name->meta->get_index($index_name);
196 12 my $index = Elasticsearch::Model::Index->new(
197 name => $index_name,
198 %$options,
199 model => $name,
200 document_namespace => $name->document_namespace,
201 );
202 12 return $index;
203 }
204 }
205  
206 =head2 analyzer
207  
208 This is a compile-time metaclass method that enables you to declare a custom analyzer using the following syntax in your model class:
209  
210 analyzer electro => (
211 type => "custom",
212 tokenizer => "standard",
213 filter => ["lowercase", "filtration"],
214 );
215  
216 =cut
217  
218 sub analyzer {
219 1 ▶ my ($self, $name, %args) = @_;
Subroutine analyzer: called CC=1 SCAR=0.0
Pod documented
220 1 $self->add_analyzer($name, \%args);
221 }
222  
223 =head2 tokenizer
224  
225 This is a compile-time metaclass method that enables you to declare a custom tokenizer using the following syntax in your model class:
226  
227 tokenizer splat => (
228 type => "simple_pattern",
229 pattern => "[0123456789]{3}",
230 );
231  
232 =cut
233  
234 sub tokenizer {
235 1 ▶ my ($self, $name, %args) = @_;
Subroutine tokenizer: called CC=1 SCAR=0.0
Pod documented
236 1 $self->add_tokenizer($name, \%args);
237 }
238  
239 =head2 normalizer
240  
241 This is a compile-time metaclass method that enables you to declare a custom normalizer using the following syntax in your model class:
242  
243 normalizer normie => (
244 type => "custom",
245 filter => ["lowercase"],
246 );
247  
248 =cut
249  
250 sub normalizer {
251 1 ▶ my ($self, $name, %args) = @_;
Subroutine normalizer: called CC=1 SCAR=0.0
Pod documented
252 1 $self->add_normalizer($name, \%args);
253 }
254  
255 =head2 filter
256  
257 This is a compile-time metaclass method that enables you to declare a custom filter using the following syntax in your model class:
258  
259 filter filtration => (
260 type => "edge_ngram",
261 min_gram => 1,
262 max_gram => 24,
263 );
264  
265 =cut
266  
267 sub filter {
268 2 ▶ my ($self, $name, %args) = @_;
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
269 2 $self->add_filter($name, \%args);
270 }
271  
272 =head2 has_document_namespace
273  
274 This is a compile-time metaclass method that enables you to declare the namespace in which to search for your document classes in your model class:
275  
276 has_document_namespace "MyApplication::Elasticsearch::DocumentClasses";
277  
278 =cut
279  
280 sub has_document_namespace {
281 1 ▶ my ($self, $namespace) = @_;
Subroutine has_document_namespace: called CC=1 SCAR=0.0
Pod documented
282 1 $self->document_namespace($namespace);
283 }
284  
285 =head1 PRIVATE METHODS
286  
287 =head2 _apply_namespace
288  
289 Takes the index name and the namespace, and constructs the namespaced_name.
290  
291 Used internally, when adding an index to the model's metaclass. See L<Elasticsearch::Model::Role::Metaclass>.
292  
293 =cut
294  
295 sub _apply_namespace {
296 5 ▶ my ($name, $args) = @_;
Subroutine _apply_namespace: called CC=2 SCAR=6.9
297 5 my $namespace = $args->{namespace};
298 5 my $namespaced_name = $name;
299 5 ▶ $namespaced_name = $namespace . "_" . $name if $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace 3 2 5
300 5 return ($name, $namespaced_name);
301 }
302  
303 =head2 _add_index_for_name
304  
305 Takes the name of an index and some arguments, and adds that index to the model's metaclass. See L<Elasticsearch::Model::Role::Metaclass>.
306  
307 =cut
308  
309 sub _add_index_for_name {
310 5 ▶ my ($self, $name, $args) = @_;
Subroutine _add_index_for_name: called CC=1 SCAR=0.0
311 5 my $namespaced_name = $name;
312 5 ($name, $namespaced_name) = _apply_namespace($name,$args);
313 5 $self->add_index($name, {%$args, name => $name, namespaced_name => $namespaced_name});
314 }
315  
316 1 __PACKAGE__->meta->make_immutable;
317  
318 1 1;
« Model/Types.pm ↑ Summary Model/Document.pm »