@uttori/search-provider-lunr

Uttori search provider powered by Lunr.

Usage no npm install needed!

<script type="module">
  import uttoriSearchProviderLunr from 'https://cdn.skypack.dev/@uttori/search-provider-lunr';
</script>

README

view on npm npm module downloads Build Status Dependency Status Coverage Status Tree-Shaking Support Dependency Count Minified + GZip Minified

Uttori Search Provider - Lunr

Uttori Search Provider powered by Lunr.js.

Install

npm install --save @uttori/search-provider-lunr

Config

{
  // Registration Events
  events: {
    search: ['search-query'],
    buildIndex: ['search-rebuild'],
    indexAdd: ['search-add'],
    indexUpdate: ['search-update'],
    indexRemove: ['search-remove'],
    getPopularSearchTerms: ['popular-search-terms'],
    validateConfig: ['validate-config'],
  },

  // A list of locales to add to Lunr
  // https://lunrjs.com/guides/language_support.html
  lunr_locales: [],

  // A list of slugs to ignore
  ignore_slugs: [],
}

API Reference

Classes

SearchProvider

Uttori Search Provider powered by Lunr.js.

Functions

debug() : function

Typedefs

UttoriDocument

SearchProvider

Uttori Search Provider powered by Lunr.js.

Kind: global class
Properties

Name Type Description
searchTerms object The collection of search terms and their counts.
index object The Lunr instance.

new SearchProvider([config])

Creates an instance of SearchProvider.

Param Type Default Description
[config] object {} Configuration object for the class.
[config.lunr_locales] Array.<string> [] A list of locales to add support for from lunr-languages.
[config.ignore_slugs] Array.<string> [] A list of slugs to not consider when indexing documents.

Example

const searchProvider = new SearchProvider();
const searchProvider = new SearchProvider({ lunr_locales: ['de', 'fr', 'jp'] });

searchProvider.validateConfig(config)

Validates the provided configuration for required entries and types.

Kind: instance method of SearchProvider

Param Type Description
config object A configuration object.
config.configKey object A configuration object specifically for this plugin.
config.configKey.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
config.configKey.lunr_locales Array.<string> A list of slugs to not consider when indexing documents.

searchProvider.buildIndex(context)

Rebuild the search index of documents.

Kind: instance method of SearchProvider

Param Type Description
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

Example

await searchProvider.buildIndex(context);

searchProvider.internalSearch(options, context) ⇒ Promise.<Array.<object>>

Searches for documents matching the provided query with Lunr.

Kind: instance method of SearchProvider
Returns: Promise.<Array.<object>> - - Returns an array of search results no longer than limit.
Access: private

Param Type Default Description
options object The passed in options.
options.query string The value to search for.
[options.limit] number 100 Limit for the number of returned documents.
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

searchProvider.search(options, context) ⇒ Promise.<Array.<object>>

External method for searching documents matching the provided query and updates the count for the query used. Uses the internalSearch method internally.

Kind: instance method of SearchProvider
Returns: Promise.<Array.<object>> - - Returns an array of search results no longer than limit.

Param Type Default Description
options object The passed in options.
options.query string The value to search for.
[options.limit] number 100 Limit for the number of returned documents.
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

Example

searchProvider.search('matching');
➜ [{ ref: 'first-matching-document', ... }, { ref: 'another-matching-document', ... }, ...]

searchProvider.indexAdd(documents, context)

Adds documents to the index. For this implementation, it is rebuilding the index.

Kind: instance method of SearchProvider

Param Type Description
documents Array.<UttoriDocument> Unused. An array of documents to be indexed.
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

searchProvider.indexUpdate(documents, context)

Updates documents in the index. For this implementation, it is rebuilding the index.

Kind: instance method of SearchProvider

Param Type Description
documents Array.<UttoriDocument> Unused. An array of documents to be indexed.
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

searchProvider.indexRemove(documents, context)

Removes documents from the index. For this implementation, it is rebuilding the index.

Kind: instance method of SearchProvider

Param Type Description
documents Array.<UttoriDocument> Unused. An array of documents to be indexed.
context object A Uttori-like context.
context.config object A provided configuration to use.
context.config.events object An object whose keys correspong to methods, and contents are events to listen for.
context.config.ignore_slugs Array.<string> A list of slugs to not consider when indexing documents.
context.hooks object An event system / hook system to use.
context.hooks.on function An event registration function.
context.hooks.fetch function An event dispatch function that returns an array of results.

searchProvider.updateTermCount(query)

Updates the search query in the query counts.

Kind: instance method of SearchProvider

Param Type Description
query string The query to increment.

searchProvider.getPopularSearchTerms(options) ⇒ Array.<string>

Returns the most popular search terms.

Kind: instance method of SearchProvider
Returns: Array.<string> - - Returns an array of search results no longer than limit.

Param Type Description
options object The passed in options.
options.limit number Limit for the number of returned popular searches.

Example

searchProvider.getPopularSearchTerms();
➜ ['popular', 'cool', 'helpful']

SearchProvider.configKey ⇒ string

The configuration key for plugin to look for in the provided configuration.

Kind: static property of SearchProvider
Returns: string - The configuration key.
Example

const config = { ...Plugin.defaultConfig(), ...context.config[Plugin.configKey] };

debug() : function

Kind: global function

UttoriDocument

Kind: global typedef
Properties

Name Type Default Description
slug string The unique identifier for the document.
[title] string "''" The unique identifier for the document.
[createDate] number | Date The creation date of the document.
[updateDate] number | Date The last date the document was updated.
[tags] Array.<string> [] The unique identifier for the document.
[customData] object {} Any extra meta data for the document.

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test
DEBUG=Uttori* npm test

Contributors

License