0.0.17 • Published 10 years ago

elasticmongooseq v0.0.17

Weekly downloads
1
License
MIT
Repository
bitbucket
Last release
10 years ago

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
0.0.17

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.10

10 years ago