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