olivernn / lunr.js

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

"Overwriting existing registered function" warning when creating multiple indexes #530

Open khromov opened 6 months ago

khromov commented 6 months ago

šŸ‘‹ This is probably me something wrong but I couldn't find the answer in the docs. I'm creating multiple indexes and have a creator function for them, like:

export const createLunrIndex = (records: SearchRecords, type: string) => {
  return lunr(function () {
    this.field('title');

    // Adding my custom plugin
    this.use(trimmer);

I also wrote a simple trimmer plugin.

export const trimmer = (builder: Builder) => {
  const trimmerFunction = function (token) {
    return token.update(function (s) {
      const trimmed = s
        .replace(/^[\.,;:()\s]+/, '')
        .replace(/[\.,;:()\s]+$/, '');
      return `${trimmed}`;
    });
  };

  // Register the pipeline function so the index can be serialised
  lunr.Pipeline.registerFunction(trimmerFunction, 'trimmerCustom');

  // Add the pipeline function to both the indexing pipeline and the search pipeline
  builder.pipeline.add(trimmerFunction);
  builder.searchPipeline.add(trimmerFunction);
};

Then I initialize two indexes:

const dogIndex = createLunrIndex(records1, 'dogs');
const catIndex = createLunrIndex(records1, 'cats');

I would expect that calling .use would init the plugin once per index, but instead I get:

Overwriting existing registered function: trimmerCustom

Is there some way to get around this issue?