Skip to content

Commit

Permalink
Add metadata to lazyMap() for both engines as well
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz-nz committed Nov 16, 2023
1 parent 1a70aca commit 1fa2842
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ public function lazyMap(Builder $builder, $results, $model)
$builder, $objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand Down
10 changes: 10 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ public function lazyMap(Builder $builder, $results, $model)
$builder, $objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,18 @@ public function test_lazy_map_correctly_maps_results_to_models()

$model = m::mock(stdClass::class);
$model->shouldReceive('queryScoutModelsByIds->cursor')->andReturn($models = LazyCollection::make([
new SearchableModel(['id' => 1]),
new SearchableModel(['id' => 1, 'name' => 'test']),
]));

$builder = m::mock(Builder::class);

$results = $engine->lazyMap($builder, ['nbHits' => 1, 'hits' => [
['objectID' => 1, 'id' => 1],
['objectID' => 1, 'id' => 1, '_rankingInfo' => ['nbTypos' => 0]],
]], $model);

$this->assertCount(1, $results);
$this->assertEquals(['id' => 1, 'name' => 'test'], $results->first()->toArray());
$this->assertEquals(['_rankingInfo' => ['nbTypos' => 0]], $results->first()->scoutMetaData());
}

public function test_lazy_map_method_respects_order()
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,21 @@ public function test_lazy_map_correctly_maps_results_to_models()

$model = m::mock(stdClass::class);
$model->shouldReceive(['getScoutKeyName' => 'id']);
$model->shouldReceive('queryScoutModelsByIds->cursor')->andReturn($models = LazyCollection::make([new SearchableModel(['id' => 1])]));
$model->shouldReceive('queryScoutModelsByIds->cursor')->andReturn($models = LazyCollection::make([
new SearchableModel(['id' => 1, 'name' => 'test']),
]));
$builder = m::mock(Builder::class);

$results = $engine->lazyMap($builder, [
'totalHits' => 1,
'hits' => [
['id' => 1],
['id' => 1, '_rankingScore' => 0.86],
],
], $model);

$this->assertEquals(1, count($results));
$this->assertEquals(['id' => 1, 'name' => 'test'], $results->first()->toArray());
$this->assertEquals(['_rankingScore' => 0.86], $results->first()->scoutMetadata());
}

public function test_lazy_map_method_respects_order()
Expand Down

0 comments on commit 1fa2842

Please sign in to comment.