File Coverage

blib/lib/Elastic/Model/Types.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package Elastic::Model::Types;
2             $Elastic::Model::Types::VERSION = '0.51';
3 25     25   152 use strict;
  25         70  
  25         1706  
4 25     25   128 use warnings;
  25         34  
  25         812  
5 25     25   14468 use Search::Elasticsearch();
  25         989179  
  25         836  
6              
7 25     25   18756 use MooseX::Types::Moose qw(HashRef ArrayRef Str Bool Num Int Defined Any);
  25         1373409  
  25         325  
8 25     25   167014 use MooseX::Types::Structured qw (Dict Optional Map);
  25         11207559  
  25         251  
9 25     25   11957 use namespace::autoclean;
  25         57  
  25         195  
10              
11 25         281 use MooseX::Types -declare => [ qw(
12             ArrayRefOfStr
13             Binary
14             Consistency
15             CoreFieldType
16             DynamicMapping
17             ES
18             ES_1x
19             ES_90
20             FieldType
21             GeoPoint
22             HighlightArgs
23             IndexMapping
24             IndexNames
25             Keyword
26             Latitude
27             Longitude
28             MultiField
29             MultiFields
30             PathMapping
31             Replication
32             SortArgs
33             StoreMapping
34             TermVectorMapping
35             Timestamp
36             UID
37             )
38 25     25   3111 ];
  25         49  
