Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lunr.js options when building the index #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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