gatsby-plugin-lunr icon indicating copy to clipboard operation
gatsby-plugin-lunr copied to clipboard

Multiple search index per site

Open JohannesHoffmann opened this issue 5 years ago • 1 comments

I have a bigger site with different unrelated sections. Each sections content is searchable. So i tried to create multiple indexes like this:

gatsby-config.json

module.exports = {
    plugins: [
        {
            resolve: `gatsby-plugin-lunr`,
            options: {
                languages: [{name: 'de',},],
                fields: [
                    { name: 'name', store: true, attributes: { boost: 20 } },
                    { name: 'content' },
                    { name: 'url', store: true },
                ],
                resolvers: {
                    SectionOneJson: {
                        name: node => node.name,
                        content: node => node.content,
                        url: node => node.url,
                    },
                },
                filename: 'section_one_index.json',
            },
        },
        {
            resolve: `gatsby-plugin-lunr`,
            options: {
                languages: [{name: 'de',},],
                fields: [
                    { name: 'title', store: true, attributes: { boost: 20 } },
                    { name: 'content' },
                    { name: 'url', store: true },
                ],
                resolvers: {
                    SectionTwoJson: {
                        title: node => node.title,
                        content: node => node.content,
                        url: node => node.url,
                    },
                },
                filename: 'section_two_index.json',
            },
        },
    ]
}

The second index overwrites the window.LUNR clientside.

Is there a way to have multiple indexes? You can maybe access them like window.__LUNR__['sectionTwo']['en']

JohannesHoffmann avatar Mar 26 '20 10:03 JohannesHoffmann

An easy way to achieve this could be look like this:

gatsby-browser.js

exports.onClientEntry = (args, {
  languages,
  indexname = "",
  filename = "search_index.json",
  fetchOptions = {}
}) => {
  enhanceLunr(lunr, languages);
  const lunrIndexName = indexname ? "__LUNR__" + indexname + "__" : "__LUNR__"
  window[lunrIndexName] = window[lunrIndexName] || {};

  window[lunrIndexName].__loaded = fetch(`${__PATH_PREFIX__}/${filename}`, fetchOptions).then(function (response) {
    return response.json();
  }).then(function (fullIndex) {
    window[lunrIndexName] = Object.keys(fullIndex).reduce((prev, key) => (0, _objectSpread2.default)({}, prev, {
      [key]: {
        index: lunr.Index.load(fullIndex[key].index),
        store: fullIndex[key].store
      }
    }), {
      __loaded: window[lunrIndexName].__loaded
    });
    return window[lunrIndexName];
  }).catch(e => {
    console.log("Failed fetch search index");
    throw e;
  });
};

if the indexname in plugin option is missing it is default window.__LUNR__ else its window.__LUNR__myindexname__

Could this be a possible feature for this plugin?

JohannesHoffmann avatar Mar 26 '20 11:03 JohannesHoffmann