line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Search::Typesense::Document; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
68
|
use v5.16.0; |
|
4
|
|
|
|
|
17
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
23
|
use Moo; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
84
|
|
6
|
|
|
|
|
|
|
with qw( |
7
|
|
|
|
|
|
|
Search::Typesense::Role::Request |
8
|
|
|
|
|
|
|
Search::Typesense::Role::UserAgentInterface |
9
|
|
|
|
|
|
|
); |
10
|
|
|
|
|
|
|
|
11
|
4
|
|
|
4
|
|
2125
|
use Mojo::JSON qw(decode_json encode_json); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
292
|
|
12
|
4
|
|
|
|
|
64
|
use Search::Typesense::Types qw( |
13
|
|
|
|
|
|
|
ArrayRef |
14
|
|
|
|
|
|
|
Enum |
15
|
|
|
|
|
|
|
HashRef |
16
|
|
|
|
|
|
|
InstanceOf |
17
|
|
|
|
|
|
|
NonEmptyStr |
18
|
|
|
|
|
|
|
Str |
19
|
|
|
|
|
|
|
compile |
20
|
4
|
|
|
4
|
|
1831
|
); |
|
4
|
|
|
|
|
16
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Search::Typesense::Document - CRUD for Typesense documents |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $typesense = Search::Typesense->new( |
29
|
|
|
|
|
|
|
host => $host, |
30
|
|
|
|
|
|
|
api_key => $key, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
my $documents = $typesense->documents; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The instantiation of this module is for internal use only. The methods are |
35
|
|
|
|
|
|
|
public. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 C<create> |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $document = $typesense->documents->create($collection, \%data); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#index-document> |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub create { |
50
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection, $document ) = @_; |
51
|
0
|
|
|
|
|
|
state $check = compile( NonEmptyStr, HashRef ); |
52
|
0
|
|
|
|
|
|
( $collection, $document ) = $check->( $collection, $document ); |
53
|
0
|
|
|
|
|
|
return $self->_POST( |
54
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents' ], |
55
|
|
|
|
|
|
|
request => $document |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 C<upsert> |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $document = $typesense->documents->upsert($collection, \%data); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#upsert> |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub upsert { |
68
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection, $document ) = @_; |
69
|
0
|
|
|
|
|
|
state $check = compile( NonEmptyStr, HashRef ); |
70
|
0
|
|
|
|
|
|
( $collection, $document ) = $check->( $collection, $document ); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# XXX It's unclear to me how to have a request body and a query string at |
73
|
|
|
|
|
|
|
# the same time with the Mojo::UserAgent. |
74
|
0
|
|
|
|
|
|
return $self->_POST( |
75
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents' ], |
76
|
|
|
|
|
|
|
request => $document, |
77
|
|
|
|
|
|
|
query => { action => 'upsert' }, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 C<update> |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $document = $typesense->documents->update($collection, $document_id, \%data); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#update-document> |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub update { |
90
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection, $document_id, $updates ) = @_; |
91
|
0
|
|
|
|
|
|
state $check = compile( NonEmptyStr, NonEmptyStr, HashRef ); |
92
|
0
|
|
|
|
|
|
( $collection, $document_id, $updates ) = |
93
|
|
|
|
|
|
|
$check->( $collection, $document_id, $updates ); |
94
|
0
|
|
|
|
|
|
return $self->_PATCH( |
95
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents', $document_id ], |
96
|
|
|
|
|
|
|
request => $updates |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 C<delete> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $document = $typesense->documents->delete($collection_name, $document_id); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#delete-document> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub delete { |
109
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection, $document_id ) = @_; |
110
|
0
|
|
|
|
|
|
state $check = compile( NonEmptyStr, NonEmptyStr ); |
111
|
0
|
|
|
|
|
|
( $collection, $document_id ) = $check->( $collection, $document_id ); |
112
|
0
|
|
|
|
|
|
return $self->_DELETE( |
113
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents', $document_id ] ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 C<export> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $export = $typesense->documents->export($collection_name); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Response as shown at L<https://typesense.org/docs/0.19.0/api/#export-documents> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
(An arrayref of hashrefs) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub export { |
127
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection ) = @_; |
128
|
0
|
|
|
|
|
|
state $check = compile(NonEmptyStr); |
129
|
0
|
|
|
|
|
|
($collection) = $check->($collection); |
130
|
0
|
0
|
|
|
|
|
my $tx = $self->_GET( |
131
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents', 'export' ], |
132
|
|
|
|
|
|
|
return_transaction => 1 |
133
|
|
|
|
|
|
|
) or return; # 404 |
134
|
0
|
|
|
|
|
|
return [ map { decode_json($_) } split /\n/ => $tx->res->body ]; |
|
0
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 C<import> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my $response = $typesense->documents->import( |
140
|
|
|
|
|
|
|
$collection_name, |
141
|
|
|
|
|
|
|
$action, |
142
|
|
|
|
|
|
|
\@documents, |
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Response as shown at L<https://typesense.org/docs/0.19.0/api/#import-documents> |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
C<$action> must be one of C<create>, C<update>, or C<upsert>. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub import { |
152
|
0
|
|
|
0
|
|
|
my $self = shift; |
153
|
0
|
|
|
|
|
|
state $check = compile( |
154
|
|
|
|
|
|
|
NonEmptyStr, |
155
|
|
|
|
|
|
|
Enum [qw/create upsert update/], |
156
|
|
|
|
|
|
|
ArrayRef [HashRef], |
157
|
|
|
|
|
|
|
); |
158
|
0
|
|
|
|
|
|
my ( $collection, $action, $documents ) = $check->(@_); |
159
|
0
|
|
|
|
|
|
my $request_body = join "\n" => map { encode_json($_) } @$documents; |
|
0
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $tx = $self->_POST( |
162
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents', "import" ], |
163
|
|
|
|
|
|
|
request => $request_body, |
164
|
|
|
|
|
|
|
query => { action => $action }, |
165
|
|
|
|
|
|
|
return_transaction => 1, |
166
|
|
|
|
|
|
|
); |
167
|
0
|
|
|
|
|
|
my $response = $tx->res->json; |
168
|
0
|
0
|
|
|
|
|
if ( exists $response->{success} ) { |
169
|
0
|
|
|
|
|
|
$response->{success} += 0; |
170
|
|
|
|
|
|
|
} |
171
|
0
|
|
|
|
|
|
return $response; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|