Skip to content

Commit e9e6df4

Browse files
Add highlight implementation for typesense adapter (#354)
1 parent 2b7ad9a commit e9e6df4

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

packages/seal-typesense-adapter/src/TypesenseSearcher.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function search(Search $search): Result
5151
$data = $this->client->collections[$search->index->name]->documents[$search->filters[0]->identifier]->retrieve();
5252
} catch (ObjectNotFound) {
5353
return new Result(
54-
$this->hitsToDocuments($search->index, []),
54+
$this->hitsToDocuments($search->index, [], []),
5555
0,
5656
);
5757
}
5858

5959
return new Result(
60-
$this->hitsToDocuments($search->index, [['document' => $data]]),
60+
$this->hitsToDocuments($search->index, [['document' => $data]], []),
6161
1,
6262
);
6363
}
@@ -95,24 +95,55 @@ public function search(Search $search): Result
9595
$searchParams['sort_by'] = \implode(',', $sortBys);
9696
}
9797

98+
if ([] !== $search->highlightFields) {
99+
$searchParams['highlight_fields'] = \implode(', ', $search->highlightFields);
100+
$searchParams['highlight_start_tag'] = $search->highlightPreTag;
101+
$searchParams['highlight_end_tag'] = $search->highlightPostTag;
102+
}
103+
98104
$data = $this->client->collections[$search->index->name]->documents->search($searchParams);
99105

100106
return new Result(
101-
$this->hitsToDocuments($search->index, $data['hits']),
107+
$this->hitsToDocuments($search->index, $data['hits'], $search->highlightFields),
102108
$data['found'] ?? null,
103109
);
104110
}
105111

106112
/**
107113
* @param iterable<array<string, mixed>> $hits
114+
* @param array<string> $highlightFields
108115
*
109116
* @return \Generator<int, array<string, mixed>>
110117
*/
111-
private function hitsToDocuments(Index $index, iterable $hits): \Generator
118+
private function hitsToDocuments(Index $index, iterable $hits, array $highlightFields): \Generator
112119
{
113-
/** @var array{document: array<string, mixed>} $hit */
120+
/** @var array{document: array<string, mixed>, highlight?: array<string, array{snippet: string}>} $hit */
114121
foreach ($hits as $hit) {
115-
yield $this->marshaller->unmarshall($index->fields, $hit['document']);
122+
$document = $this->marshaller->unmarshall($index->fields, $hit['document']);
123+
124+
if ([] === $highlightFields) {
125+
yield $document;
126+
127+
continue;
128+
}
129+
130+
$document['_formatted'] ??= [];
131+
132+
\assert(
133+
\is_array($document['_formatted']),
134+
'Document with key "_formatted" expected to be array.',
135+
);
136+
137+
foreach ($highlightFields as $highlightField) {
138+
\assert(
139+
isset($hit['highlight'][$highlightField]['snippet']),
140+
'Expected highlight field to be set.',
141+
);
142+
143+
$document['_formatted'][$highlightField] = $hit['highlight'][$highlightField]['snippet'];
144+
}
145+
146+
yield $document;
116147
}
117148
}
118149

0 commit comments

Comments
 (0)