Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add search engine meta data to results #780

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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