55 * @since Jun 2023
66 * @author Greg Priday <greg@siteorigin.com>
77 */
8- namespace Qdrant \Models \Request ;
8+ namespace Qdrant \Models \Request \ Points ;
99
10+ use Qdrant \Exception \InvalidArgumentException ;
1011use Qdrant \Models \Filter \Filter ;
1112use Qdrant \Models \Traits \ProtectedPropertyAccessor ;
1213
1314class RecommendRequest
1415{
1516 use ProtectedPropertyAccessor;
1617
18+ /**
19+ * average_vector - Average positive and negative vectors and create a single query with the formula
20+ * query = avg_pos + avg_pos - avg_neg. Then performs normal search.
21+ */
22+ const STRATEGY_AVERAGE_VECTOR = 'average_vector ' ;
23+
24+ /**
25+ * best_score - Uses custom search objective. Each candidate is compared against all examples, its
26+ * score is then chosen from the max(max_pos_score, max_neg_score). If the max_neg_score is chosen
27+ * then it is squared and negated, otherwise it is just the max_pos_score.
28+ */
29+ const STRATEGY_BEST_SCORE = 'best_score ' ;
30+
31+ protected ?string $ shardKey = null ;
32+ protected ?string $ strategy = null ;
1733 protected ?Filter $ filter = null ;
1834 protected ?string $ using = null ;
1935 protected ?int $ limit = null ;
@@ -31,6 +47,27 @@ public function setFilter(Filter $filter): static
3147 return $ this ;
3248 }
3349
50+ public function setShardKey (string $ shardKey ): static
51+ {
52+ $ this ->shardKey = $ shardKey ;
53+
54+ return $ this ;
55+ }
56+
57+ public function setStrategy (string $ strategy ): static
58+ {
59+ $ strategies = [
60+ self ::STRATEGY_AVERAGE_VECTOR ,
61+ self ::STRATEGY_BEST_SCORE ,
62+ ];
63+ if (!in_array ($ strategy , $ strategies )) {
64+ throw new InvalidArgumentException ('Invalid strategy for recommendation. ' );
65+ }
66+ $ this ->strategy = $ strategy ;
67+
68+ return $ this ;
69+ }
70+
3471 public function setScoreThreshold (float $ scoreThreshold ): static
3572 {
3673 $ this ->scoreThreshold = $ scoreThreshold ;
@@ -66,19 +103,25 @@ public function toArray(): array
66103 'negative ' => $ this ->negative ,
67104 ];
68105
106+ if ($ this ->shardKey !== null ) {
107+ $ body ['shard_key ' ] = $ this ->shardKey ;
108+ }
69109 if ($ this ->filter !== null && $ this ->filter ->toArray ()) {
70110 $ body ['filter ' ] = $ this ->filter ->toArray ();
71111 }
72- if ($ this ->scoreThreshold ) {
112+ if ($ this ->scoreThreshold !== null ) {
73113 $ body ['score_threshold ' ] = $ this ->scoreThreshold ;
74114 }
75- if ($ this ->using ) {
115+ if ($ this ->using !== null ) {
76116 $ body ['using ' ] = $ this ->using ;
77117 }
78- if ($ this ->limit ) {
118+ if ($ this ->limit !== null ) {
79119 $ body ['limit ' ] = $ this ->limit ;
80120 }
81- if ($ this ->offset ) {
121+ if ($ this ->strategy !== null ) {
122+ $ body ['strategy ' ] = $ this ->strategy ;
123+ }
124+ if ($ this ->offset !== null ) {
82125 $ body ['offset ' ] = $ this ->offset ;
83126 }
84127
0 commit comments