39              
40             my @enums = (
41                 FieldType,
42                 [ 'string', 'integer', 'long', 'float',
43                     'double', 'short', 'byte', 'boolean',
44                     'date', 'binary', 'object', 'nested',
45                     'ip', 'geo_point', 'attachment'
46                 ],
47                 CoreFieldType,
48                 [ 'string', 'integer', 'long', 'float',
49                     'double', 'short', 'byte', 'boolean',
50                     'date', 'ip', 'geo_point'
51                 ],
52                 TermVectorMapping,
53                 [ 'no', 'yes', 'with_offsets', 'with_positions', 'with_positions_offsets' ],
54                 IndexMapping,
55                 [ 'analyzed', 'not_analyzed', 'no' ],
56                 DynamicMapping,
57                 [ 'false', 'strict', 'true' ],
58                 PathMapping,
59                 [ 'just_name', 'full' ],
60                 Replication,
61                 [ 'sync', 'async' ],
62                 Consistency,
63                 [ 'quorum', 'one', 'all' ],
64             );
65              
66             while ( my $type = shift @enums ) {
67                 my $vals = shift @enums;
68                 subtype(
69                     $type,
70                     { as => enum($vals),
71                         message => sub { "Allowed values are: " . join '|', @$vals }
72                     }
73                 );
74             }
75              
76             class_type ES_1x, { class => 'Search::Elasticsearch::Client::1_0::Direct' };
77             class_type ES_90, { class => 'Search::Elasticsearch::Client::0_90::Direct' };
78              
79             #===================================
80             subtype ES(), as ES_1x | ES_90;
81             #===================================
82             coerce ES, from HashRef, via { Search::Elasticsearch->new($_) };
83             coerce ES, from Str, via {
84                 s/^:/127.0.0.1:/;
85                 Search::Elasticsearch->new( nodes => $_ );
86             };
87             coerce ES, from ArrayRef, via {
88                 my @nodes = @$_;
89                 s/^:/127.0.0.1:/ for @nodes;
90                 Search::Elasticsearch->new( servers => \@nodes );
91             };
92              
93             #===================================
94             subtype StoreMapping, as enum( [ 'yes', 'no' ] );
95             #===================================
96             coerce StoreMapping, from Any, via { $_ ? 'yes' : 'no' };
97              
98             #===================================
99             subtype MultiField, as Dict [
100             #===================================
101                 type => Optional [CoreFieldType],
102                 index => Optional [IndexMapping],
103                 index_name => Optional [Str],
104                 boost => Optional [Num],
105                 null_value => Optional [Str],
106                 analyzer => Optional [Str],
107                 index_analyzer => Optional [Str],
108                 search_analyzer => Optional [Str],
109                 search_quote_analyzer => Optional [Str],
110                 term_vector => Optional [TermVectorMapping],
111                 geohash => Optional [Bool],
112                 lat_lon => Optional [Bool],
113                 geohash_precision => Optional [Int],
114                 precision_step => Optional [Int],
115                 format => Optional [Str],
116              
117             ];
118              
119             #===================================
120             subtype MultiFields, as HashRef [MultiField];
121             #===================================
122              
123             #===================================
124             subtype SortArgs, as ArrayRef;
125             #===================================
126             coerce SortArgs, from HashRef, via { [$_] };
127             coerce SortArgs, from Str, via { [$_] };
128              
129             #===================================
130             subtype HighlightArgs, as HashRef;
131             #===================================
132             coerce HighlightArgs, from Str, via { return { $_ => {} } };
133             coerce HighlightArgs, from ArrayRef, via {
134                 my $args = $_;
135                 my %fields;
136              
137                 while ( my $field = shift @$args ) {
138                     die "Expected a field name but got ($field)"
139                         if ref $field;
140                     $fields{$field} = ref $args->[0] eq 'HASH' ? shift @$args : {};
141                 }
142                 return \%fields;
143             };
144              
145             #===================================
146             subtype Longitude, as Num,
147             #===================================
148                 where { $_ >= -180 and $_ <= 180 },
149                 message {"Longitude must be in the range -180 to 180"};
150              
151             #===================================
152             subtype Latitude, as Num,
153             #===================================
154                 where { $_ >= -90 and $_ <= 90 },
155                 message {"Latitude must be in the range -90 to 90"};
156              
157             #===================================
158             subtype GeoPoint, as Dict [ lat => Latitude, lon => Longitude ];
159             #===================================
160             coerce GeoPoint, from ArrayRef, via { { lon => $_->[0], lat => $_->[1] } };
161             coerce GeoPoint, from Str, via {
162                 my ( $lat, $lon ) = split /,/;
163                 { lon => $lon, lat => $lat };
164             };
165              
166             #===================================
167             subtype Binary, as Defined;
168             #===================================
169              
170             #===================================
171             subtype IndexNames, as ArrayRef [Str],
172             #===================================
173                 where { @{$_} > 0 }, #
174                 message {"At least one domain name is required"};
175             coerce IndexNames, from Str, via { [$_] };
176              
177             #===================================
178             subtype ArrayRefOfStr, as ArrayRef [Str];
179             #===================================
180             coerce ArrayRefOfStr, from Str, via { [$_] };
181              
182             #===================================
183             subtype Timestamp, as Num;
184             #===================================
185              
186             #===================================
187             subtype Keyword, as Str;
188             #===================================
189              
190             #===================================
191             class_type UID, { class => 'Elastic::Model::UID' };
192             #===================================
193             coerce UID, from Str, via { Elastic::Model::UID->new_from_string($_) };
194             coerce UID, from HashRef, via { Elastic::Model::UID->new($_) };
195              
196             1;
197              
198             =pod
199            
200             =encoding UTF-8
201            
202             =head1 NAME
203            
204             Elastic::Model::Types - MooseX::Types for general and internal use
205            
206             =head1 VERSION
207            
208             version 0.51
209            
210             =head1 SYNOPSIS
211            
212             use Elastic::Model::Types qw(GeoPoint);
213            
214             has 'point' => (
215             is => 'ro',
216             isa => GeoPoint,
217             coerce => 1
218             );
219            
220             =head1 DESCRIPTION
221            
222             Elastic::Model::Types define a number of L<MooseX::Types>, some for internal
223             use and some which will be useful generally.
224            
225             =head1 PUBLIC TYPES
226            
227             =head2 Keyword
228            
229             use Elastic::Model::Types qw(Keyword);
230            
231             has 'status' => (
232             is => 'ro',
233             isa => Keyword
234             );
235            
236             C<Keyword> is a sub-type of C<Str>. It is provided to make it easy to map
237             string values which should not be analyzed (eg a C<status> field rather than
238             a C<comment_body> field). See L<Elastic::Model::TypeMap::ES/Keyword>.
239            
240             =head2 Binary
241            
242             use Elastic::Model::Types qw(Binary);
243            
244             has 'binary_field' => (
245             is => 'ro',
246             isa => Binary
247             );
248            
249             Inherits from the C<Defined> type. Is automatically Base64 encoded/decoded.
250            
251             =head2 GeoPoint
252            
253             use Elastic::Model::Types qw(GeoPoint);
254            
255             has 'point' => (
256             is => 'ro',
257             isa => GeoPoint,
258             coerce => 1,
259             );
260            
261             C<GeoPoint> is a hashref with two keys:
262            
263             =over
264            
265             =item *
266            
267             C<lon>: a C<Number> between -180 and 180
268            
269             =item *
270            
271             C<lat>: a C<Number> between -90 and 90
272            
273             =back
274            
275             It can be coerced from an C<ArrayRef> with C<[$lon,$lat]> and from a
276             C<Str> with C<"$lat,$lon">.
277            
278             =head2 Timestamp
279            
280             use Elastic::Model::Types qw(Timestamp);
281            
282             has 'timestamp' => (
283             is => 'ro',
284             isa => Timestamp
285             );
286            
287             A C<Timestamp> is a C<Num> which holds floating point epoch seconds, with milliseconds resolution.
288             It is automatically mapped as a C<date> field in Elasticsearch.
289            
290             =head1 AUTHOR
291            
292             Clinton Gormley <drtech@cpan.org>
293            
294             =head1 COPYRIGHT AND LICENSE
295            
296             This software is copyright (c) 2015 by Clinton Gormley.
297            
298             This is free software; you can redistribute it and/or modify it under
299             the same terms as the Perl 5 programming language system itself.
300            
301             =cut
302              
303             __END__
304            
305             # ABSTRACT: MooseX::Types for general and internal use
306            
307