ElasticStore provides a high-level MongoDB-like API for ElasticSearch. It aims to be minimal.
$ npm install elasticstore
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);
});
A store is corresponding to an index. A model is corresponding to a document type in ElasticSearch.
connect()
, connect to ElasticSearchmodel(name, schema)
, create a new modelregisterType(Type)
, to create a subclass of Node, include a call toutil.inherits(Type, Node)
.
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.
You can create a new Model instance by new Model()
.
getContext()
save()
remove()
update()
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
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.
Ensure to set index.mapping.coerce: false
so that ES will not try to coerce numerical values.