elasticmongooseq
Mongoose elasticsearch bindings
Last updated 6 years ago by oliverbrooks .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install elasticmongooseq 
SYNC missed versions from official npm registry.

ElasticMongooseQ

Binds a mongoose model to an elasticsearch type using the Q promise library.

I wanted a promise based library based on the official elasticsearch node client. There are other great alternatives such as elasticMongoose, 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 elasticMongooseQ.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

  mongoose = require("mongoose")
  ElasticMongooseQ = require("elasticmongooseq")
  
  # 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 elasticMongoose creation in an initializer
  elasticMongooseQ = new ElasticMongooseQ
    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 elasticMongooseQ.plugin(),
    type: 'animal' 
    mapping: elasticMapping

  model = mongoose.model "Animal", AnimalSchema

Use model

./test.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, hydrate: true).then (res) ->
      console.log 'res', res

### Maintenance and migrations

./tasks.coffee

  elastic

Current Tags

  • 0.0.17                                ...           latest (6 years ago)

7 Versions

  • 0.0.17                                ...           6 years ago
  • 0.0.16                                ...           6 years ago
  • 0.0.15                                ...           6 years ago
  • 0.0.14                                ...           6 years ago
  • 0.0.13                                ...           6 years ago
  • 0.0.12                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 5
Last Day 0
Last Week 1
Last Month 0
Dependencies (5)
Dev Dependencies (3)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |