Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/leonardoventurini/mongoosearch
- Owner: leonardoventurini
- License: mit
- Created: 2022-04-20T02:12:13.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-22T23:45:12.000Z (over 1 year ago)
- Last Synced: 2024-10-04T10:42:58.136Z (about 1 month ago)
- Topics: elasticsearch, mongoose
- Language: TypeScript
- Homepage:
- Size: 143 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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' },
})
```