Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/leonardoventurini/mongoosearch

An Elasticsearch plugin for Mongoose
https://github.com/leonardoventurini/mongoosearch

elasticsearch mongoose

Last synced: 26 days ago
JSON representation

An Elasticsearch plugin for Mongoose

Awesome Lists containing this project

README

        

# Mongoosearch Beta

Integrate Mongoose with Elasticsearch, the easy way.

## Installation

This module is distributed via [npm](https://www.npmjs.com/), commands:

```shell
npm install mongoosearch
```

or:

```shell
yarn add mongoosearch
```

## Initializing

```ts
import mongoose, { Schema } from 'mongoose'
import { Mongoosearch, MongoosearchModel } from 'mongoosearch'
import { ElasticsearchClient } from '../elasticsearch-client'
import { CollectionNames } from '../collection-names'

export interface Cat extends Document {
sample: string
}

export interface CatModel extends MongoosearchModel {}

export const CatSchema = new Schema({
sample: { type: String, elasticsearch: true },
}).plugin(Mongoosearch, { client: ElasticsearchClient })

export const CatCollection = mongoose.model(
CollectionNames.Cat,
CatSchema,
)
```

Then:

```ts
await CatCollection.esCreateMapping()
```

This will parse the schema fields containing Elasticsearch enabled fields and persist the corresponding mapping.

Now new documents will be automatically persisted on Elasticsearch.

If you specify the `manual` option like so:

```ts
schema.plugin(Mongoosearch, { client: ElasticsearchClient, manual: true })
```

Then you will need to call `esSync` manually:

```ts
await CatCollection.esSync()
```

## Searching

After you got your collection indexed you can search it:

```ts
const results = await CatCollection.esSearch({
query_string: { query: 'garfield' },
})
```