LunrSearch(domain, user, pass, optionsopt)

new LunrSearch(domain, user, pass, optionsopt)

A class to handle building, importing, exporting, and searching a lunr index. It contains static and instance methods of each function so you can choose how to use it.

Parameters:
Name Type Attributes Default Description
domain string

the domain to use for all fetch requests

user string

the username to use for all fetch requests

pass string

the password to use for all fetch requests

options object <optional>
{}
Properties
Name Type Attributes Default Description
builder lunr.Builder <optional>

a custom builder to use instead of creating a new instance

plugins Array.<Plugin> <optional>

a list of plugins to use

editDistance number <optional>
1

a default edit distance to use in search queries

Examples

Instance usage

const builder = new lunr.Builder()   // if you need to,
builder.metadataWhitelist = ['tags'] // you can customize the builder that will be used internally

const lunrSearch = new LunrSearch('myfreshdeskdomain', 'freshdeskusername', 'freshdeskpassword', {
  builder, // pass the custom builder if needed
  plugins: [myCustomBuilderPlugin],
  editDistance: 2 // in case your users can't type very well :)
})

lunrSearch.buildIndex().then(() => mySearchBar.addEventListener('input', () => lunrSeach.search(mySearchBar.value))

Static usage

const opts = {domain: 'freshdeskdomain', user: 'username', pass: 'password'}
const builder = new lunr.Builder() // custom builder, etc.
const indexPromise = LunrSearch.buildIndex({builder, ...opts}) // you can pass a custom builder into the static methods too

indexPromise.then(() => mySearchBar.addEventListener('input', () => lunrSeach.search(mySearchBar.value))

Members

static VERSION :string

The package version.

builder :lunr.Builder

a custom builder to use

docs :Object

the fetched documents the keys are the document ID and the value is the document. useful for getting info about a search result.

domain :string

the domain to use for all fetch requests

editDistance :number

a default edit distance to use in search queries

Default Value:
  • 0

index :lunr.Index

The built lunr index.

pass :string

the password to use for all fetch requests

plugins :Array.<Plugin>

a list of plugins to use

user :string

the username to use for all fetch requests

Methods

async, static buildIndex(optionsopt) → {lunr.Index}

Builds an index and returns it. If no docs are supplied, they will be fetched.

Parameters:
Name Type Attributes Default Description
options Object <optional>
{}
Properties
Name Type Attributes Description
domain string <optional>

the domain of the API to make the request to

user string <optional>

the username for the API

pass string <optional>

the password for the API

docs Array.<Object> <optional>

list of docs to build the index with

builder lunr.Builder <optional>

the builder to use for building the index

plugins Array.<Plugin> <optional>

a list of plugins

Returns:
lunr.Index -

the built index

async, static exportIndex(optionsopt) → {string}

Builds then serializes and exports a lunr index.

Parameters:
Name Type Attributes Default Description
options Object <optional>
{}
Properties
Name Type Attributes Description
domain string <optional>

the domain of the API to make the request to

user string <optional>

the username for the API

pass string <optional>

the password for the API

builder lunr.Builder <optional>

the builder to use for building the index

plugins Array.<function()> <optional>

a list of plugins for the builder to use

Returns:
string -

the serialized index

async, static fetch(optionsopt) → {Array.<Object>}

Fetches some documents that are ready to be indexed.

Parameters:
Name Type Attributes Default Description
options Object <optional>
{}
Properties
Name Type Attributes Description
domain string <optional>

the domain of the API to make the request to

user string <optional>

the username for the API

pass string <optional>

the password for the API

plugins Array.<Plugin> <optional>

a list of plugins

Returns:
Array.<Object> -

the array of Objects to be indexed

async, static getIndex(url, authopt) → {lunr.Index}

Gets and imports an index fram a URL.

Parameters:
Name Type Attributes Default Description
url string

the URL of the endpoint

auth Object <optional>
{}

authentication details for the API

Properties
Name Type Attributes Description
user string <optional>

the username for the API

pass string <optional>

the password for the API

Returns:
lunr.Index

static importIndex(json) → {lunr.Index}

Imports an exported lunr index and returns it.

Parameters:
Name Type Description
json string

the stringified index to import

Returns:
lunr.Index

static isSupported(pluginsopt) → {Boolean}

Checks to see if certain functions are defined.

Parameters:
Name Type Attributes Description
plugins Array.<Plugin> <optional>

a list of plugins

Returns:
Boolean

async, static postIndex(index, url, authopt) → {axios.Response}

Posts an exported index to a URL.

Parameters:
Name Type Attributes Default Description
index lunr.Index

the index to be posted

url string

the URL of the endpoint

auth Object <optional>
{}

authentication details for the API

Properties
Name Type Attributes Description
user string <optional>

the username for the API

pass string <optional>

the password for the API

Returns:
axios.Response

static search(query, index, optionsopt) → {Array.<Result>}

Searches the given index with the given query. If query is falsey no search is performed.

Parameters:
Name Type Attributes Default Description
query string

the query to search with

index lunr.Index

an index to use for the search

options Object <optional>
{}
Properties
Name Type Attributes Default Description
editDistance number <optional>
1

the edit distance to use for the search

Returns:
Array.<Result> -

an array of search results

async buildIndex() → {lunr.Index}

Builds an index and stores it.

Returns:
lunr.Index -

the built index

async exportIndex() → {Promise.<string>}

Serializes and exports a lunr index. If the index exists locally, it will not be built. Call #buildIndex to build a fresh index. If the index does not already exist, it will be built asynchronously.

Returns:
Promise.<string> -

the serialized index

async fetch() → {Array.<Object>}

Fetches some documents that are ready to be indexed.

Returns:
Array.<Object> -

the array of Objects to be indexed

async getIndex(url, authopt) → {lunr.Index}

Gets and imports an index fram a URL.

Parameters:
Name Type Attributes Default Description
url string

the URL of the endpoint

auth Object <optional>
{}

authentication details for the API

Properties
Name Type Attributes Description
user string <optional>

the username for the API

pass string <optional>

the password for the API

Returns:
lunr.Index

importIndex(json) → {void}

Imports an exported lunr index and stores it.

Parameters:
Name Type Description
json string

the index to import as a json string

Returns:
void

async postIndex(url, authopt) → {axios.Response}

Posts index to a URL. If the index exists locally, it will not be built. Call LunrSearch#buildIndex to build a fresh index. If the index does not already exist, it will be built asynchronously.

Parameters:
Name Type Attributes Default Description
url string

the URL of the endpoint

auth Object <optional>
{}

authentication details for the API

Properties
Name Type Attributes Description
user string <optional>

the username for the API

pass string <optional>

the password for the API

Returns:
axios.Response

Searches the index with the given query.

Parameters:
Name Type Attributes Default Description
query string

the query to search with

options Object <optional>
{}
Properties
Name Type Attributes Description
editDistance number <optional>

the edit distance to use for the search

Returns:
Array.<Result> -

an array of search results

Type Definitions

Plugin

Properties:
Name Type Description
plugin function

a lunr builder plugin

fetch function

a function to fetch documents

isSupported function

checks whether the plugin is supported in the current environment

Result

Properties:
Name Type Description
document Object

the document that was matched

source Object

describes the source of the document

Properties
Name Type Description
type string

the type of the document source, examples are 'forum' or 'solution'

url string

the URL that the document was fetched from

result lunr.Index~Result

the lunr search result