Skip to content

Commit

Permalink
Add lunr.js options when building the index
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkle authored and nolanlawson committed Feb 13, 2016
1 parent 186dbed commit ef9f634
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ API
* [Minimum should match (mm)](#minimum-should-match-mm)
* [Filtering documents](#filtering-documents)
* [Building the index](#building-the-index)
* [Passing Options to lunr.js during build](#passing-options-to-lunr.js-during-build)
* [Deleting the index](#deleting-the-index)
* [Stale queries](#stale-queries)
* [Other languages](#other-languages)
Expand Down Expand Up @@ -422,6 +423,23 @@ This will build up the index without querying it. If the database has changed si

You must at least provide the `fields` you want to index. If the language isn't English, you must pass in the `language` option. Boosts don't matter.

### Passing Options to lunr.js during build

You can pass in options to lunr.js during the index build by adding a `lunrOptions` option to the search. `lunrOptions` is a function whereby you can access the lunr instance via `this` from within the function. For example, if you wanted to add a function to the pipeline, you could do it like so:

```js
pouch.search({
fields: ['title', 'text'],
build: true,
lunrOptions: function(){
this.pipeline.add(function (token, tokenIndex, tokens) {
// text processing in here
})
}
});
```
More info on the lunr.js methods available here: http://lunrjs.com/docs/

### Deleting the index

If, for whatever reason, you need to delete an index that's been saved to disk, you can pass in `{destroy: true}` to the `search()` function, and instead of searching, it will delete the external search database.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ exports.search = utils.toPromise(function (opts, callback) {
var stale = opts.stale;
var limit = opts.limit;
var build = opts.build;
var lunrOptions = build ? opts.lunrOptions : null;
var skip = opts.skip || 0;
var language = opts.language || 'en';
var filter = opts.filter;
Expand All @@ -125,7 +126,7 @@ exports.search = utils.toPromise(function (opts, callback) {

var index = indexes[language];
if (!index) {
index = indexes[language] = lunr();
index = indexes[language] = lunr(lunrOptions);
if (language !== 'en') {
index.use(global.lunr[language]);
}
Expand Down

0 comments on commit ef9f634

Please sign in to comment.