forked from acseo/TypesenseBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionClient.php
84 lines (68 loc) · 2.28 KB
/
CollectionClient.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
<?php
declare(strict_types=1);
namespace ACSEO\TypesenseBundle\Client;
use ACSEO\TypesenseBundle\Finder\TypesenseQuery;
class CollectionClient
{
private $client;
public function __construct(TypesenseClient $client)
{
$this->client = $client;
}
public function search(string $collectionName, TypesenseQuery $query)
{
if (!$this->client->isOperationnal()) {
return null;
}
return $this->client->collections[$collectionName]->documents->search($query->getParameters());
}
public function multiSearch(array $searchRequests, ?TypesenseQuery $commonSearchParams)
{
if (!$this->client->isOperationnal()) {
return null;
}
$searches = [];
foreach ($searchRequests as $sr) {
if (!$sr instanceof TypesenseQuery) {
throw new \Exception('searchRequests must be an array of TypesenseQuery objects');
}
if (!$sr->hasParameter('collection')) {
throw new \Exception('TypesenseQuery must have the key : `collection` in order to perform multiSearch');
}
$searches[] = $sr->getParameters();
}
return $this->client->multiSearch->perform(
[
'searches' => $searches,
],
$commonSearchParams ? $commonSearchParams->getParameters() : []
);
}
public function list()
{
if (!$this->client->isOperationnal()) {
return null;
}
return $this->client->collections->retrieve();
}
public function create($name, $fields, $defaultSortingField, array $tokenSeparators, array $symbolsToIndex)
{
if (!$this->client->isOperationnal()) {
return null;
}
$this->client->collections->create([
'name' => $name,
'fields' => $fields,
'default_sorting_field' => $defaultSortingField,
'token_separators' => $tokenSeparators,
'symbols_to_index' => $symbolsToIndex,
]);
}
public function delete(string $name)
{
if (!$this->client->isOperationnal()) {
return null;
}
return $this->client->collections[$name]->delete();
}
}