Skip to content

Commit 93d11aa

Browse files
author
Michaël
committed
Make lazyMap actually lazy
1 parent aeba68c commit 93d11aa

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/Engine.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ protected function performSearch(Builder $builder, array $options = [])
221221
->cloneWithoutBindings(['order']);
222222
$results = ['nbHits' => $countQuery->count($countQuery->getConnection()->raw('distinct record_id'))];
223223

224-
// Performing the search itself:
225-
$results['hits'] = $query->with('record')->get();
224+
// Preparing the actual query:
225+
$results['query'] = $query->with('record');
226226

227227
return $results;
228228
}
@@ -235,7 +235,19 @@ protected function performSearch(Builder $builder, array $options = [])
235235
*/
236236
public function mapIds($results)
237237
{
238-
return $results['hits']->pluck('record_id')->values();
238+
return $results['query']->pluck('record_id')->values();
239+
}
240+
241+
/**
242+
* Extract the Model from the search hit.
243+
*
244+
* @param SearchIndex $hit
245+
* @return \Illuminate\Database\Eloquent\Model
246+
*/
247+
protected function getRecord($hit)
248+
{
249+
$hit->record->_score = $hit->_score;
250+
return $hit->record;
239251
}
240252

241253
/**
@@ -248,9 +260,8 @@ public function mapIds($results)
248260
*/
249261
public function map(Builder $builder, $results, $model)
250262
{
251-
$models = $results['hits']->map(function ($hit) {
252-
$hit->record->_score = $hit->_score;
253-
return $hit->record;
263+
$models = $results['query']->get()->map(function ($hit) {
264+
return $this->getRecord($hit);
254265
})->all();
255266
return $model->newCollection($models);
256267
}
@@ -265,11 +276,9 @@ public function map(Builder $builder, $results, $model)
265276
*/
266277
public function lazyMap(Builder $builder, $results, $model)
267278
{
268-
$models = $results['hits']->map(function ($hit) {
269-
$hit->record->_score = $hit->_score;
270-
return $hit->record;
271-
})->all();
272-
return new LazyCollection($models);
279+
return $results['query']->lazy()->map(function ($hit) {
280+
return $this->getRecord($hit);
281+
});
273282
}
274283

275284
/**

tests/SearchTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,18 @@ public function test_morph_map()
238238
Post::all()->searchable();
239239
$this->assertEquals(5, Post::search('morphs')->count());
240240
}
241+
242+
public function test_lazy()
243+
{
244+
Post::skip(1)->take(3)->get()->each(function ($post) {
245+
$post->title = 'kikikuku';
246+
$post->save();
247+
});
248+
249+
$search = Post::search('kikikuku');
250+
$results = $search->get()->all();
251+
$lazyResults = $search->cursor()->all();
252+
$this->assertCount(3, $lazyResults);
253+
$this->assertEquals($results, $lazyResults);
254+
}
241255
}

0 commit comments

Comments
 (0)