Skip to content

Commit 1115186

Browse files
Add highlight implementation for memory adapter (#351)
1 parent f318c39 commit 1115186

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/MemorySearcher.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function search(Search $search): Result
3434
{
3535
$documents = [];
3636

37+
$searchTerms = [];
38+
3739
/** @var Index $index */
3840
foreach ([$search->index] as $index) {
3941
$indexDocuments = MemoryStorage::getDocuments($index);
@@ -45,7 +47,7 @@ public function search(Search $search): Result
4547
}
4648

4749
foreach ($search->filters as $filter) {
48-
$indexDocuments = $this->filterDocuments($index, $indexDocuments, $filter);
50+
$indexDocuments = $this->filterDocuments($index, $indexDocuments, $filter, $searchTerms);
4951
}
5052

5153
foreach ($indexDocuments as $document) {
@@ -66,8 +68,40 @@ public function search(Search $search): Result
6668

6769
$documents = \array_slice($documents, $search->offset, $search->limit);
6870

69-
$generator = (function () use ($documents): \Generator {
71+
$generator = (function () use ($documents, $search, $searchTerms): \Generator {
7072
foreach ($documents as $document) {
73+
foreach ($search->highlightFields as $highlightField) {
74+
$highlightFieldContent = \json_encode($document[$highlightField], \JSON_THROW_ON_ERROR);
75+
foreach ($searchTerms as $searchTerm) {
76+
$highlightFieldContent = \str_replace(
77+
$searchTerm,
78+
$search->highlightPreTag . $searchTerm . $search->highlightPostTag,
79+
$highlightFieldContent,
80+
);
81+
}
82+
83+
$highlightFieldContent = \str_replace(
84+
$search->highlightPostTag . $search->highlightPostTag,
85+
'',
86+
$highlightFieldContent,
87+
);
88+
89+
$highlightFieldContent = \str_replace(
90+
$search->highlightPostTag . ' ' . $search->highlightPostTag,
91+
' ',
92+
$highlightFieldContent,
93+
);
94+
95+
$document['_formatted'] ??= [];
96+
97+
\assert(
98+
\is_array($document['_formatted']),
99+
'Document with key "_formatted" expected to be array.',
100+
);
101+
102+
$document['_formatted'][$highlightField] = \json_decode($highlightFieldContent, true, 512, \JSON_THROW_ON_ERROR);
103+
}
104+
71105
yield $document;
72106
}
73107
});
@@ -80,10 +114,11 @@ public function search(Search $search): Result
80114

81115
/**
82116
* @param array<array<string, mixed>> $documents
117+
* @param string[] $searchTerms
83118
*
84119
* @return array<array<string, mixed>>
85120
*/
86-
private function filterDocuments(Index $index, array $documents, object $filter): array
121+
private function filterDocuments(Index $index, array $documents, object $filter, array &$searchTerms): array
87122
{
88123
$filteredDocuments = [];
89124

@@ -99,6 +134,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
99134

100135
$text = \json_encode($searchableDocument, \JSON_THROW_ON_ERROR);
101136
$terms = \explode(' ', $filter->query);
137+
$searchTerms = \array_unique([...$searchTerms, ...$terms]);
102138

103139
foreach ($terms as $term) {
104140
if (!\str_contains($text, $term)) {
@@ -285,7 +321,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
285321
} elseif ($filter instanceof Condition\AndCondition) {
286322
$subDocuments = [];
287323
foreach ($filter->conditions as $subFilter) {
288-
$subDocuments = [...$subDocuments, ...$this->filterDocuments($index, [$document], $subFilter)];
324+
$subDocuments = [...$subDocuments, ...$this->filterDocuments($index, [$document], $subFilter, $searchTerms)];
289325
}
290326

291327
if (\count($filter->conditions) !== \count($subDocuments)) {
@@ -294,7 +330,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
294330
} elseif ($filter instanceof Condition\OrCondition) {
295331
$subDocuments = [];
296332
foreach ($filter->conditions as $subFilter) {
297-
$subDocuments = [...$subDocuments, ...$this->filterDocuments($index, [$document], $subFilter)];
333+
$subDocuments = [...$subDocuments, ...$this->filterDocuments($index, [$document], $subFilter, $searchTerms)];
298334
}
299335

300336
if ([] === $subDocuments) {

0 commit comments

Comments
 (0)