Skip to content

Commit

Permalink
Pass lunr options when building the index
Browse files Browse the repository at this point in the history
  • Loading branch information
knubie committed Sep 22, 2023
1 parent 3c8be30 commit 43146d8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PouchDB Quick Search
Fork notes:
- Applies pouchdb-community/pouchdb-quick-search#99 Change md5 dependency
- Applies pouchdb-community/pouchdb-quick-search#85 check for pouchdb through global variable
- Applies pouchdb-community/pouchdb-quick-search#90 Add lunr.js options when building the index


[![Build Status](https://travis-ci.org/nolanlawson/pouchdb-quick-search.svg)](https://travis-ci.org/nolanlawson/pouchdb-quick-search)
Expand Down Expand Up @@ -82,6 +83,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 @@ -428,6 +430,24 @@ 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
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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 @@ -142,11 +143,12 @@ exports.search = utils.toPromise(function (opts, callback) {
var indexPipeline;
if (!index) {
index = indexes[language] = lunr(function() {
lunrOptions && lunrOptions.bind(this)(lunr);
indexPipeline = this.pipeline;
if (Array.isArray(language)) {
this.use(global.lunr['multiLanguage'].apply(this, language));
this.use(lunr['multiLanguage'].apply(this, language));
} else if (language !== 'en') {
this.use(global.lunr[language]);
this.use(lunr[language]);
}
});
index.searchPipeline = index.pipeline;
Expand Down
21 changes: 16 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ chai.use(require("chai-as-promised"));
var should = chai.should(); // var should = chai.should();
require('bluebird'); // var Promise = require('bluebird');

// have to make this global for the languages plugin, sadly
global.lunr = require('lunr');
require('./deps/lunr.stemmer.support')(global.lunr);
require('./deps/lunr.fr')(global.lunr);
require('./deps/lunr.multi')(global.lunr);
var lunrStemmer = require('./deps/lunr.stemmer.support');
var lunrFr = require('./deps/lunr.fr');
var lunrMulti = require('./deps/lunr.multi');

var dbs;
if (process.browser) {
dbs = 'testdb' + Math.random();
Expand Down Expand Up @@ -643,6 +642,18 @@ function tests(dbName, dbType) {

it('indexes english and french simultaneously', function () {
return db.bulkDocs({docs: docs7}).then(function () {
var opts = {
fields: ['text'],
language: 'fr',
build: true,
lunrOptions: function(lunr) {
lunrStemmer(lunr);
lunrFr(lunr);
lunrMulti(lunr);
}
};
return db.search(opts);
}).then(function (res) {
var opts = {
fields: ['text'],
query: 'parlera',
Expand Down

0 comments on commit 43146d8

Please sign in to comment.