mongoosastic-ts
TypeScript icon, indicating that this package has built-in type declarations

6.0.3 • Public • Published

mongoosastic-ts

Build Status 7 Build Status 8.0 Build Status 8.2 Build Status 8.5 Build Status 8.6 NPM version Downloads UNPKG

mongoosastic-ts is a mongoose plugin that can automatically index your models into elasticsearch.

This 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

  1. Install the package
npm install -S mongoosastic-ts
  1. 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);
  1. 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

Documentation

View docs

Package Sidebar

Install

npm i mongoosastic-ts

Weekly Downloads

4

Version

6.0.3

License

MIT

Unpacked Size

73.2 kB

Total Files

15

Last publish

Collaborators

  • meabed