line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Search::Typesense::Collection; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
59
|
use v5.16.0; |
|
4
|
|
|
|
|
12
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
19
|
use Moo; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
24
|
|
6
|
|
|
|
|
|
|
with qw(Search::Typesense::Role::Request); |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
1639
|
use Carp 'croak'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
282
|
|
9
|
4
|
|
|
|
|
36
|
use Search::Typesense::Types qw( |
10
|
|
|
|
|
|
|
HashRef |
11
|
|
|
|
|
|
|
InstanceOf |
12
|
|
|
|
|
|
|
NonEmptyStr |
13
|
|
|
|
|
|
|
Str |
14
|
|
|
|
|
|
|
compile |
15
|
4
|
|
|
4
|
|
28
|
); |
|
4
|
|
|
|
|
8
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Search::Typesense::Collection - CRUD for Typesense collections |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $typesense = Search::Typesense->new( |
24
|
|
|
|
|
|
|
host => $host, |
25
|
|
|
|
|
|
|
api_key => $key, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
my $collections = $typesense->collections; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
The instantiation of this module is for internal use only. The methods are |
30
|
|
|
|
|
|
|
public. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 C<get> |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
if ( my $collections = $typesense->collections->get ) { |
35
|
|
|
|
|
|
|
# returns all collections |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
if ( my $collections = $typesense->collections->get($collection_name) ) { |
38
|
|
|
|
|
|
|
# returns collection matching $collection_name, if any |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Response shown at L<https://typesense.org/docs/0.19.0/api/#retrieve-collection> |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 C<search> |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $results = $typesense->collections->search($collection_name, {q => 'London'}); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
The parameters for C<$query> are defined at |
52
|
|
|
|
|
|
|
L<https://typesense.org/docs/0.19.0/api/#search-collection>, as are the results. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Unlike other methods, if we find nothing, we still return the data structure |
55
|
|
|
|
|
|
|
(instead of C<undef> instead of a 404 exception). |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub search { |
60
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection, $query ) = @_; |
61
|
0
|
|
|
|
|
|
state $check = compile( NonEmptyStr, HashRef ); |
62
|
0
|
|
|
|
|
|
( $collection, $query ) = $check->( $collection, $query ); |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
unless ( exists $query->{q} ) { |
65
|
0
|
|
|
|
|
|
croak("Query parameter 'q' is required for searching"); |
66
|
|
|
|
|
|
|
} |
67
|
0
|
0
|
|
|
|
|
my $tx = $self->_GET( |
68
|
|
|
|
|
|
|
path => [ 'collections', $collection, 'documents', 'search' ], |
69
|
|
|
|
|
|
|
query => $query, |
70
|
|
|
|
|
|
|
return_transaction => 1, |
71
|
|
|
|
|
|
|
) or return; |
72
|
0
|
|
|
|
|
|
return $tx->res->json; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get { |
76
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection ) = @_; |
77
|
0
|
|
|
|
|
|
state $check = compile(Str); |
78
|
0
|
|
0
|
|
|
|
my @collection = $check->( $collection // '' ); |
79
|
0
|
|
|
|
|
|
return $self->_GET( path => [ 'collections', @collection ] ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 C<create> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $collection = $typesense->collections->create(\%definition); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Arguments and response as shown at |
87
|
|
|
|
|
|
|
L<https://typesense.org/docs/0.19.0/api/#create-collection> |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub create { |
92
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection_definition ) = @_; |
93
|
0
|
|
|
|
|
|
state $check = compile(HashRef); |
94
|
0
|
|
|
|
|
|
($collection_definition) = $check->($collection_definition); |
95
|
0
|
|
|
|
|
|
my $fields = $collection_definition->{fields}; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
foreach my $field (@$fields) { |
98
|
0
|
0
|
|
|
|
|
if ( exists $field->{facet} ) { |
99
|
|
|
|
|
|
|
$field->{facet} |
100
|
0
|
0
|
|
|
|
|
= $field->{facet} ? Mojo::JSON->true : Mojo::JSON->false; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
return $self->_POST( |
105
|
|
|
|
|
|
|
path => ['collections'], |
106
|
|
|
|
|
|
|
body => $collection_definition |
107
|
|
|
|
|
|
|
); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 C<delete> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $response = $typesense->collections->delete($collection_name); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Response shown at L<https://typesense.org/docs/0.19.0/api/#drop-collection> |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub delete { |
119
|
0
|
|
|
0
|
1
|
|
my ( $self, $collection ) = @_; |
120
|
0
|
|
|
|
|
|
state $check = compile(NonEmptyStr); |
121
|
0
|
|
|
|
|
|
($collection) = $check->($collection); |
122
|
0
|
|
|
|
|
|
return $self->_DELETE( path => [ 'collections', $collection ] ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 C<delete_all> |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$typesense->collections->delete_all; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Deletes everything from Typesense. B<Use with caution>! |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub delete_all { |
134
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
135
|
0
|
|
|
|
|
|
my $collections = $self->get; |
136
|
0
|
|
|
|
|
|
foreach my $collection (@$collections) { |
137
|
0
|
|
|
|
|
|
my $name = $collection->{name}; |
138
|
0
|
|
|
|
|
|
$self->delete($name); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |
143
|
|
|
|
|
|
|
|