@@ -34,6 +34,8 @@ public function search(Search $search): Result
34
34
{
35
35
$ documents = [];
36
36
37
+ $ searchTerms = [];
38
+
37
39
/** @var Index $index */
38
40
foreach ([$ search ->index ] as $ index ) {
39
41
$ indexDocuments = MemoryStorage::getDocuments ($ index );
@@ -45,7 +47,7 @@ public function search(Search $search): Result
45
47
}
46
48
47
49
foreach ($ search ->filters as $ filter ) {
48
- $ indexDocuments = $ this ->filterDocuments ($ index , $ indexDocuments , $ filter );
50
+ $ indexDocuments = $ this ->filterDocuments ($ index , $ indexDocuments , $ filter, $ searchTerms );
49
51
}
50
52
51
53
foreach ($ indexDocuments as $ document ) {
@@ -66,8 +68,40 @@ public function search(Search $search): Result
66
68
67
69
$ documents = \array_slice ($ documents , $ search ->offset , $ search ->limit );
68
70
69
- $ generator = (function () use ($ documents ): \Generator {
71
+ $ generator = (function () use ($ documents, $ search , $ searchTerms ): \Generator {
70
72
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
+
71
105
yield $ document ;
72
106
}
73
107
});
@@ -80,10 +114,11 @@ public function search(Search $search): Result
80
114
81
115
/**
82
116
* @param array<array<string, mixed>> $documents
117
+ * @param string[] $searchTerms
83
118
*
84
119
* @return array<array<string, mixed>>
85
120
*/
86
- private function filterDocuments (Index $ index , array $ documents , object $ filter ): array
121
+ private function filterDocuments (Index $ index , array $ documents , object $ filter, array & $ searchTerms ): array
87
122
{
88
123
$ filteredDocuments = [];
89
124
@@ -99,6 +134,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
99
134
100
135
$ text = \json_encode ($ searchableDocument , \JSON_THROW_ON_ERROR );
101
136
$ terms = \explode (' ' , $ filter ->query );
137
+ $ searchTerms = \array_unique ([...$ searchTerms , ...$ terms ]);
102
138
103
139
foreach ($ terms as $ term ) {
104
140
if (!\str_contains ($ text , $ term )) {
@@ -285,7 +321,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
285
321
} elseif ($ filter instanceof Condition \AndCondition) {
286
322
$ subDocuments = [];
287
323
foreach ($ filter ->conditions as $ subFilter ) {
288
- $ subDocuments = [...$ subDocuments , ...$ this ->filterDocuments ($ index , [$ document ], $ subFilter )];
324
+ $ subDocuments = [...$ subDocuments , ...$ this ->filterDocuments ($ index , [$ document ], $ subFilter, $ searchTerms )];
289
325
}
290
326
291
327
if (\count ($ filter ->conditions ) !== \count ($ subDocuments )) {
@@ -294,7 +330,7 @@ private function filterDocuments(Index $index, array $documents, object $filter)
294
330
} elseif ($ filter instanceof Condition \OrCondition) {
295
331
$ subDocuments = [];
296
332
foreach ($ filter ->conditions as $ subFilter ) {
297
- $ subDocuments = [...$ subDocuments , ...$ this ->filterDocuments ($ index , [$ document ], $ subFilter )];
333
+ $ subDocuments = [...$ subDocuments , ...$ this ->filterDocuments ($ index , [$ document ], $ subFilter, $ searchTerms )];
298
334
}
299
335
300
336
if ([] === $ subDocuments ) {
0 commit comments