SearchIndex Wyam.SearchIndex

Generates a JavaScript-based search index from the input documents.
This module generates a search index that can be imported into the JavaScript Lunr.js search engine. Each input document should either specify the SearchIndexItem metadata key or a delegate that returns a SearchIndexItem instance.

Package

This module exists in the Wyam.SearchIndex package which is not part of the core distribution. Add the following preprocessor directive to your configuration file to use it:
#n Wyam.SearchIndex
Alternativly, you can add all modules at once with the following preprocessor directive:
#n Wyam.All

Examples

The client-side JavaScript code for importing the search index should look something like this (assuming you have an HTML input with an ID of #search and a div with an ID of #search-results):
 function runSearch(query) {
     $("#search-results").empty();
     if (query.length < 2)
     {
         return;
     }
     var results = searchModule.search(query);
     var listHtml = "<ul>";
     listHtml += "<li><strong>Search Results</strong></li>";
     if (results.length == 0)
     {
         listHtml += "<li>No results found</li>";
     }
     else
     {
         for (var i = 0; i < results.length; ++i)
         {
             var res = results[i];
             listHtml += "<li><a href='" + res.url + "'>" + res.title + "</a></li>";
         }
     }
     listHtml += "</ul>";
     $("#search-results").append(listHtml);
 }

 $(document).ready(function() {
     $("#search").on('input propertychange paste', function() {
         runSearch($("#search").val());
     });
 });

Usage

  • SearchIndex(DocumentConfig searchIndexItem, FilePath stopwordsPath = null, bool enableStemming = false)

    Creates the search index by using a delegate that returns a SearchIndexItem instance for each input document.

    • searchIndexItem

      A delegate that should return a SearchIndexItem.

    • stopwordsPath

      A file to use that contains a set of stopwords.

    • enableStemming

      If set to true, stemming is enabled.

  • SearchIndex(FilePath stopwordsPath = null, bool enableStemming = false)

    Creates the search index by looking for a SearchIndexItem metadata key in each input document that contains a SearchIndexItem instance.

    • stopwordsPath

      A file to use that contains a set of stopwords.

    • enableStemming

      If set to true, stemming is enabled.

  • SearchIndex(string searchIndexItemMetadataKey, FilePath stopwordsPath = null, bool enableStemming = false)

    Creates the search index by looking for a specified metadata key in each input document that contains a SearchIndexItem instance.

    • searchIndexItemMetadataKey

      The metadata key that contains the SearchIndexItem instance.

    • stopwordsPath

      A file to use that contains a set of stopwords.

    • enableStemming

      If set to true, stemming is enabled.

Fluent Methods

Chain these methods together after the constructor to modify behavior.

  • EnableStemming(bool enableStemming = true)

    Controls whether stemming is turned on.

    • enableStemming

      If set to true, stemming is enabled.

  • IncludeHost(bool includeHost = true)

    Indicates whether the host should be automatically included in generated links.

    • includeHost

      true to include the host.

  • WithPath(ContextConfig path)

    Controls the output path of the result document. If this is specified, the resulting Wyam.Common.IO.FilePath will be used to set a WritePath metadata value.

    • path

      A delegate that should return a Wyam.Common.IO.FilePath to the output file.

  • WithPath(FilePath path)

    Controls the output path of the result document. If this is specified, the resulting Wyam.Common.IO.FilePath will be used to set a WritePath metadata value.

    • path

      The path to the output file.

  • WithScript(Func<StringBuilder, IExecutionContext, string> script)

    This allows you to customize the Lunr.js JavaScript that this module creates.

    • script

      A script transformation function. The System.Text.StringBuilder contains the generated script content. You can manipulate as appropriate and then return the final script as a string.

  • WithStopwordsPath(FilePath stopwordsPath)

    Sets the path to a stopwords file.

    • stopwordsPath

      A file to use that contains a set of stopwords.

Input Metadata

The metadata values listed below apply to individual documents and are typically set from front matter (with just the name of the key) and used as inputs to the module to control behavior.

  • SearchIndexKeys.SearchIndexItem: Wyam.SearchIndex.SearchIndexKeys.SearchIndexItem

    Contains a Wyam.SearchIndex.SearchIndexKeys.SearchIndexItem that can be used to provide specific search index information for a given document.

Output Metadata

The metadata values listed below apply to individual documents and are created and set by the module as indicated in their descriptions.

  • Keys.RelativeFilePath: Wyam.Common.IO.FilePath

    Relative path to the output search index file.

  • Keys.WritePath: Wyam.Common.IO.FilePath

    The path to use when writing the file.

GitHub