-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
TypesenseSearcher.php
139 lines (117 loc) · 5.04 KB
/
TypesenseSearcher.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
<?php
declare(strict_types=1);
/*
* This file is part of the Schranz Search package.
*
* (c) Alexander Schranz <alexander@sulu.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Schranz\Search\SEAL\Adapter\Typesense;
use Schranz\Search\SEAL\Adapter\SearcherInterface;
use Schranz\Search\SEAL\Marshaller\Marshaller;
use Schranz\Search\SEAL\Schema\Index;
use Schranz\Search\SEAL\Search\Condition;
use Schranz\Search\SEAL\Search\Result;
use Schranz\Search\SEAL\Search\Search;
use Typesense\Client;
use Typesense\Exceptions\ObjectNotFound;
final class TypesenseSearcher implements SearcherInterface
{
private readonly Marshaller $marshaller;
public function __construct(
private readonly Client $client,
) {
$this->marshaller = new Marshaller(dateAsInteger: true);
}
public function search(Search $search): Result
{
// optimized single document query
if (
1 === \count($search->indexes)
&& 1 === \count($search->filters)
&& $search->filters[0] instanceof Condition\IdentifierCondition
&& 0 === $search->offset
&& 1 === $search->limit
) {
try {
$data = $this->client->collections[$search->indexes[\array_key_first($search->indexes)]->name]->documents[$search->filters[0]->identifier]->retrieve();
} catch (ObjectNotFound) {
return new Result(
$this->hitsToDocuments($search->indexes, []),
0,
);
}
return new Result(
$this->hitsToDocuments($search->indexes, [['document' => $data]]),
1,
);
}
if (1 !== \count($search->indexes)) {
throw new \RuntimeException('Typesense does not yet support search multiple indexes: TODO');
}
$index = $search->indexes[\array_key_first($search->indexes)];
$searchParams = [
'q' => '',
'query_by' => \implode(',', $index->searchableFields),
];
$filters = [];
foreach ($search->filters as $filter) {
match (true) {
$filter instanceof Condition\IdentifierCondition => $filters[] = 'id:=' . $this->escapeFilterValue($filter->identifier),
$filter instanceof Condition\SearchCondition => $searchParams['q'] = $filter->query,
$filter instanceof Condition\EqualCondition => $filters[] = $filter->field . ':=' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\NotEqualCondition => $filters[] = $filter->field . ':!=' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanCondition => $filters[] = $filter->field . ':>' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanEqualCondition => $filters[] = $filter->field . ':>=' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanCondition => $filters[] = $filter->field . ':<' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanEqualCondition => $filters[] = $filter->field . ':<=' . $this->escapeFilterValue($filter->value),
default => throw new \LogicException($filter::class . ' filter not implemented.'),
};
}
if ([] !== $filters) {
$searchParams['filter_by'] = \implode(' && ', $filters);
}
if (0 !== $search->offset) {
$searchParams['page'] = ($search->offset / $search->limit) + 1;
}
if ($search->limit) {
$searchParams['per_page'] = $search->limit;
}
$sortBys = [];
foreach ($search->sortBys as $field => $direction) {
$sortBys[] = $field . ':' . $direction;
}
if ([] !== $sortBys) {
$searchParams['sort_by'] = \implode(',', $sortBys);
}
$data = $this->client->collections[$index->name]->documents->search($searchParams);
return new Result(
$this->hitsToDocuments($search->indexes, $data['hits']),
$data['found'] ?? null,
);
}
/**
* @param Index[] $indexes
* @param iterable<array<string, mixed>> $hits
*
* @return \Generator<int, array<string, mixed>>
*/
private function hitsToDocuments(array $indexes, iterable $hits): \Generator
{
$index = $indexes[\array_key_first($indexes)];
/** @var array{document: array<string, mixed>} $hit */
foreach ($hits as $hit) {
yield $this->marshaller->unmarshall($index->fields, $hit['document']);
}
}
private function escapeFilterValue(string|int|float|bool $value): string
{
return match (true) {
\is_string($value) => '"' . \addcslashes($value, '"&') . '"', // TODO escape?
\is_bool($value) => $value ? 'true' : 'false',
default => (string) $value,
};
}
}