@@ -64,13 +64,13 @@ public function search(Search $search): Result
64
64
}
65
65
66
66
return new Result (
67
- $ this ->hitsToDocuments ($ search ->index , []),
67
+ $ this ->hitsToDocuments ($ search ->index , [], [] ),
68
68
0 ,
69
69
);
70
70
}
71
71
72
72
return new Result (
73
- $ this ->hitsToDocuments ($ search ->index , [$ searchResult ]),
73
+ $ this ->hitsToDocuments ($ search ->index , [$ searchResult ], [] ),
74
74
1 ,
75
75
);
76
76
}
@@ -99,6 +99,20 @@ public function search(Search $search): Result
99
99
$ body ['size ' ] = $ search ->limit ;
100
100
}
101
101
102
+ if ([] !== $ search ->highlightFields ) {
103
+ $ highlightFields = [];
104
+ foreach ($ search ->highlightFields as $ highlightField ) {
105
+ $ highlightFields [$ highlightField ] = [
106
+ 'pre_tags ' => [$ search ->highlightPreTag ],
107
+ 'post_tags ' => [$ search ->highlightPostTag ],
108
+ ];
109
+ }
110
+
111
+ $ body ['highlight ' ] = [
112
+ 'fields ' => $ highlightFields ,
113
+ ];
114
+ }
115
+
102
116
/** @var Elasticsearch $response */
103
117
$ response = $ this ->client ->search ([
104
118
'index ' => $ search ->index ->name ,
@@ -118,21 +132,48 @@ public function search(Search $search): Result
118
132
$ searchResult = $ response ->asArray ();
119
133
120
134
return new Result (
121
- $ this ->hitsToDocuments ($ search ->index , $ searchResult ['hits ' ]['hits ' ]),
135
+ $ this ->hitsToDocuments ($ search ->index , $ searchResult ['hits ' ]['hits ' ], $ search -> highlightFields ),
122
136
$ searchResult ['hits ' ]['total ' ]['value ' ],
123
137
);
124
138
}
125
139
126
140
/**
127
141
* @param array<array<string, mixed>> $hits
142
+ * @param array<string> $highlightFields
128
143
*
129
144
* @return \Generator<int, array<string, mixed>>
130
145
*/
131
- private function hitsToDocuments (Index $ index , array $ hits ): \Generator
146
+ private function hitsToDocuments (Index $ index , array $ hits, array $ highlightFields ): \Generator
132
147
{
133
- /** @var array{_index: string, _source: array<string, mixed>} $hit */
148
+ /** @var array{_index: string, _source: array<string, mixed>, highlight?: mixed } $hit */
134
149
foreach ($ hits as $ hit ) {
135
- yield $ this ->marshaller ->unmarshall ($ index ->fields , $ hit ['_source ' ]);
150
+ $ document = $ this ->marshaller ->unmarshall ($ index ->fields , $ hit ['_source ' ]);
151
+
152
+ if ([] === $ highlightFields ) {
153
+ yield $ document ;
154
+
155
+ continue ;
156
+ }
157
+
158
+ $ document ['_formatted ' ] ??= [];
159
+
160
+ \assert (
161
+ \is_array ($ document ['_formatted ' ]),
162
+ 'Document with key "_formatted" expected to be array. ' ,
163
+ );
164
+
165
+ foreach ($ highlightFields as $ highlightField ) {
166
+ \assert (
167
+ isset ($ hit ['highlight ' ])
168
+ && \is_array ($ hit ['highlight ' ])
169
+ && isset ($ hit ['highlight ' ][$ highlightField ])
170
+ && \is_array ($ hit ['highlight ' ][$ highlightField ]),
171
+ );
172
+
173
+ $ document ['_formatted ' ][$ highlightField ] = $ hit ['highlight ' ][$ highlightField ][0 ] ?? null ;
174
+ }
175
+
176
+ yield $ document ;
136
177
}
137
178
}
138
179
0 commit comments