22
33namespace BoxedCode \Laravel \Scout ;
44
5- use Laravel \Scout \Builder ;
6- use Laravel \Scout \Engines \Engine ;
75use Elasticsearch \Client ;
86use Illuminate \Database \Eloquent \Collection ;
7+ use Laravel \Scout \Builder ;
8+ use Laravel \Scout \Engines \Engine ;
99
1010class ElasticsearchEngine extends Engine
11- {
11+ {
1212 /**
1313 * Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
1414 *
@@ -19,7 +19,8 @@ class ElasticsearchEngine extends Engine
1919 /**
2020 * Create a new engine instance.
2121 *
22- * @param \Elasticsearch\Client $elastic
22+ * @param \Elasticsearch\Client $elastic
23+ *
2324 * @return void
2425 */
2526 public function __construct (Client $ elastic )
@@ -30,30 +31,30 @@ public function __construct(Client $elastic)
3031 /**
3132 * Update the given model in the index.
3233 *
33- * @param Collection $models
34+ * @param Collection $models
35+ *
3436 * @return void
3537 */
3638 public function update ($ models )
3739 {
3840 $ params ['body ' ] = [];
3941
40- $ models ->each (function ($ model ) use (&$ params )
41- {
42+ $ models ->each (function ($ model ) use (&$ params ) {
4243 $ params ['body ' ][] = [
4344 'update ' => [
44- '_id ' => $ model ->getKey (),
45+ '_id ' => $ model ->getKey (),
4546 '_index ' => $ model ->searchableAs (),
4647 /**
47- * @deprecated Document mapping types scheduled deprecated in
48+ * @deprecated Document mapping types scheduled deprecated in
4849 * elasticsearch 6.0 will be removed in 8.0.
4950 * https://bit.ly/2TZVZvq
5051 */
5152 '_type ' => $ model ->searchableAs (),
52- ]
53+ ],
5354 ];
5455 $ params ['body ' ][] = [
55- 'doc ' => $ model ->toSearchableArray (),
56- 'doc_as_upsert ' => true
56+ 'doc ' => $ model ->toSearchableArray (),
57+ 'doc_as_upsert ' => true ,
5758 ];
5859 });
5960
@@ -63,26 +64,26 @@ public function update($models)
6364 /**
6465 * Remove the given model from the index.
6566 *
66- * @param Collection $models
67+ * @param Collection $models
68+ *
6769 * @return void
6870 */
6971 public function delete ($ models )
7072 {
7173 $ params ['body ' ] = [];
7274
73- $ models ->each (function ($ model ) use (&$ params )
74- {
75+ $ models ->each (function ($ model ) use (&$ params ) {
7576 $ params ['body ' ][] = [
7677 'delete ' => [
77- '_id ' => $ model ->getKey (),
78+ '_id ' => $ model ->getKey (),
7879 '_index ' => $ model ->searchableAs (),
7980 /**
80- * @deprecated Document mapping types scheduled deprecated in
81+ * @deprecated Document mapping types scheduled deprecated in
8182 * elasticsearch 6.0 will be removed in 8.0.
8283 * https://bit.ly/2TZVZvq
8384 */
8485 '_type ' => $ model ->searchableAs (),
85- ]
86+ ],
8687 ];
8788 });
8889
@@ -92,51 +93,54 @@ public function delete($models)
9293 /**
9394 * Perform the given search on the engine.
9495 *
95- * @param Builder $builder
96+ * @param Builder $builder
97+ *
9698 * @return mixed
9799 */
98100 public function search (Builder $ builder )
99101 {
100102 return $ this ->performSearch ($ builder , array_filter ([
101103 'numericFilters ' => $ this ->filters ($ builder ),
102- 'size ' => $ builder ->limit ,
104+ 'size ' => $ builder ->limit ,
103105 ]));
104106 }
105107
106108 /**
107109 * Perform the given search on the engine.
108110 *
109- * @param Builder $builder
110- * @param int $perPage
111- * @param int $page
111+ * @param Builder $builder
112+ * @param int $perPage
113+ * @param int $page
114+ *
112115 * @return mixed
113116 */
114117 public function paginate (Builder $ builder , $ perPage , $ page )
115118 {
116119 $ result = $ this ->performSearch ($ builder , [
117120 'numericFilters ' => $ this ->filters ($ builder ),
118- 'from ' => (($ page * $ perPage ) - $ perPage ),
119- 'size ' => $ perPage ,
121+ 'from ' => (($ page * $ perPage ) - $ perPage ),
122+ 'size ' => $ perPage ,
120123 ]);
121124
122- $ result ['nbPages ' ] = $ result ['hits ' ]['total ' ]/ $ perPage ;
125+ $ result ['nbPages ' ] = $ result ['hits ' ]['total ' ] / $ perPage ;
123126
124127 return $ result ;
125128 }
126129
127130 /**
128131 * Perform the given search on the engine.
129132 *
130- * @param Builder $builder
131- * @param array $options
133+ * @param Builder $builder
134+ * @param array $options
135+ *
132136 * @return mixed
133137 */
134138 protected function performSearch (Builder $ builder , array $ options = [])
135139 {
136140 $ params = [
137141 'index ' => $ builder ->model ->searchableAs (),
138142 /**
139- * @deprecated Document mapping types scheduled deprecated in
143+ * @deprecated Document mapping types scheduled deprecated in
140144 * elasticsearch 6.0 will be removed in 8.0.
141145 * https://bit.ly/2TZVZvq
142146 */
@@ -145,14 +149,14 @@ protected function performSearch(Builder $builder, array $options = [])
145149 'query ' => [
146150 'bool ' => [
147151 'must ' => [
148- [" simple_query_string " => [
149- " query " => $ builder ->query ,
150- " default_operator " => " and "
151- ]]
152- ]
153- ]
154- ]
155- ]
152+ [' simple_query_string ' => [
153+ ' query ' => $ builder ->query ,
154+ ' default_operator ' => ' and ' ,
155+ ]],
156+ ],
157+ ],
158+ ],
159+ ],
156160 ];
157161
158162 if ($ sort = $ this ->sort ($ builder )) {
@@ -168,8 +172,10 @@ protected function performSearch(Builder $builder, array $options = [])
168172 }
169173
170174 if (isset ($ options ['numericFilters ' ]) && count ($ options ['numericFilters ' ])) {
171- $ params ['body ' ]['query ' ]['bool ' ]['must ' ] = array_merge ($ params ['body ' ]['query ' ]['bool ' ]['must ' ],
172- $ options ['numericFilters ' ]);
175+ $ params ['body ' ]['query ' ]['bool ' ]['must ' ] = array_merge (
176+ $ params ['body ' ]['query ' ]['bool ' ]['must ' ],
177+ $ options ['numericFilters ' ]
178+ );
173179 }
174180
175181 if ($ builder ->callback ) {
@@ -187,7 +193,8 @@ protected function performSearch(Builder $builder, array $options = [])
187193 /**
188194 * Get the filter array for the query.
189195 *
190- * @param Builder $builder
196+ * @param Builder $builder
197+ *
191198 * @return array
192199 */
193200 protected function filters (Builder $ builder )
@@ -204,7 +211,8 @@ protected function filters(Builder $builder)
204211 /**
205212 * Pluck and return the primary keys of the given results.
206213 *
207- * @param mixed $results
214+ * @param mixed $results
215+ *
208216 * @return \Illuminate\Support\Collection
209217 */
210218 public function mapIds ($ results )
@@ -215,9 +223,10 @@ public function mapIds($results)
215223 /**
216224 * Map the given results to instances of the given model.
217225 *
218- * @param \Laravel\Scout\Builder $builder
219- * @param mixed $results
220- * @param \Illuminate\Database\Eloquent\Model $model
226+ * @param \Laravel\Scout\Builder $builder
227+ * @param mixed $results
228+ * @param \Illuminate\Database\Eloquent\Model $model
229+ *
221230 * @return Collection
222231 */
223232 public function map (Builder $ builder , $ results , $ model )
@@ -226,30 +235,34 @@ public function map(Builder $builder, $results, $model)
226235 return $ model ->newCollection ();
227236 }
228237 $ keys = collect ($ results ['hits ' ]['hits ' ])->pluck ('_id ' )->values ()->all ();
238+
229239 return $ model ->getScoutModelsByIds (
230- $ builder , $ keys
231- )->filter (function ($ model ) use ($ keys ) {
240+ $ builder ,
241+ $ keys
242+ )->filter (function ($ model ) use ($ keys ) {
232243 return in_array ($ model ->getScoutKey (), $ keys );
233244 });
234245 }
235246
236247 /**
237248 * Flush all of the model's records from the engine.
238249 *
239- * @param \Illuminate\Database\Eloquent\Model $model
250+ * @param \Illuminate\Database\Eloquent\Model $model
251+ *
240252 * @return void
241253 */
242254 public function flush ($ model )
243255 {
244256 $ model ->newQuery ()
245257 ->orderBy ($ model ->getKeyName ())
246258 ->unsearchable ();
247- }
259+ }
248260
249261 /**
250262 * Get the total count from a raw result returned by the engine.
251263 *
252- * @param mixed $results
264+ * @param mixed $results
265+ *
253266 * @return int
254267 */
255268 public function getTotalCount ($ results )
@@ -260,7 +273,8 @@ public function getTotalCount($results)
260273 /**
261274 * Generates the sort if theres any.
262275 *
263- * @param Builder $builder
276+ * @param Builder $builder
277+ *
264278 * @return array|null
265279 */
266280 protected function sort ($ builder )
@@ -269,7 +283,7 @@ protected function sort($builder)
269283 return null ;
270284 }
271285
272- return collect ($ builder ->orders )->map (function ($ order ) {
286+ return collect ($ builder ->orders )->map (function ($ order ) {
273287 return [$ order ['column ' ] => $ order ['direction ' ]];
274288 })->toArray ();
275289 }
0 commit comments