Typesense.pm

filter: Statementstmt 21.3% 31 / 145 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 27.5% 8 / 29 Podpod 92.3% 12 / 13 totaltotal 22.3% 51 / 228 CC 23 SCAR 27.1
CC
23
Cov
17%
search
42.0
_check_for_failure
42.0
create_collection
20.0
CRAP 15.0
↑ Summary Typesense/Types.pm »
1 package Search::Typesense;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Search::Typesense - Perl interface to Typesense search engine.
15  
16 =head1 SYNOPSIS
17  
18 my $typesense = Search::Typesense->new(
19 use_https => $bool,
20 host => $host,
21 port => $port,
22 );
23
24 my $results = $typesense->search(
25 $collection_name,
26 { q => 'Search String' },
27 );
28 if ( $results->{found} ) {
29 foreach my $hit ( @{ $results->{hits} } ) {
30 ...;
31 }
32 }
33  
34 =head1 DESCRIPTION
35  
36 This is a simple interface to the L<Typesense|https://typesense.org/> search
37 engine. Most methods will do one of three things:
38  
39 =over 4
40  
41 =item * Return results as defined in the Typesense documentation (listed per section)
42  
43 =item * Return nothing if Typesense returns a 404.
44  
45 =item * C<croak> if Typesense returns an error.
46  
47 =back
48  
49 =cut
50  
51 1 use Search::Typesense::Types qw(
52 ArrayRef
53 Bool
54 Enum
55 HashRef
56 InstanceOf
57 NonEmptyStr
58 Str
59 compile
60 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 =head1 VERSION
63  
64 Version 0.01
65  
66 =cut
67  
68 1 our $VERSION = '0.01';
69  
70 1 has _ua => (
71 is => 'ro',
72 isa => InstanceOf ['Mojo::UserAgent'],
73 lazy => 1,
74 builder => 1,
75 );
76  
77 sub _build__ua {
78 0 ▶ my $self = shift;
Subroutine _build__ua: not called CC=1 SCAR=6.9
79 0 my $ua = Mojo::UserAgent->new;
80 $ua->on(
81 start => sub {
82 0 ▶ my ( $ua, $tx ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 $tx->req->headers->header( 'Content-Type' => 'application/json' );
84 0 $tx->req->headers->header(
85 'X-TYPESENSE-API-KEY' => $self->api_key );
86 }
87 0 );
88 0 return $ua;
89 }
90  
91 1 has _url_base => (
92 is => 'ro',
93 isa => InstanceOf ['Mojo::URL'],
94 lazy => 1,
95 builder => 1,
96 );
97  
98 1 has use_https => (
99 is => 'ro',
100 isa => Bool,
101 required => 1,
102 default => 1,
103 );
104  
105 1 has api_key => (
106 is => 'ro',
107 isa => NonEmptyStr,
108 required => 1,
109 );
110  
111 1 has host => (
112 is => 'ro',
113 isa => NonEmptyStr,
114 required => 1,
115 );
116  
117 1 has port => (
118 is => 'ro',
119 isa => NonEmptyStr,
120 required => 1,
121 );
122  
123 sub _build__url_base {
124 0 ▶ my $self = shift;
Subroutine _build__url_base: not called CC=2 SCAR=17.9
125 0 my $url = Mojo::URL->new;
126 0 ▶ $url->scheme( $self->use_https ? 'https' : 'http' );
Branch: 1 branch
BranchTrueFalseTotal
$self->use_https ? : 0 0 0
127 0 $url->host( $self->host );
128 0 $url->port( $self->port );
129 0 return $url;
130 }
131  
132 sub _url {
133 0 ▶ my ( $self, $path ) = @_;
Subroutine _url: not called CC=1 SCAR=6.9
134 0 return $self->_url_base->clone->path( '/' . join( '/' => @$path ) );
135 }
136  
137 sub BUILD {
138 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
139 0 $self->assert_is_running;
140 }
141  
142 =head1 METHODS
143  
144 =head2 C<assert_is_running>
145  
146 $typesense->assert_is_running;
147  
148 This does nothing if we can connect to Typesense. Otherwise, this method will
149 C<croak> with a message explaining the error.
150  
151 =cut
152  
153 sub assert_is_running {
154 0 ▶ my $self = shift;
Subroutine assert_is_running: not called CC=1 SCAR=6.9
Pod documented
155 0 $self->_GET( path => ['health'] );
156 }
157  
158 =head2 C<get_collections>
159  
160 if ( my $collections = $typesense->get_collections ) {
161 # returns all collections
162 }
163 if ( my $collections = $typesense->get_collections($collection_name) ) {
164 # returns collection matching $collection_name, if any
165 }
166  
167 Response shown at L<https://typesense.org/docs/0.19.0/api/#retrieve-collection>
168  
169 =cut
170  
171 sub get_collections {
172 0 ▶ my ( $self, $collection ) = @_;
Subroutine get_collections: not called CC=2 SCAR=17.9
Pod documented
173 0 state $check = compile(Str);
174 0 ▶ my @collection = $check->( $collection // '' );
Condition0%0/2
$collection // ""
l!l
00
MC/DC0%0/1
$collection // ""
$collection
Truth table0%0/2
A
A= $collection
Aresult
11
00
175 0 return $self->_GET( path => [ 'collections', @collection ] );
176 }
177  
178 sub _GET {
179 0 ▶ my ( $self, %arg_for ) = @_;
Subroutine _GET: not called CC=2 SCAR=17.9
180 0 my $request = $arg_for{request};
181  
182 0 my $url = $self->_url( $arg_for{path} );
183  
184 0 ▶ my @args = $request ? ( form => $request ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$request ? : 0 0 0
185 0 my $tx = $self->_ua->get( $url, @args );
186 0 return $self->_check_for_failure( $tx, $arg_for{return_transaction} );
187 }
188  
189 =head2 C<delete_collection>
190  
191 my $response = $typesense->delete_collection($collection_name);
192  
193 Response shown at L<https://typesense.org/docs/0.19.0/api/#drop-collection>
194  
195 =cut
196  
197 sub delete_collection {
198 0 ▶ my ( $self, $collection ) = @_;
Subroutine delete_collection: not called CC=1 SCAR=6.9
Pod documented
199 0 state $check = compile(NonEmptyStr);
200 0 ($collection) = $check->($collection);
201 0 return $self->_DELETE( path => [ 'collections', $collection ] );
202 }
203  
204 sub _DELETE {
205 0 ▶ my ( $self, %arg_for ) = @_;
Subroutine _DELETE: not called CC=1 SCAR=6.9
206 0 my $url = $self->_url( $arg_for{path} );
207 0 my $tx = $self->_ua->delete($url);
208 0 return $self->_check_for_failure( $tx, $arg_for{return_transaction} );
209 }
210  
211 =head2 C<create_collection>
212  
213 my $collection = $typesense->create_collection(\%definition);
214  
215 Arguments and response as shown at
216 L<https://typesense.org/docs/0.19.0/api/#create-collection>
217  
218 =cut
219  
220 sub create_collection {
221 0 ▶ my ( $self, $collection_definition ) = @_;
Subroutine create_collection: not called CC=4 SCAR=30.0
Pod documented
222 0 state $check = compile(HashRef);
223 0 ($collection_definition) = $check->($collection_definition);
224 0 my $fields = $collection_definition->{fields};
225  
226 0 foreach my $field ( $fields->@* ) {
227 0 ▶ if ( exists $field->{facet} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"facet"}) 0 0 0
228 $field->{facet} =
229 0 ▶ $field->{facet} ? Mojo::JSON->true : Mojo::JSON->false;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'facet'} ? : 0 0 0
230 }
231 }
232  
233 0 return $self->_POST(
234 path => ['collections'],
235 request => $collection_definition
236 );
237 } ## end sub create_collection
238  
239 sub _POST {
240 0 ▶ my ( $self, %arg_for ) = @_;
Subroutine _POST: not called CC=4 SCAR=30.0
241 0 my $request = $arg_for{request};
242 0 my $url = $self->_url( $arg_for{path} );
243  
244 0 ▶ if ( my $query = $arg_for{query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = $arg_for{"query"}) 0 0 0
245 0 my $parameters = Mojo::Parameters->new(%$query);
246 0 $url .= "?$parameters";
247 }
248  
249 0 ▶ my @args = ref $request
Branch: 2 branches
BranchTrueFalseTotal
ref $request ? : 0 0 0
$request ? : 0 0 0
250 ? ( json => $request ) # data structures
251 : $request ? $request # raw POST content
252 : croak("POST $url not allowed without a request");
253  
254 0 my $tx = $self->_ua->post( $url, @args );
255 0 return $self->_check_for_failure( $tx, $arg_for{return_transaction} );
256 }
257  
258 =head2 C<create_document>
259  
260 my $document = $typesense->create_document($collection, \%data);
261  
262 Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#index-document>
263  
264 =cut
265  
266 sub create_document {
267 0 ▶ my ( $self, $collection, $document ) = @_;
Subroutine create_document: not called CC=1 SCAR=6.9
Pod documented
268 0 state $check = compile( NonEmptyStr, HashRef );
269 0 ( $collection, $document ) = $check->( $collection, $document );
270 0 return $self->_POST(
271 path => [ 'collections', $collection, 'documents' ],
272 request => $document
273 );
274 }
275  
276 =head2 C<upsert_document>
277  
278 my $document = $typesense->upsert_document($collection, \%data);
279  
280 Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#upsert>
281  
282 =cut
283  
284 sub upsert_document {
285 0 ▶ my ( $self, $collection, $document ) = @_;
Subroutine upsert_document: not called CC=1 SCAR=6.9
Pod documented
286 0 state $check = compile( NonEmptyStr, HashRef );
287 0 ( $collection, $document ) = $check->( $collection, $document );
288  
289 # XXX It's unclear to me how to have a request body and a query string at
290 # the same time with the Mojo::UserAgent.
291 0 return $self->_POST(
292 path => [ 'collections', $collection, 'documents' ],
293 request => $document,
294 query => { action => 'upsert' },
295 );
296 }
297  
298 =head2 C<update_document>
299  
300 my $document = $typesense->update_document($collection, $document_id, \%data);
301  
302 Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#update-document>
303  
304 =cut
305  
306 sub update_document {
307 0 ▶ my ( $self, $collection, $document_id, $updates ) = @_;
Subroutine update_document: not called CC=1 SCAR=6.9
Pod documented
308 0 state $check = compile( NonEmptyStr, NonEmptyStr, HashRef );
309 0 ( $collection, $document_id, $updates ) =
310 $check->( $collection, $document_id, $updates );
311 0 return $self->_make_request(
312 method => 'patch',
313 path => [ 'collections', $collection, 'documents', $document_id ],
314 request => $updates
315 );
316 }
317  
318 =head2 C<delete_document>
319  
320 my $document = $typesense->delete_document($collection_name, $document_id);
321  
322 Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#delete-document>
323  
324 =cut
325  
326 sub delete_document {
327 0 ▶ my ( $self, $collection, $document_id ) = @_;
Subroutine delete_document: not called CC=1 SCAR=6.9
Pod documented
328 0 state $check = compile( NonEmptyStr, NonEmptyStr );
329 0 ( $collection, $document_id ) = $check->( $collection, $document_id );
330 0 return $self->_DELETE(
331 path => [ 'collections', $collection, 'documents', $document_id ] );
332 }
333  
334 =head2 C<search>
335  
336 my $results = $typesense->search($collection_name, {q => 'London'});
337  
338 The parameters for C<$query> are defined at
339 L<https://typesense.org/docs/0.19.0/api/#search-collection>, as are the results.
340  
341 Unlike other methods, if we find nothing, we still return the data structure
342 (instead of C<undef> instead of a 404 exception).
343  
344 =cut
345  
346 sub search {
347 0 ▶ my ( $self, $collection, $query ) = @_;
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
348 0 state $check = compile( NonEmptyStr, HashRef );
349 0 ( $collection, $query ) = $check->( $collection, $query );
350  
351 0 ▶ unless ( exists $query->{q} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $query->{"q"}) 0 0 0
352 0 croak("Query parameter 'q' is required for searching");
353 }
354 0 ▶ unless ( exists $query->{query_by} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $query->{"query_by"}) 0 0 0
355 0 $query->{query_by} = 'search';
356 }
357 0 ▶ my $tx = $self->_GET(
Branch: 1 branch
BranchTrueFalseTotal
my $tx = $self->_GET("path", ["collections", $collection, "documents", "search"], "request", $query, "return_transaction", 1) or return 0 0 0
358 path => [ 'collections', $collection, 'documents', 'search' ],
359 request => $query,
360 return_transaction => 1,
361 ) or return;
362 0 my $response = $tx->res->json;
363 0 foreach my $hit ( $response->{hits}->@* ) {
364 0 ▶ if ( exists $hit->{document}{json} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hit->{"document"}{"json"}) 0 0 0
365 0 $hit->{document}{json} = decode_json( $hit->{document}{json} );
366 }
367 }
368 0 return $response;
369 } ## end sub search
370  
371 =head2 C<export_documents>
372  
373 my $export = $typesense->export_documents($collection_name);
374  
375 Response as shown at L<https://typesense.org/docs/0.19.0/api/#export-documents>
376  
377 (An arrayref of hashrefs)
378  
379 =cut
380  
381 sub export_documents {
382 0 ▶ my ( $self, $collection ) = @_;
Subroutine export_documents: not called CC=2 SCAR=17.9
Pod documented
383 0 state $check = compile(NonEmptyStr);
384 0 ($collection) = $check->($collection);
385 0 ▶ my $tx = $self->_GET(
Branch: 1 branch
BranchTrueFalseTotal
my $tx = $self->_GET("path", ["collections", $collection, "documents", "export"], "return_transaction", 1) or return 0 0 0
386 path => [ 'collections', $collection, 'documents', 'export' ],
387 return_transaction => 1
388 ) or return; # 404
389 0 return [ map { decode_json($_) } split /\n/ => $tx->res->body ];
390 }
391  
392 =head2 C<import_documents>
393  
394 my $response = $typesense->import_documents(
395 $collection_name,
396 $action,
397 \@documents,
398 );
399  
400 Response as shown at L<https://typesense.org/docs/0.19.0/api/#import-documents>
401  
402 C<$action> must be one of C<create>, C<update>, or C<upsert>.
403  
404 =cut
405  
406 sub import_documents {
407 0 ▶ my $self = shift;
Subroutine import_documents: not called CC=2 SCAR=17.9
Pod documented
408 0 state $check = compile(
409 NonEmptyStr,
410 Enum [qw/create upsert update/],
411 ArrayRef [HashRef],
412 );
413 0 my ( $collection, $action, $documents ) = $check->(@_);
414 0 my $request_body = join "\n" => map { encode_json($_) } @$documents;
415  
416 0 my $tx = $self->_POST(
417 path => [ 'collections', $collection, 'documents', "import" ],
418 request => $request_body,
419 query => { action => $action },
420 return_transaction => 1,
421 );
422 0 my $response = $tx->res->json;
423 0 ▶ if ( exists $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{"success"}) 0 0 0
424 0 $response->{success} += 0;
425 }
426 0 return $response;
427 }
428  
429 =head2 C<purge>
430  
431 $typesense->purge;
432  
433 Deletes everything from Typsense. B<Use with caution>!
434  
435 =cut
436  
437 sub purge {
438 0 ▶ my ($self) = @_;
Subroutine purge: not called CC=2 SCAR=17.9
Pod documented
439 0 my $collections = $self->get_collections;
440 0 foreach my $collection (@$collections) {
441 0 my $name = $collection->{name};
442 0 $self->delete_collection($name);
443 }
444 }
445  
446 sub _check_for_failure {
447 0 ▶ my ( $self, $tx, $return_transaction ) = @_;
Subroutine _check_for_failure: not called CC=6 SCAR=37.4
448 0 my $res = $tx->res;
449 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
450  
451 0 ▶ return if ( $res->code // 0 ) == 404;
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code // 0) == 404 0 0 0
Condition0%0/2
$res->code // 0
l!l
00
MC/DC0%0/1
$res->code // 0
$res->code
Truth table0%0/2
A
A= $res->code
Aresult
11
00
452 0 ▶ my $message = $res->message // '';
Condition0%0/2
$res->message // ""
l!l
00
MC/DC0%0/1
$res->message // ""
$res->message
Truth table0%0/2
A
A= $res->message
Aresult
11
00
453  
454 0 my $body = $res->body;
455 0 my $method = $tx->req->method;
456 0 my $url = $tx->req->url;
457 0 croak("'$method $url' failed: $message. $body");
458 }
459 0 ▶ return $return_transaction ? $tx : $tx->res->json;
Branch: 1 branch
BranchTrueFalseTotal
$return_transaction ? : 0 0 0
460 } ## end sub _check_for_failure
461  
462 =head1 AUTHOR
463  
464 Curtis "Ovid" Poe, C<< <ovid at allaroundtheworld.fr> >>
465  
466 =head1 BUGS
467  
468 Please report any bugs or feature requests to C<bug-search-typesense at rt.cpan.org>, or through
469 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Search-Typesense>. I will be notified, and then you'll
470 automatically be notified of progress on your bug as I make changes.
471  
472 =head1 SUPPORT
473  
474 You can find documentation for this module with the perldoc command.
475  
476 perldoc Search::Typesense
477  
478  
479 You can also look for information at:
480  
481 =over 4
482  
483 =item * Github Repo
484  
485 L<https://github.com/Ovid/Search-Typesense/>
486  
487 =item * Issue Tracker
488  
489 L<https://github.com/Ovid/Search-Typesense/issues>
490  
491 =item * Search CPAN
492  
493 L<https://metacpan.org/release/Search-Typesense>
494  
495 =back
496  
497 =head1 ACKNOWLEDGEMENTS
498  
499  
500 =head1 LICENSE AND COPYRIGHT
501  
502 This software is Copyright (c) 2021 by Curtis "Ovid" Poe.
503  
504 This is free software, licensed under:
505  
506 The Artistic License 2.0 (GPL Compatible)
507  
508 =cut
509  
510 1 1; # End of Search::Typesense
↑ Summary Typesense/Types.pm »