Skip to content
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

Add Atlas search index helpers to Models and Schemas #14251

Merged
merged 6 commits into from Jan 15, 2024
Merged

Conversation

vkarpov15
Copy link
Collaborator

Summary

cc @alexbevi

Atlas search indexes are special indexes for Atlas text search. There's distinct functionality to create Atlas search indexes in the MongoDB Node driver, so I added the following functionality:

  1. Model helpers: Model.createSearchIndex(), Model.updateSearchIndex(), Model.dropSearchIndex() to wrap the MongoDB Node driver's Collection.prototype.createSearchIndex(), etc. functions
  2. Schema helper to define search indexes Schema.prototype.searchIndex()
  3. An autoSearchIndex option analogous to autoIndex that opts in to automatically creating search indexes. Unlike autoIndex, autoSearchIndex is false by default.

Here's an example of this code in action:

mongoose.set('autoSearchIndex', true);

const schema = new mongoose.Schema({ name: String }, { });
schema.searchIndex({
  name: 'my-index',
  definition: { mappings: { dynamic: true } }
});
// Will automatically attempt to create the `my-index` search index.
const Test = mongoose.model('Test', schema); 

No tests right now because search indexes are only supported in MongoDB Atlas, and our CI tests don't run in Atlas. The MongoDB Node driver has a "manual" test suite that tests search indexes, something we should consider.

Examples

@vkarpov15 vkarpov15 added this to the 8.1 milestone Jan 11, 2024
lib/model.js Outdated Show resolved Hide resolved
lib/schema.js Outdated Show resolved Hide resolved
types/models.d.ts Show resolved Hide resolved
vkarpov15 and others added 3 commits January 13, 2024 14:59
Co-authored-by: hasezoey <hasezoey@gmail.com>
Co-authored-by: hasezoey <hasezoey@gmail.com>
@alexbevi
Copy link
Contributor

@vkarpov15 changes LGTM. Just FYI (re: CI) you can use the Atlas CLI to create a local Atlas deployment, which could be used for testing features such as this.

@vkarpov15
Copy link
Collaborator Author

@alexbevi that's really cool, thanks for the link. I don't think setting up a local Atlas deployment in CI should block releasing 8.1, so I'll merge this. But I'll look into local Atlas deployment in the future.

@vkarpov15 vkarpov15 merged commit 3393c96 into 8.1 Jan 15, 2024
25 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-14232 branch January 15, 2024 18:05
@alexbevi
Copy link
Contributor

Awesome Val. I just threw that out there as an FYI (not to hold anything up)

@hasezoey
Copy link
Collaborator

added a separate discussion to not forget to consider adding atlas tests #14261

noseworthy added a commit to noseworthy/mongoose that referenced this pull request Mar 19, 2024
Support for MongoDB Atlas search indexes was added in [Automattic#14251](Automattic#14251)
but the types for `Schema.searchIndex` and the `autoSearchIndex` option
were missed.

Add these types so that the typescript definitions match the
capabilities of the JavaScript version of `mongoose`.
noseworthy added a commit to noseworthy/mongoose that referenced this pull request Mar 19, 2024
Support for MongoDB Atlas search indexes was added in [Automattic#14251](Automattic#14251)
but the types for `Schema.searchIndex` and the `autoSearchIndex` option
were missed.

Add these types so that the typescript definitions match the
capabilities of the JavaScript version of `mongoose`.
noseworthy added a commit to noseworthy/mongoose that referenced this pull request Mar 20, 2024
Support for MongoDB Atlas search indexes was added in [Automattic#14251](Automattic#14251)
but the types for `Schema.searchIndex` and the `autoSearchIndex` option
were missed.

Add these types so that the typescript definitions match the
capabilities of the JavaScript version of `mongoose`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants