A high-level MongoDB-like API for ElasticSearch
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.jshintrc
.npmignore
.travis.yml
LICENSE
README.md
gulpfile.js
package.json

README.md

ElasticStore

ElasticStore provides a high-level MongoDB-like API for ElasticSearch. It aims to be minimal.

Installation

$ npm install elasticstore

Usage

var elasticstore = require('elasticstore');
var store = new elasticstore.Store();

function Post(data) {
  elasticstore.Node.call(this, data);
}

Post._type = 'post';
Post._schema = {
  title: String,
  created: {type: Date, default: Date.now}
};

require('util').inherits(Post, Node);

store.registerType(Post);

Post.insert({
  title: 'Hello world'
}).then(function(post) {
  console.log(post);
});

Design

A store is corresponding to an index. A model is corresponding to a document type in ElasticSearch.

Store methods

  • connect(), connect to ElasticSearch
  • model(name, schema), create a new model
  • registerType(Type), to create a subclass of Node, include a call to util.inherits(Type, Node).

Node methods

Access the Node class by store.Node.

  • getContext

  • getStore

  • getStoreClient

  • getIndex

  • getType

  • find(q), find all matching document. if q is null, return all documents

  • findById(id), (get is aliased) find the document with the given id

  • save(doc_or_docs_array), insert a new document to this model

  • updateById(id, values)

  • removeById(id), (remove is aliased) remove all documents in this model

  • count(q), return the number of documents matching the query

  • createInstance(data), create a new document of this model

To create a subclass, use the following snippet:

function Post() {
  Node.call(this);
}
Node.registerType(Post);

Then you need to access the methods by Post.findById etc.

Model instance methods

You can create a new Model instance by new Model().

  • getContext()
  • save()
  • remove()
  • update()

Test

ORG_PKG=/Users/cllu/Projects/OrganizedApp/ ./vendor/elasticsearch-1.4.4/bin/elasticsearch -D es.config=/Users/cllu/Projects/OrganizedApp/etc/elasticsearch/elasticsearch.yml

$ gulp test

Gothas

Index refresh

After ElasticSearch indexes a document, it needs some time before the document is searchable. To simplify the logic, the save() method has a refresh optional parameter which is True by default.

Mapping

Ensure to set index.mapping.coerce: false so that ES will not try to coerce numerical values.