is a solution to bring Backbone Collections together with the browser fulltext search engine Lunr.js.
To use backlunr you first have to load the dependencies:
Then add the backlunr.mixin.js
script to your page.
To use the lunr.js
fulltext search within a collection you have to load backlunr.js
and use either Backbone.Collection.Lunr
instead of Backbone.Collection
.
If you want to use the mixin instead of the new base collection, just add the mixin with Cocktail as in 'backlunr.collection.js'
The only thing you have to define are the fields to index.
Config Example:
var User = Backbone.Model.extend();
var Users = Backbone.Collection.extend({
model: User,
lunroptions: {
fields: [
{ name: "firstname", boost: 10 },
{ name: "lastname", boost: 5 },
{ name: "email" },
{ name: "address" }
]
}
});
Cocktail.mixin(Users, Backbone.LunrMixin);
fields
( Array | Function ): An array of field definitions or a method called on init/reset which has to return the field definitions arrayfields[ n ]
( Object ): The field definitionfields[ n ].name
( String - required ): The field name within your model attributefields[ n ].boost
( Number - optional: default=1 ): A boost factor for results in this field.
Attension: Currently Lunr.js does not support an indexing of numeric values. To use numeric values backlunr converts all fields to strings.
To search within your collection you just have to call .search( "[ term ]" )
.
Search Example:
// add some users
var usersCollection = new Users([
{ firstname: "Fritz", lastname: "Maier", "email": "fmaier@maier.com", "address": "Teststreet 123" },
{ firstname: "Hans", lastname: "Schubert", "email": "hschubert@maier.com", "address": "Checkway 987" }
]);
var result = usersCollection.search("Fritz");
// [ User( Fritz ) ]
// returns an array of matching models sorted by the result scoring
result.toJSON();
// [ {firstname: "Fritz", lastname: "Maier", "email": "fmaier@maier.com", "address": "Teststreet 123"} ]
// the single special method `toJSON` of the array returns all models converted by `model.toJSON()`.
.search()
is the only method added to the collection by Backlunr.
arguments:
term
( String - required ): the search termraw
( Boolean - optional: default=false ): Return the raw lunr result including thescore
and modelscid
asref
retuns ( Array ):
An array of matching models.
If you define raw=true
you will receive an array with object like { score: 0.789, ref: "c123" }
toJSON()
Ifraw=false
you can use the specialtoJSON
method to transform the aray of models to an array of model attributes.
- Create a solution to search in multiple collections by hooking them to a global search module. If a search has been done the result will return all matching models in al hooked collection including the type ( Collection name )
backlunr
is work in progress. Your ideas, suggestions etc. are very welcome.
- Ported from CoffeeScript to JavaScript to add mixin functionality
- Updated test to Lunr.js
v0.4.0
, jqueryv2.0.3
, mochav0.2.0
and expectv1.12.0
. Backlunr test already working fine but 15% faster. - Compiled scripts with new coffee version
v1.6.3
- Added minified version
- Added support for numeric values by converting every field-value to a string
- Initial version
(The MIT License)
Copyright (c) 2010 TCS <dev (at) tcs.de>
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.