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

Fix: typesense missing scoutmetadata #868

Draft
wants to merge 11 commits into
base: 10.x
Choose a base branch
from
13 changes: 13 additions & 0 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ public function map(Builder $builder, $results, $model)
->filter(static function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds, false);
})
->map(static function ($model) use ($hits, $objectIdPositions) {
$result = $hits[$objectIdPositions[$model->getScoutKey()]] ?? [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a test for the fallback of the empty array


foreach ($result as $key => $value) {
if ($key === 'document') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this case specifically?

continue;
}

$model->withScoutMetadata($key, $value);
}

return $model;
})
->sortBy(static function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,42 @@ public function test_map_ids_method(): void
$this->assertEquals([1, 2, 3], $mappedIds->toArray());
}

public function test_map_correctly_maps_results_to_models()
{
$engine = new TypesenseEngine($this->createMock(TypesenseClient::class));

$model = m::mock(stdClass::class);
$model->shouldReceive(['getScoutKeyName' => 'id']);
$model->shouldReceive('getScoutModelsByIds')->andReturn(
Collection::make([
new SearchableModel([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some more response data to verify handling of multiple results?

'id' => 1,
'name' => 'test',
]),
])
);

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

$results = $engine->map($builder, [
'found' => 1,
'hits' => [
[
'document' => ['id' => 1, 'name' => 'test'],
'geo_distance_meters' => ['location' => 5],
'highlights' => [],
],
],
], $model);

$this->assertCount(1, $results);
$this->assertEquals(['id' => 1, 'name' => 'test'], $results->first()->toArray());
$this->assertEquals(
['geo_distance_meters' => ['location' => 5], 'highlights' => []],
$results->first()->scoutMetadata()
);
}

public function test_get_total_count_method(): void
{
// Sample search results with 'found' key
Expand Down