# mongoose-elasticsearch

> Mongoose elasticsearch bindings

Latest version **0.1.1** (published 2014-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-elasticsearch
pnpm add mongoose-elasticsearch
yarn add mongoose-elasticsearch
bun add mongoose-elasticsearch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2014-10-21 |
| First published | 2014-10-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Oliver Brooks |
| Maintainers | oliverbrooks |
| Keywords | Elasticsearch, Mongoose, Bluebird |

## Links

- npm: https://www.npmjs.com/package/mongoose-elasticsearch
- Repository: https://bitbucket.org/pearlshare/mongoose-elasticsearch
- npm.io page: https://npm.io/package/mongoose-elasticsearch

## Dependencies (3)

- [bluebird](https://npm.io/package/bluebird.md) ^2.3.5
- [coffee-script](https://npm.io/package/coffee-script.md) ^1.4.x
- [elasticsearch](https://npm.io/package/elasticsearch.md) ^2.4.3

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.1 (latest) — 2014-10-21
- 0.1.0 — 2014-10-17
- 0.0.18 — 2014-10-17

## README

# MongooseElasticSearch

Binds a mongoose model to an elasticsearch.

I wanted a promise based library based on the official elasticsearch node client. There are other great alternatives such as mongooseElasticsearch, mongoosastic but they didn't fit my needs as I needed more flexiblity around setting the schema to use parent associations and complex analyzers. 

## Changes!!!

syntax change when defining plugins
  
    AnimalSchema.plugin mongooseElasticsearch.plugin(), {type: 'animal', mapping: elasticMapping}

## Features

* Optional automatic saving to elasticsearch
* Optional automatic deletion from elasticsearch
* Bulk indexing of a model to ES
* Use a preconfigured elasticsearch client
* Manually define elasticsearch mapping
* Optional custom object to save (which can be a promise to populate fields)

## Usage

### Setup model

./animal.coffee

```coffee
  mongoose = require("mongoose")
  MongooseElasticSearch = require("mongoose-elasticsearch")
  
  # Generic setup
  elasticsearch = require("elasticsearch") 

  # can put the elasticClient creation in an initializer
  elasticClient = new elasticsearch.Client
    host: "localhost:9200"
    maxSockets: 2
    sniffOnStart: true
    sniffInterval: 60000
    defer: () -> Q.defer()

  # Can put the mongooseElasticsearch creation in an initializer
  mongooseElasticsearch = new MongooseElasticSearch
    index: 'animals'
    client: elasticClient

  # Exact elasticsearch mapping required for the object
  elasticmapping =
    properties:
      name:
        type: "string"
      description:
        type: "string"
        analyzer: "english"
      createdAt:
        type: "date"

  # Define mongoose schema
  AnimalSchema = new mongoose.Schema
    name:
      type: String
    description:
      type: String
    createdAt:
      type: Date

  # Add the plugin giving the type and 
  AnimalSchema.plugin mongooseElasticsearch.plugin(),
    type: 'animal' 
    mapping: elasticMapping

  model = mongoose.model "Animal", AnimalSchema
```

### Use model

./test.coffee

```coffee
  Animal = require("./animal")
  animal = new Animal(name: 'Cheater', description: "Freakin fast cat", createdAt: new Date())

  animal.save().then (animal) ->
    # Standard elasticsearch query
    query = 
      matchAll: {}

    Animal.search(query).then (res) ->
      console.log 'res', res

```

### Maintenance and migrations

./tasks.coffee

```coffee
  elastic
```

---
_Source: https://npm.io/package/mongoose-elasticsearch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
