From 1fa284267db3c7b8fd5440631806bdbb8f2115dd Mon Sep 17 00:00:00 2001 From: Toby Evans Date: Thu, 16 Nov 2023 22:35:52 +1300 Subject: [PATCH] Add metadata to lazyMap() for both engines as well --- src/Engines/AlgoliaEngine.php | 10 ++++++++++ src/Engines/MeilisearchEngine.php | 10 ++++++++++ tests/Unit/AlgoliaEngineTest.php | 6 ++++-- tests/Unit/MeilisearchEngineTest.php | 8 ++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index d95299c6..cc2a3b8a 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -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(); diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index 5cae1114..3e5f02b7 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -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(); diff --git a/tests/Unit/AlgoliaEngineTest.php b/tests/Unit/AlgoliaEngineTest.php index 6091c0f0..30354fb1 100644 --- a/tests/Unit/AlgoliaEngineTest.php +++ b/tests/Unit/AlgoliaEngineTest.php @@ -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() diff --git a/tests/Unit/MeilisearchEngineTest.php b/tests/Unit/MeilisearchEngineTest.php index 4ede6173..bdd059ff 100644 --- a/tests/Unit/MeilisearchEngineTest.php +++ b/tests/Unit/MeilisearchEngineTest.php @@ -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()