mongoosastic-ts
mongoose plugin that can automatically index your models into elasticsearch.
mongoosastic-ts is aThis package is forked version from mongoosastic
It has been updated and migrated to typescript and updated dependencies and codebase to the latest packages
Support elasticsearch 7.x and 8.x
Getting started
- Install the package
npm install -S mongoosastic-ts
- Setup your mongoose model to use the plugin
import { mongoosastic } from 'mongoosastic-ts';
import { MongoosasticDocument, MongoosasticModel, MongoosasticPluginOpts } from 'mongoosastic-ts/dist/types';
import { Document, Schema, model } from 'mongoose';
export interface IBookModel extends Document, MongoosasticDocument {
title?: string;
}
const BookSchema = new Schema<IBookModel>({
title: {
type: String,
required: true,
},
});
BookSchema.plugin(mongoosastic, {
index: 'books',
type: 'book',
} as MongoosasticPluginOpts);
export const bookModel = model<IBookModel, MongoosasticModel<IBookModel>>('Book', BookSchema);
- Query your Elasticsearch with the
search()
method (added by the plugin)
const results = await bookModel.search({
query_string: {
query: "john"
});
// do something with elastic search results
NOTE: You can also query Elasticsearch with any other method. Example:
curl http://localhost:9200/users/user/_search