Hybrid Couchdb/Elasticsearch engine for Resourceful
Start up an empty instance of CouchDB and an empty instance of Elasticsearch, then do this:
var resourceful = require'resourceful'Couchelastic = require'resourceful-couchelastic'Couchelastic;resourcefulenginesCouchelastic = Couchelastic; // patch in the engineresourcefuluse 'couchelastic'// CouchDB settingshost : 'localhost' // optional - defaults to localhostport : 5984 // optional - defaults to 5984database:'db-name' // requiredsearch:// Elasticsearch settingsindex:'my-index-name' // optional - defaults to db name;
From this point onwards each
Resource that you define will have its data stored in your specified
CouchDB cluster and have an Elasticsearch indexing river configured to index the data for searching.
Assuming you're already defining resources with Resourceful nothing more is required, but there are some additional options you might want to use. Everything specified within the options.search is passed straight into ES's Mapping API, allowing you fine-grained control over how that individual field is treated in the index.
var Creature = resourcefuldefine'Creature'thisstring'name' required:true ;thisstring'genus';thisnumber'population';thisnumber'legs'required: true // validation rulesminimum: 0search:type:'integer' // this tells ES what type of number this is;thisarray'eats'search: type:'string';thisstring'geography'search:analyzer:'snowball' // stem words to match word stem rather than exact form.;;
sync() method sets up your data sources for you. Until you sync you can
get() but not much more.
sync does the following:
You'll need to
sync after you make any significant design changes so that they're carried through to the schemas of both servers. Be aware that changing the data type of a field after data has already been indexed can be problematic.
Assuming you've defined a Creature as per the Resourceful docs you should be able to search your database in free text like this.
Creaturesearch"fur"// creatures is a convenient array of Creature objects that match the search// response contains the entire ES response including search results and metadata;
You can also search using the Elasticsearch query DSL, for example the following
fuzzy query would also match the word "oscar"
Actorsearchquery :fuzzy : text : "socar"// actors is an array// response is the full response containing metadata, e.g. facets;
Or using Resoureceful's parametric query method
If you have two data types related by a one-to-many foreign key you can do this (many-many not available yet):
Creaturesearch"name:wolf"Genusjoin creatures// creatures now contains:// creature.genus_id - foreign key// creature.genus - linked entity;;
If some of your Resource data is sensitive you might want to block it from being indexed
var Employee = resourcefuldefine'Employee'thisstring'name';thisnumber'salary'search:index:'no';;
TODO - clean up the object search API and allow faceting etc.
npm install resourceful-couchelastic`
$ npm test