Skip to content

Commit ac38118

Browse files
Darklenolanlawson
authored andcommitted
Add lunr.js options when building the index
1 parent 7aaba6b commit ac38118

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ API
7777
* [Minimum should match (mm)](#minimum-should-match-mm)
7878
* [Filtering documents](#filtering-documents)
7979
* [Building the index](#building-the-index)
80+
* [Passing Options to lunr.js during build](#passing-options-to-lunr.js-during-build)
8081
* [Deleting the index](#deleting-the-index)
8182
* [Stale queries](#stale-queries)
8283
* [Other languages](#other-languages)
@@ -422,6 +423,23 @@ This will build up the index without querying it. If the database has changed si
422423

423424
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.
424425

426+
### Passing Options to lunr.js during build
427+
428+
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:
429+
430+
```js
431+
pouch.search({
432+
fields: ['title', 'text'],
433+
build: true,
434+
lunrOptions: function(){
435+
this.pipeline.add(function (token, tokenIndex, tokens) {
436+
// text processing in here
437+
})
438+
}
439+
});
440+
```
441+
More info on the lunr.js methods available here: http://lunrjs.com/docs/
442+
425443
### Deleting the index
426444

427445
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.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ exports.search = utils.toPromise(function (opts, callback) {
102102
var stale = opts.stale;
103103
var limit = opts.limit;
104104
var build = opts.build;
105+
var lunrOptions = build ? opts.lunrOptions : null;
105106
var skip = opts.skip || 0;
106107
var language = opts.language || 'en';
107108
var filter = opts.filter;
@@ -125,7 +126,7 @@ exports.search = utils.toPromise(function (opts, callback) {
125126

126127
var index = indexes[language];
127128
if (!index) {
128-
index = indexes[language] = lunr();
129+
index = indexes[language] = lunr(lunrOptions);
129130
if (language !== 'en') {
130131
index.use(global.lunr[language]);
131132
}

0 commit comments

Comments
 (0)