How do I add a new Raven database index?
Objective: This article explains how to create a new database index in the platform.
Prerequisites:
- You have an existing database model that you want to add an index for.
Steps:
- Create a new file in
modules/<module>/core/indexes/
- Use
this.map
to generate a new index object for each document in the collection. - Export the index the
index.ts
file:modules/<module>/core/indexes/index.ts
Example:
// modules/<module>/core/indexes/my-model.js
import { AbstractJavaScriptIndexCreationTask } from 'ravendb'
import { MyModel } from '../models/my-model.js'
export class MyModels extends AbstractJavaScriptIndexCreationTask<MyModel> {
constructor() {
super()
this.searchEngineType = 'Lucene'
this.map('MyModels', model => {
return {
createdAtUTC: new Date(model.createdAtUTC),
patch: model.patch,
price: model.price, // Example property from the model
...
}
})
}
}
Usage:
const models = await context.session.query<MyModel>('MyModels').whereGreaterThan('price', 100).all()
Additional Resources: