If you have homebrew, you can install and run Elasticsearch with this one-liner:
brew install elasticsearch && elasticsearch
Or you can install Elasticsearch and run it in the background with this one-liner (assuming you have a
curl -o temp-es.zip && unzip temp-es.zip && rm temp-es.zip && mv elasticsearch-0.90.1 ~/bin/elasticsearch && ~/bin/elasticsearch/bin/elasticsearch
npm install elmongo
var mongoose =elmongo =Schema = mongooseSchemavar CatSchema =name: String// add the elmongo plugin to your collectionCatSchemavar Cat = mongoose
Now setup the search index with your data:
At this point your Cat schema has all the power of Elasticsearch. Here's how you can search on the model:
Cat// Perform a fuzzy searchCat// Search in specific fieldsCat// Paginate through the dataCat// Use `where` clauses to filter the dataCat
After the initial
.sync(), any Cat models you create/edit/delete with mongoose will be up-to-date in Elasticsearch. Also,
elmongo reindexes with zero downtime. This means that your data will always be available in Elasticsearch even if you're in the middle of reindexing.
Re-indexes your collection's data in Elasticsearch. After the first
.sync() call, Elasticsearch will be all setup with your collection's data. You can re-index your data anytime using this function. Re-indexing is done with zero downtime, so you can keep making search queries even while
.sync() is running, and your existing data will be searchable.
Perform a search query on your model. Any values you provide will override the default search options. The default options are:
query: '*'fields: '_all' // searches all fields by defaultfuzziness: 00 // exact match by defaultpageSize: 25page: 1
Gives your collection
.sync() methods, and keeps Elasticsearch up-to-date with your data when you insert/edit/delete documents with mongoose. Takes an optional
options object to tell
elmongo the url that Elasticsearch is running at. In
options you can specify:
protocol- http or https (defaults to
host- the host that Elasticsearch is running on (defaults to
port- the port that Elasticsearch is listening on (defaults to
prefix- adds a prefix to the model's search index, allowing you to have separate indices for the same collection on an Elasticsearch instance (defaults to no prefix)
url- allows you to specify the protocol, host and port by just passing in a url eg.
https://elasticsearch.mydomain.com:9300. The provided url must contain at least a host and port.
Suppose you have a test database and a development database both storing models in the
Cats collection, but you want them to share one Elasticsearch instance. With the
prefix option, you can separate out the indices used by
elmongo to store your data for test and development.
For tests, you could do something like:
And for development you could do something like:
This way, you can use the same
mongoose collections for test and for development, and you will have separate search indices for them (so you won't have situations like test data showing up in development search results).
Note: there is no need to specify a
prefix if you are using separate Elasticsearch hosts or ports. The
prefix is simply for cases where you are sharing a single Elasticsearch instance for multiple codebases.
You can use this function to make searches that are not limited to a specific collection. Use this to search across one or several collections at the same time (without making multiple roundtrips to Elasticsearch). The default options are the same as for
Model.search(), with one extra key:
collections. It defaults to searching all collections, but you can specify an array of collections to search on.
elmongo.search() will use
localhost:9200 (the default Elasticsearch configuration). To configure it to use a different url, use
Configure the Elasticsearch url that
elmongo uses to perform a search when
elmongo.search() is used.
options can specify the same keys as
elmongo.search.config() has no effect on the configuration for individual collections - to configure the url for collections, use
To add autocomplete functionality to your models, specify which fields you want autocomplete on in the schema:
var CatSchema =name: type: String autocomplete: trueage: type: Numberowner: type: ObjectId ref: 'Person'nicknames: type: String autocomplete: true// add the elmongo plugin to your collectionCatSchemavar Cat = mongoosevar kitty = name: 'simba'
Setup the search index using
Now you have autocomplete on
nicknames whenever you search on those fields:
(The MIT License)
Copyright (c) by Sold. email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.