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.4'; # 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 has_non_attribute_mapping {
141 _source => {
142 enabled => "false",
143 },
144 };
145  
146 1;
147  
148 You define your attributes as you would expect, setting Moose type attributes, and also using the C<type> attribute to tell elasticsearch what the elasticsearch type is. You can set C<non_attribute_mapping>s here as well, as shown in the above example, and these will be folded into your index mapping.
149  
150 =cut
151  
152 1 Moose::Exporter->setup_import_methods(
153 with_meta => [qw(index analyzer tokenizer filter normalizer has_document_namespace)],
154 # The class itself does this role
155 base_class_roles => ["Elasticsearch::Model::Role"],
156  
157 # The class's meta_class does this role.
158 # We need class_metaroles here because we do not actually have an object instance
159 # at the time the indexes, analyzers, tokenizers, filters and normalizers are constructed.
160 # All we have at that time is the Moose::Meta::Class. So the functionality to support
161 # that sugar needs to be in a role applied to that Moose::Meta::Class, and not in a role
162 # applied to our base class, i.e, whatever is using this class.
163 class_metaroles => {class => ['Elasticsearch::Model::Role::Metaclass']},
164 );
165  
166  
167 =head1 PUBLIC METHODS
168  
169 =head2 index
170  
171 This is an overloaded method that has 2 modes of operation:
172  
173 =over
174  
175 =item 1 Adds one single index to the model's metaclass.
176  
177 This is a compile-time metaclass method that is called when you have the following in your model class:
178  
179 index addresses => (
180 type => 'Address',
181 );
182  
183 =item 2 Retrieves a single index object from the model instance.
184  
185 This is a run-time instance method that is called when you do the following:
186  
187 my $index_object = $model->index($doc_type);
188  
189 =back
190  
191 =cut
192  
193 sub index {
194 18 ▶ my ($self, $name, @args) = @_;
Subroutine index: called CC=2 SCAR=6.9
Pod documented
195 18 ▶ if (not ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $name) { } 5 13 18
196 # "Metaclass call to index";
197 5 _add_index_for_name($self, $name, {@args});
198 5 return;
199 } else {
200 # "Object method call to index";
201 # $name is our object instance
202 13 my $index_name = $args[0];
203 13 my $options = $name->meta->get_index($index_name);
204 13 my $index = Elasticsearch::Model::Index->new(
205 name => $index_name,
206 %$options,
207 model => $name,
208 document_namespace => $name->document_namespace,
209 );
210 13 return $index;
211 }
212 }
213  
214 =head2 analyzer
215  
216 This is a compile-time metaclass method that enables you to declare a custom analyzer using the following syntax in your model class:
217  
218 analyzer electro => (
219 type => "custom",
220 tokenizer => "standard",
221 filter => ["lowercase", "filtration"],
222 );
223  
224 =cut
225  
226 sub analyzer {
227 1 ▶ my ($self, $name, %args) = @_;
Subroutine analyzer: called CC=1 SCAR=0.0
Pod documented
228 1 $self->add_analyzer($name, \%args);
229 }
230  
231 =head2 tokenizer
232  
233 This is a compile-time metaclass method that enables you to declare a custom tokenizer using the following syntax in your model class:
234  
235 tokenizer splat => (
236 type => "simple_pattern",
237 pattern => "[0123456789]{3}",
238 );
239  
240 =cut
241  
242 sub tokenizer {
243 1 ▶ my ($self, $name, %args) = @_;
Subroutine tokenizer: called CC=1 SCAR=0.0
Pod documented
244 1 $self->add_tokenizer($name, \%args);
245 }
246  
247 =head2 normalizer
248  
249 This is a compile-time metaclass method that enables you to declare a custom normalizer using the following syntax in your model class:
250  
251 normalizer normie => (
252 type => "custom",
253 filter => ["lowercase"],
254 );
255  
256 =cut
257  
258 sub normalizer {
259 1 ▶ my ($self, $name, %args) = @_;
Subroutine normalizer: called CC=1 SCAR=0.0
Pod documented
260 1 $self->add_normalizer($name, \%args);
261 }
262  
263 =head2 filter
264  
265 This is a compile-time metaclass method that enables you to declare a custom filter using the following syntax in your model class:
266  
267 filter filtration => (
268 type => "edge_ngram",
269 min_gram => 1,
270 max_gram => 24,
271 );
272  
273 =cut
274  
275 sub filter {
276 2 ▶ my ($self, $name, %args) = @_;
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
277 2 $self->add_filter($name, \%args);
278 }
279  
280 =head2 has_document_namespace
281  
282 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:
283  
284 has_document_namespace "MyApplication::Elasticsearch::DocumentClasses";
285  
286 =cut
287  
288 sub has_document_namespace {
289 1 ▶ my ($self, $namespace) = @_;
Subroutine has_document_namespace: called CC=1 SCAR=0.0
Pod documented
290 1 $self->document_namespace($namespace);
291 }
292  
293 =head1 PRIVATE METHODS
294  
295 =head2 _apply_namespace
296  
297 Takes the index name and the namespace, and constructs the namespaced_name.
298  
299 Used internally, when adding an index to the model's metaclass. See L<Elasticsearch::Model::Role::Metaclass>.
300  
301 =cut
302  
303 sub _apply_namespace {
304 5 ▶ my ($name, $args) = @_;
Subroutine _apply_namespace: called CC=2 SCAR=6.9
305 5 my $namespace = $args->{namespace};
306 5 my $namespaced_name = $name;
307 5 ▶ $namespaced_name = $namespace . "_" . $name if $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace 3 2 5
308 5 return ($name, $namespaced_name);
309 }
310  
311 =head2 _add_index_for_name
312  
313 Takes the name of an index and some arguments, and adds that index to the model's metaclass. See L<Elasticsearch::Model::Role::Metaclass>.
314  
315 =cut
316  
317 sub _add_index_for_name {
318 5 ▶ my ($self, $name, $args) = @_;
Subroutine _add_index_for_name: called CC=1 SCAR=0.0
319 5 my $namespaced_name = $name;
320 5 ($name, $namespaced_name) = _apply_namespace($name,$args);
321 5 $self->add_index($name, {%$args, name => $name, namespaced_name => $namespaced_name});
322 }
323  
324 1 __PACKAGE__->meta->make_immutable;
325  
326 1 1;
« Model/Types.pm ↑ Summary Model/Document.pm »