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