-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(search): add WITHSCORES support to FT.SEARCH (#2143) #3432
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: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ export interface FtSearchOptions { | |
| VERBATIM?: boolean; | ||
| NOSTOPWORDS?: boolean; | ||
| INKEYS?: RedisVariadicArgument; | ||
| WITHSCORES?: boolean; | ||
| INFIELDS?: RedisVariadicArgument; | ||
| RETURN?: RedisVariadicArgument; | ||
| SUMMARIZE?: boolean | { | ||
|
|
@@ -72,6 +73,10 @@ export function parseSearchOptions(parser: CommandParser, options?: FtSearchOpti | |
| parser.push('NOSTOPWORDS'); | ||
| } | ||
|
|
||
| if (options?.WITHSCORES) { | ||
| parser.push('WITHSCORES'); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| parseOptionalVariadicArgument(parser, 'INKEYS', options?.INKEYS); | ||
| parseOptionalVariadicArgument(parser, 'INFIELDS', options?.INFIELDS); | ||
| parseOptionalVariadicArgument(parser, 'RETURN', options?.RETURN); | ||
|
|
@@ -171,8 +176,14 @@ function transformSearchReplyResp2( | |
| const documents: SearchReply['documents'] = []; | ||
| let i = 1; | ||
| while (i < reply.length) { | ||
| let score: number | undefined; | ||
|
|
||
| if(typeof reply[i] === 'number' || (typeof reply[i] === 'string' && !isNaN(Number(reply[i])) && Array.isArray(reply[i + 1]))){ | ||
| score = Number(reply[i++]); | ||
|
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.
For RESP2, Useful? React with 👍 / 👎. |
||
| } | ||
| documents.push({ | ||
| id: reply[i++] as string, | ||
| ...(score !== undefined ? {score} : {}), | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| value: (withoutDocuments ? {} : documentValue(reply[i++])) as SearchDocumentValue | ||
| }); | ||
| } | ||
|
|
@@ -203,9 +214,15 @@ function transformSearchReplyResp3( | |
| ); | ||
|
|
||
| const documents: SearchReply['documents'] = results.map(result => { | ||
| const resultMap = mapLikeToObject(result); | ||
| const { id, value } = parseSearchResultRow(result); | ||
|
|
||
| const rawScore = getMapValue(resultMap,['score']); | ||
| const score = rawScore !== undefined ? Number(rawScore) : undefined; | ||
|
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 a caller uses Useful? React with 👍 / 👎. |
||
|
|
||
| return { | ||
| id: String((id as { toString?(): string })?.toString?.() ?? id ?? ''), | ||
| ...(score !== undefined && !isNaN(score) ? {score} : {}), | ||
| value: value as SearchDocumentValue | ||
| }; | ||
| }); | ||
|
|
@@ -243,6 +260,7 @@ export interface SearchReply { | |
| total: number; | ||
| documents: Array<{ | ||
| id: string; | ||
| score?: number; | ||
| value: SearchDocumentValue; | ||
| }>; | ||
| /** | ||
|
|
||
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.
SEARCH_NOCONTENT.parseCommandreusesParameters<typeof SEARCH.parseCommand>, so addingWITHSCOREShere also exposes and serializes it forft.searchNoContent. That command's separate RESP2 transformer returnsreply.slice(1), treating every score as another document ID, while its RESP3 transformer maps the main result back to IDs and silently discards every score. Callers can therefore request scores through the public type but cannot receive a valid no-content result; either exclude this option from that variant or preserve ID/score pairs in its reply contract.Useful? React with 👍 / 👎.