forked from typesense/typesense-php
-
Notifications
You must be signed in to change notification settings - Fork 2
/
alias_operations.php
142 lines (138 loc) · 4.32 KB
/
alias_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
<?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->aliases['books']->delete());
} catch (Exception $e) {
// Don't error out if the collection was not found
}
try {
print_r($client->collections['books_january']->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_january',
'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 "--------Create Collection Alias-------\n";
print_r(
$client->aliases->upsert(
'books',
[
'collection_name' => 'books_january',
]
)
);
echo "--------Create Collection Alias-------\n";
echo "\n";
echo "--------Create Document on Alias-------\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 on Alias-------\n";
echo "\n";
echo "--------Search Document on Alias-------\n";
print_r(
$client->collections['books']->documents->search(
[
'q' => 'hunger',
'query_by' => 'title',
'sort_by' => 'ratings_count:desc',
]
)
);
echo "--------Search Document on Alias-------\n";
echo "\n";
echo "--------Retrieve All Aliases-------\n";
print_r($client->aliases->retrieve());
echo "--------Retrieve All Aliases-------\n";
echo "\n";
echo "--------Retrieve All Alias Documents-------\n";
print_r($client->aliases['books']->retrieve());
echo "--------Retrieve All Alias Documents-------\n";
echo "\n";
echo "--------Delete Alias-------\n";
print_r($client->aliases['books']->delete());
echo "--------Delete Alias-------\n";
echo "\n";
} catch (Exception $e) {
echo $e->getMessage();
}