Skip to content

Commit

Permalink
[10.x] Add search engine meta data to results (#780)
Browse files Browse the repository at this point in the history
* Add meilisearch meta data to results

add result meta data to the return models with `withScoutMetadata`

* Update src/Engines/MeilisearchEngine.php

* add engine metadata to AlgoliaEngine

* move adding model meta data to after filtering

* Add assertions to test scoutMetadata value

for both Aloglia and Meilisearch engines

* Add metadata to lazyMap() for both engines as well

* codestyle fixes

---------

Co-authored-by: Dries Vints <[email protected]>
  • Loading branch information
tobz-nz and driesvints authored Nov 21, 2023
1 parent 03c94ec commit f2bbeb3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ public function map(Builder $builder, $results, $model)
$builder, $objectIds
)->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 All @@ -236,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
20 changes: 20 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ public function map(Builder $builder, $results, $model)
$builder, $objectIds
)->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 All @@ -319,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
7 changes: 1 addition & 6 deletions tests/Fixtures/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ class SearchableModel extends Model
*
* @var array
*/
protected $fillable = ['id'];
protected $fillable = ['id', 'name'];

public function searchableAs()
{
return 'table';
}

public function scoutMetadata()
{
return [];
}
}
20 changes: 14 additions & 6 deletions tests/Unit/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,23 @@ public function test_map_correctly_maps_results_to_models()
$engine = new AlgoliaEngine($client);

$model = m::mock(stdClass::class);

$model->shouldReceive('getScoutModelsByIds')->andReturn($models = Collection::make([
new SearchableModel(['id' => 1]),
new SearchableModel(['id' => 1, 'name' => 'test']),
]));

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

$results = $engine->map($builder, ['nbHits' => 1, 'hits' => [
['objectID' => 1, 'id' => 1],
]], $model);
$results = $engine->map($builder, [
'nbHits' => 1,
'hits' => [
['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_map_method_respects_order()
Expand Down Expand Up @@ -210,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
19 changes: 14 additions & 5 deletions tests/Unit/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,22 @@ public function test_map_correctly_maps_results_to_models()

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

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

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

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

public function test_map_method_respects_order()
Expand Down Expand Up @@ -341,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 f2bbeb3

Please sign in to comment.