-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featureThis issue is a user-facing general improvement that doesn't fix a bug or add a new featurenew featureThis change adds new functionality, like a new method or classThis change adds new functionality, like a new method or class
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
There's no parallel to ensureIndexes
for search indexes; if autoSearchIndex
is false then we must call createSearchIndex
manually, which is inconvenient if search indexes were added to the schema directly using the searchIndex
prop. I'd like to propose a new method, ensureSearchIndexes
, that covers this behaviour.
Motivation
We load our models using autoIndex: false
and instead ensure that we run model.ensureIndexes()
on deployments. Recently, we added a search index to one of our models (using the @searchIndex
prop) but found that it wasn't created via ensureIndexes
. We'd like to keep autoSearchIndex: false
for performance reasons.
In the example below, the index on testProperty
is created, but the namesake search index isn't.
Example
import mongoose from 'mongoose';
import {
getModelForClass,
index,
modelOptions,
prop,
searchIndex,
} from '@typegoose/typegoose';
@index({ testProperty: 1 })
@searchIndex({
type: 'search',
name: 'search_index',
definition: {
mappings: {
dynamic: true,
fields: {
testProperty: [{ type: 'string' }, { type: 'token' }],
},
},
},
})
@modelOptions({ options: { customName: 'Test' } })
class Test {
@prop({ type: String })
public testProperty?: string;
}
const mongooseConnection = await mongoose.connect(mongoUri, {
dbName: dbName,
// ...
});
const TestModel = getModelForClass(Test, {
existingMongoose: mongooseConnection,
schemaOptions: { autoIndex: false },
});
// Create a document to ensure the collection is created
const doc = await new TestModel({
testProperty: 'testProperty',
}).save();
await TestModel.ensureIndexes();
await TestModel.ensureSearchIndexes();
await mongoose.disconnect();
Metadata
Metadata
Assignees
Labels
enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featureThis issue is a user-facing general improvement that doesn't fix a bug or add a new featurenew featureThis change adds new functionality, like a new method or classThis change adds new functionality, like a new method or class