-
-
Notifications
You must be signed in to change notification settings - Fork 598
feat: Add support for MongoDB $search
operator
#2526
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
Changes from all commits
c8a6874
57df0f5
8e52828
51cc9ed
1d1d926
9ade102
13b75b9
233ac9c
11c06d9
d569d7e
d256d4b
60d4de4
bbcceea
4c7291b
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 |
---|---|---|
|
@@ -35,6 +35,10 @@ interface FullTextQueryOptions { | |
diacriticSensitive?: boolean; | ||
} | ||
|
||
interface SearchOptions { | ||
index?: string; | ||
} | ||
|
||
export interface QueryJSON { | ||
where: WhereClause; | ||
watch?: string; | ||
|
@@ -1585,6 +1589,49 @@ class ParseQuery<T extends ParseObject = ParseObject> { | |
return this._addCondition(key, '$text', { $search: fullOptions }); | ||
} | ||
|
||
/* | ||
* Triggers a MongoDb Atlas Text Search | ||
* | ||
* @param {string} value The string to search | ||
* @param {string[]} path The fields to search | ||
* @param {object} options (Optional) | ||
* @param {string} options.index The index to search | ||
* @returns {Promise} Returns a promise that will be resolved with the results | ||
* of the search | ||
* | ||
*/ | ||
|
||
async search(value: string, path: string[], options?: SearchOptions): Promise<T[]> { | ||
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 consistency we should rename Edit: There is a empty line above that should be removed |
||
if (!value) { | ||
throw new Error('A search term is required.'); | ||
} | ||
|
||
if (typeof value !== 'string') { | ||
throw new Error('The value being searched for must be a string.'); | ||
} | ||
|
||
const controller = CoreManager.getQueryController(); | ||
const params = { | ||
$search: { | ||
index: options?.index || 'default', | ||
text: { | ||
path, | ||
query: value, | ||
}, | ||
}, | ||
}; | ||
|
||
const searchOptions: { sessionToken?: string; useMasterKey: boolean } = { | ||
useMasterKey: true, | ||
}; | ||
const results = await controller.aggregate(this.className, params, searchOptions); | ||
return ( | ||
results.results?.map(result => | ||
ParseObject.fromJSON({ className: this.className, ...result }) | ||
) || [] | ||
); | ||
} | ||
|
||
/** | ||
* Method to sort the full text search by text score | ||
* | ||
|
Uh oh!
There was an error while loading. Please reload this page.