forked from ashrafkamarudin/typesense-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_operations.php
232 lines (223 loc) · 7.55 KB
/
collection_operations.php
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
<?php
/** @noinspection ForgottenDebugOutputInspection */
include '../vendor/autoload.php';
use Symfony\Component\HttpClient\HttplugClient;
use Typesense\Client;
try {
$client = new Client(
[
'api_key' => 'xyz',
'nodes' => [
[
'host' => 'localhost',
'port' => '8108',
'protocol' => 'http',
],
],
'client' => new HttplugClient(),
]
);
echo '<pre>';
try {
print_r($client->collections['books']->delete());
} catch (Exception $e) {
// Don't error out if the collection was not found
}
echo "--------Create Collection-------\n";
print_r(
$client->collections->create(
[
'name' => 'books',
'fields' => [
[
'name' => 'title',
'type' => 'string',
],
[
'name' => 'authors',
'type' => 'string[]',
],
[
'name' => 'authors_facet',
'type' => 'string[]',
'facet' => true,
],
[
'name' => 'publication_year',
'type' => 'int32',
],
[
'name' => 'publication_year_facet',
'type' => 'string',
'facet' => true,
],
[
'name' => 'ratings_count',
'type' => 'int32',
],
[
'name' => 'average_rating',
'type' => 'float',
],
[
'name' => 'image_url',
'type' => 'string',
],
],
'default_sorting_field' => 'ratings_count',
]
)
);
echo "--------Create Collection-------\n";
echo "\n";
echo "--------Retrieve Collection-------\n";
print_r($client->collections['books']->retrieve());
echo "--------Retrieve Collection-------\n";
echo "\n";
echo "--------Retrieve All Collections-------\n";
print_r($client->collections->retrieve());
echo "--------Retrieve All Collections-------\n";
echo "\n";
echo "--------Create Document-------\n";
print_r(
$client->collections['books']->documents->create(
[
'id' => '1',
'original_publication_year' => 2008,
'authors' => [
'Suzanne Collins',
],
'average_rating' => 4.34,
'publication_year' => 2008,
'publication_year_facet' => '2008',
'authors_facet' => [
'Suzanne Collins',
],
'title' => 'The Hunger Games',
'image_url' => 'https://images.gr-assets.com/books/1447303603m/2767052.jpg',
'ratings_count' => 4780653,
]
)
);
echo "--------Create Document-------\n";
echo "\n";
echo "--------Upsert Document-------\n";
print_r(
$client->collections['books']->documents->upsert(
[
'id' => '1',
'original_publication_year' => 2008,
'authors' => [
'Suzanne Collins',
],
'average_rating' => 4.6,
'publication_year' => "2008",
'publication_year_facet' => '2008',
'authors_facet' => [
'Suzanne Collins',
],
'title' => 'The Hunger Games',
'image_url' => 'https://images.gr-assets.com/books/1447303603m/2767052.jpg',
'ratings_count' => 4780653,
],
[
'dirty_values' => 'coerce_or_reject',
]
)
);
echo "--------Upsert Document-------\n";
echo "\n";
echo "--------Export Documents-------\n";
$exportedDocStrs = $client->collections['books']->documents->export(["exclude_fields" => "authors_facet"]);
print_r($exportedDocStrs);
echo "--------Export Documents-------\n";
echo "\n";
echo "--------Update Single Document-------\n";
print_r($client->collections['books']->documents['1']->update([
'average_rating' => 4.5,
]));
echo "--------Update Single Document-------\n";
echo "\n";
echo "--------Fetch Single Document-------\n";
print_r($client->collections['books']->documents['1']->retrieve());
echo "--------Fetch Single Document-------\n";
echo "\n";
echo "--------Search Document-------\n";
print_r(
$client->collections['books']->documents->search(
[
'q' => 'hunger',
'query_by' => 'title',
'sort_by' => 'ratings_count:desc',
]
)
);
echo "--------Search Document-------\n";
echo "\n";
echo "--------Multi search-------\n";
print_r(
$client->multiSearch->perform(
[
'searches' => [
[
'q' => 'hunger',
'sort_by' => 'ratings_count:desc',
],
[
'q' => 'game',
'sort_by' => 'ratings_count:asc',
]
]
],
[
'query_by' => 'title',
'collection' => 'books'
]
)
);
echo "--------Multi Search-------\n";
echo "\n";
echo "--------Delete Document-------\n";
print_r($client->collections['books']->documents['1']->delete());
echo "--------Delete Document-------\n";
echo "\n";
echo "--------Import Documents-------\n";
$docsToImport = [];
$exportedDocStrsArray = explode('\n', $exportedDocStrs);
foreach ($exportedDocStrsArray as $exportedDocStr) {
$docsToImport[] = json_decode($exportedDocStr, true);
}
$importRes =
$client->collections['books']->documents->import($docsToImport);
print_r($importRes);
// Or if you have documents in JSONL format, and want to save the overhead of parsing JSON,
// you can also pass in a JSONL string of documents
// $client->collections['books']->documents->import($exportedDocStrsArray);
echo "--------Import Documents-------\n";
echo "\n";
echo "--------Upsert Documents-------\n";
$upsertRes =
$client->collections['books']->documents->import($docsToImport, [
'action' => 'upsert'
]);
print_r($upsertRes);
echo "--------Upsert Documents-------\n";
echo "\n";
echo "--------Update Documents-------\n";
$upsertRes =
$client->collections['books']->documents->import($docsToImport, [
'action' => 'update'
]);
print_r($upsertRes);
echo "--------Upsert Documents-------\n";
echo "\n";
echo "--------Bulk Delete Documents-------\n";
print_r($client->collections['books']->documents->delete(['filter_by' => 'publication_year:=2008']));
echo "--------Bulk Delete Documents-------\n";
echo "\n";
echo "--------Delete Collection-------\n";
print_r($client->collections['books']->delete());
echo "--------Delete Collection-------\n";
} catch (Exception $e) {
echo $e->getMessage();
}