mongoosastic-ts
Version:
A mongoose plugin that indexes models into elastic search
73 lines (53 loc) • 2.8 kB
Markdown
# mongoosastic-ts
[](https://github.com/meabed/mongoosastic-ts/actions/workflows/ci-7.yml)
[](https://github.com/meabed/mongoosastic-ts/actions/workflows/ci-8-0.yml)
[](https://github.com/meabed/mongoosastic-ts/actions/workflows/ci-8-2.yml)
[](https://github.com/meabed/mongoosastic-ts/actions/workflows/ci-8-5.yml)
[](https://github.com/meabed/mongoosastic-ts/actions/workflows/ci-8-6.yml)
[](https://www.npmjs.com/package/mongoosastic-ts)
[](https://www.npmjs.com/package/mongoosastic-ts)
[](https://unpkg.com/browse/mongoosastic-ts@latest/)
#### mongoosastic-ts is a [mongoose](http://mongoosejs.com/) plugin that can automatically index your models into [elasticsearch](https://www.elastic.co/).
> This package is forked version from [mongoosastic](https://github.com/mongoosastic/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
```bash
npm install -S mongoosastic-ts
```
2. Setup your mongoose model to use the plugin
```typescript
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);
```
3. Query your Elasticsearch with the `search()` method (added by the plugin)
```typescript
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:
```bash
curl http://localhost:9200/users/user/_search
```
### Documentation
[View docs](docs/README.md)