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: missing scout metadata for typesense search #1

Merged
merged 1 commit into from
Sep 24, 2024
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
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()]] ?? [];

foreach ($result as $key => $value) {
if ($key === 'document') {
continue;
}

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

return $model;
})
->sortBy(static function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,46 @@ 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([
'document' => [
[
'id' => 1,
'name' => 'test',
]
],
'geo_distance_meters' => ['location' => 5],
'highlights' => [],
]),
]));

$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