-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exercises): 🔍 added autocomplete suggestion search
- Loading branch information
1 parent
e639f2c
commit a08f6d3
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../exercises/use-cases/get-autocomplete-suggestions/get-autocomplete-suggestions.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { IUseCase } from '#common/types/use-case.type.js' | ||
import { IExerciseDoc, IExerciseModel } from '#infra/mongodb/models/exercises/exercise.entity.js' | ||
|
||
export interface GetAutoCompleteSuggestionsArgs { | ||
search?: string | ||
} | ||
export class GetAutoCompleteSuggestionsUseCase implements IUseCase<GetAutoCompleteSuggestionsArgs, IExerciseDoc[]> { | ||
constructor(private readonly exerciseModel: IExerciseModel) {} | ||
|
||
async execute({ search = '' }: GetAutoCompleteSuggestionsArgs): Promise<IExerciseDoc[]> { | ||
try { | ||
if (!search) { | ||
return [] | ||
} | ||
|
||
const autocompleteResults = await this.exerciseModel.aggregate([ | ||
{ | ||
$search: { | ||
index: 'exercises', | ||
autocomplete: { | ||
query: search, | ||
path: 'name', | ||
fuzzy: { | ||
maxEdits: 1, | ||
prefixLength: 1 | ||
} | ||
} | ||
} | ||
}, | ||
{ $limit: 10 }, | ||
{ | ||
$project: { | ||
name: 1, | ||
gifUrl: 1, | ||
exerciseId: 1, | ||
score: { $meta: 'searchScore' } | ||
} | ||
}, | ||
{ $sort: { score: -1 } } | ||
]) | ||
|
||
return autocompleteResults | ||
} catch (error) { | ||
throw new Error('Failed to generate exercises suggestions') | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/modules/exercises/use-cases/get-autocomplete-suggestions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './get-autocomplete-suggestions.usecase' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters