Skip to content

Commit 19114ef

Browse files
committed
Normalise score/_score in SearchMatch for Spice v1/v2 compatibility
- Both score and _score are required (non-optional) on SearchMatch - If only score is present (v1), _score is filled from it - If only _score is present (v2), score is filled from it - score is marked @deprecated in favour of _score
1 parent 5138783 commit 19114ef

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/client-common.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,15 @@ export class SpiceClient {
13861386
}
13871387

13881388
const result = await response.json();
1389+
if (result.results) {
1390+
for (const match of result.results) {
1391+
if (match._score === undefined && match.score !== undefined) {
1392+
match._score = match.score;
1393+
} else if (match.score === undefined && match._score !== undefined) {
1394+
match.score = match._score;
1395+
}
1396+
}
1397+
}
13891398
return result as SearchResponse;
13901399
}
13911400

src/interfaces.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,20 @@ export interface SearchMatch {
133133
*/
134134
dataset: string;
135135
/**
136-
* The similarity of the match to the query (Spice v1)
136+
* The similarity of the match to the query.
137+
*
138+
* @deprecated Use `_score` instead. This field is from Spice v1 and is kept for
139+
* backward compatibility. When the server returns only `_score` (v2), this field
140+
* is automatically filled from `_score`.
137141
*/
138-
score?: number;
142+
score: number;
139143
/**
140-
* The similarity of the match to the query (Spice v2)
144+
* The similarity of the match to the query.
145+
*
146+
* This is the canonical score field used by Spice v2. When the server returns
147+
* only `score` (v1), this field is automatically filled from `score`.
141148
*/
142-
_score?: number;
149+
_score: number;
143150
/**
144151
* The matches for this result
145152
*/

0 commit comments

Comments
 (0)