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