diff --git a/README.md b/README.md index 21191ab..490a3b0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. diff --git a/index.js b/index.js index debbce8..6cfca11 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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]); }