feat: add personalize search parameter to SearchQuery#918
Conversation
Meilisearch v1.47.0 adds experimental personalized search: the search endpoints now accept a `personalize` object with a `userContext` string. Add a `PersonalizeOptions` contract (mirroring `HybridSearchOptions`) and a `SearchQuery::setPersonalize()` builder method, serialized into the search payload through `toArray()`. The plain `search()` path already forwards arbitrary parameters, so it needs no change. Adds contract tests for `PersonalizeOptions` and `SearchQuery::setPersonalize`. Closes meilisearch#917
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughA new ChangesPersonalized Search
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Contracts/SearchQuery.php`:
- Around line 491-492: The `toArray()` method documentation in SearchQuery.php
uses a weak type annotation `personalize?: array<mixed>` for the personalize
parameter at line 492, which violates the coding guideline to use precise
PHPStan array shapes. Examine the `PersonalizeOptions::toArray()` method to
determine its exact return shape, then replace the generic `array<mixed>` type
with the precise shape that PersonalizeOptions::toArray() actually returns in
the PHPDoc comment for the toArray() method.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c6af0bca-9a25-4d19-b9d0-af37e9f3dea4
📒 Files selected for processing (4)
src/Contracts/PersonalizeOptions.phpsrc/Contracts/SearchQuery.phptests/Contracts/PersonalizeOptionsTest.phptests/Contracts/SearchQueryTest.php
| 'rankingScoreThreshold' => $this->rankingScoreThreshold, | ||
| 'distinct' => $this->distinct, | ||
| 'federationOptions' => null !== $this->federationOptions ? $this->federationOptions->toArray() : null, | ||
| 'personalize' => null !== $this->personalize ? $this->personalize->toArray() : null, |
There was a problem hiding this comment.
| 'personalize' => null !== $this->personalize ? $this->personalize->toArray() : null, | |
| 'personalize' => $this->personalize?->toArray(), |
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| class PersonalizeOptions |
There was a problem hiding this comment.
| class PersonalizeOptions | |
| final class PersonalizeOptions |
Address review feedback: simplify personalize serialization with the nullsafe operator and mark PersonalizeOptions as final.
|
Thanks for the review @norkunas! Applied both suggestions: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #918 +/- ##
==========================================
- Coverage 89.78% 88.44% -1.34%
==========================================
Files 59 93 +34
Lines 1449 1939 +490
==========================================
+ Hits 1301 1715 +414
- Misses 148 224 +76 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@Strift we need to fix phpstan, new version prints many errors |
Summary
Meilisearch v1.47.0 adds experimental personalized search. This adds support for
the new
personalizesearch parameter ({ "userContext": "..." }) to the typedSearchQuerybuilder. Closes #917.Changes
PersonalizeOptionscontract withsetUserContext(), mirroringHybridSearchOptions.SearchQuery::setPersonalize(PersonalizeOptions), serialized into the searchpayload through
toArray()(used bymultiSearch/ federated search). Thearray-based
search()path already forwards arbitrary parameters, so it needsno change.
PersonalizeOptionsandSearchQuery::setPersonalize.Example
Testing
composer lintpasses. Added contract tests forPersonalizeOptionsandSearchQuery::setPersonalize; they assert the serialized payload and don'trequire a running Meilisearch instance.
Overview
This PR adds support for personalized search (experimental in Meilisearch v1.47.0) to the PHP SDK’s typed
SearchQuerybuilder, allowing auserContextstring to be attached to search requests.Changes
New Contract:
PersonalizeOptionsA new
Meilisearch\Contracts\PersonalizeOptionscontract was introduced (src/Contracts/PersonalizeOptions.php) to model personalization settings. It provides:setUserContext(string $userContext): self(fluent setter)toArray(): arrayto serialize the payload, omitting unset valuesPersonalizeOptionsis implemented as afinalclass.Enhanced
SearchQueryThe
Meilisearch\Contracts\SearchQuerycontract now supports personalization by adding:private ?PersonalizeOptions $personalizefieldsetPersonalize(PersonalizeOptions $personalize): selfSearchQuery::toArray()was updated to include an optionalpersonalizeentry shaped like{ userContext?: non-empty-string }, serialized viaPersonalizeOptions->toArray(). Whenpersonalizeis not set, the field is omitted from the payload (with serialization simplified using PHP’s nullsafe operator).This serialized payload is used by request flows that rely on typed query serialization (e.g.,
multiSearchand federated search).Test Coverage
tests/Contracts/PersonalizeOptionsTest.phpvalidatesPersonalizeOptions::toArray()for both empty and populateduserContext.tests/Contracts/SearchQueryTest.phpadds coverage forSearchQuery::setPersonalize()ensuring the serializedpersonalize.userContextis present intoArray().Resolution
Implements the requested SDK support for personalized search introduced in Meilisearch v1.47.0 (fixing issue
#917).