olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.89k stars 548 forks source link

Using languages with prebuilt indexes #392

Closed alexturpin closed 5 years ago

alexturpin commented 5 years ago

Hey all,

I'm looking for some clarification on prebuilt indexes with languages. I build indexes in both English and French. The code for when I build my indexes looks roughly like this:

Object.keys(docs).forEach(function(lang) {
    idx[lang] = lunr(function() {
        if (lang === 'fr') {
            this.use(lunr.fr);
        }

        // add fields and docs
    });
});

My question is, do I have to do something special to load the French language when loading the index and searching later on? The code that loads it looks like this:

index = lunr.Index.load(data[lang].index);

If I need to "set" this index to French, how should I do that? I tried doing index.use(lunr.fr) but index doesn't have a use method at this point, and since this doesn't use the builder I'm a bit at a loss.

Cheers

olivernn commented 5 years ago

You shouldn't have to do anything special to use lunr-languages when using pre-built indexes.

When loading the index you just must ensure that any pipeline functions are already registered before loading the index. This just means that you need to require the relevant language extension in the same context that you are going to load the index, for example:

var lunr = require("lunr")
require("lunr-languages/lunr.stemmer.support")(lunr)
require("lunr-languages/lunr.fr")(lunr)

Index = lunr.Index.load(data[lang].index)

Let me know if that doesn't work.

alexturpin commented 5 years ago

@olivernn thanks for your reply! Sorry for the lack of feedback on my part, I got moved to some other task at work. I wasn't experiencing any particular problems but simply wasn't sure if it needed to be loaded in a particular way. I will report back if I encounter any real issues.