olivernn / lunr.js

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

Overwriting existing registered function: lunr-multi-trimmer-en-it #339

Closed crystalfp closed 6 years ago

crystalfp commented 6 years ago

I'm using lunr v2 to index a hundred documents (Italian and English). Each time the user requests a full text search I rebuild the index if the set of documents has changed. The only small problem is that on subsequent requests I receive the warning: Overwriting existing registered function: lunr-multi-trimmer-en-it. Here is the code:

"use strict";

const lunr = require("lunr");
require("lunr-languages/lunr.stemmer.support")(lunr);
require("lunr-languages/lunr.multi")(lunr);
require("lunr-languages/lunr.it")(lunr);

// > Access to the index for full text search
let fullTextIndex;

// > Generate the index for full text search
exports.regenerateFullTextIndex = function(documents) {

   fullTextIndex = lunr(function() {

      this.use(lunr.multiLanguage("en", "it"));

      this.ref("id");
      this.field("title");
      this.field("body");

      const len = documents.length;
      for(let i = 0; i < len; ++i) {
         this.add({id: documents[i].id, title: documents[i].title, body: documents[i].text});
      }
   });
};

The calling function is very simple (hasFileListChanged is set true every time I modify the set of documents:

if(hasFileListChanged) {
   search.regenerateFullTextIndex(documents);
   hasFileListChanged = false;
}
// Do the full text search

Seems that the main lunr() function not only recreates the index, but also registers the language related function every time. I don't see any workaround to the warning message. Yes, I can simply ignore it, but sure a user will notice immediately. Maybe there is another method to delete and recreate the index.

Thanks! mario

hoelzro commented 6 years ago

Hi @crystalfp!

Have you tried lifting the call to lunr.multiLanguage out of the regeneration function in order to call it once, and passing its return value to this.use? Maybe that would clear up the error. To clarify, this is what I mean:

"use strict";

const lunr = require("lunr");
require("lunr-languages/lunr.stemmer.support")(lunr);
require("lunr-languages/lunr.multi")(lunr);
require("lunr-languages/lunr.it")(lunr);

let englishItalianSupport = lunr.multiLanguage('en', 'it'); // here

// > Access to the index for full text search
let fullTextIndex;

// > Generate the index for full text search
exports.regenerateFullTextIndex = function(documents) {

   fullTextIndex = lunr(function() {

      this.use(englishItalianSupport); // ...and here

      this.ref("id");
      this.field("title");
      this.field("body");

      const len = documents.length;
      for(let i = 0; i < len; ++i) {
         this.add({id: documents[i].id, title: documents[i].title, body: documents[i].text});
      }
   });
};
crystalfp commented 6 years ago

Thanks for the suggestion! But unfortunately the warning is still here: Overwriting existing registered function: lunr-multi-trimmer-en-it. Now I try to delete the index before regenerating it to see if anything change.

crystalfp commented 6 years ago

Apparently deleting the index before regenerating it prevents the small warning. That is:

    fullTextIndex = {};
    fullTextIndex = lunr(function() {...})

Strange, but it works. Thanks!

olivernn commented 6 years ago

As @hoelzro says, pulling the creation of the instance of lunr.multiLanguage up, so that it is only called once, should solve this warning, if I'm reading the code right.

Alternatively you can change how Lunr warns bye overwriting the lunr.utils.warn function.