-
Notifications
You must be signed in to change notification settings - Fork 349
Fix: typesense missing scoutmetadata #868
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
base: 10.x
Are you sure you want to change the base?
Changes from 5 commits
7c4fd18
acb57e3
27191c8
e3283cb
8e70c54
558ff31
3841436
bddac47
bcbc773
c8a7bb8
5fd05e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this case specifically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using a join between 2 collections and including fields of a joined collection in your search response, you get those nested objects in the document (https://typesense.org/docs/27.1/api/joins.html#merging-nesting-joined-fields). If the 'document' key is also added to the scout metadata, then there is a way to access the fields of those joined collection objects. |
||
continue; | ||
} | ||
|
||
$model->withScoutMetadata($key, $value); | ||
} | ||
|
||
return $model; | ||
}) | ||
->sortBy(static function ($model) use ($objectIdPositions) { | ||
return $objectIdPositions[$model->getScoutKey()]; | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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