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
|
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 optionsObject <optional>
{} Properties
Name Type Attributes Description domainstring <optional>
the domain of the API to make the request to
userstring <optional>
the username for the API
passstring <optional>
the password for the API
docsArray.<Object> <optional>
list of docs to build the index with
builderlunr.Builder <optional>
the builder to use for building the index
pluginsArray.<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 optionsObject <optional>
{} Properties
Name Type Attributes Description domainstring <optional>
the domain of the API to make the request to
userstring <optional>
the username for the API
passstring <optional>
the password for the API
builderlunr.Builder <optional>
the builder to use for building the index
pluginsArray.<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 optionsObject <optional>
{} Properties
Name Type Attributes Description domainstring <optional>
the domain of the API to make the request to
userstring <optional>
the username for the API
passstring <optional>
the password for the API
pluginsArray.<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 urlstring the URL of the endpoint
authObject <optional>
{} authentication details for the API
Properties
Name Type Attributes Description userstring <optional>
the username for the API
passstring <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 jsonstring 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 pluginsArray.<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 indexlunr.Index the index to be posted
urlstring the URL of the endpoint
authObject <optional>
{} authentication details for the API
Properties
Name Type Attributes Description userstring <optional>
the username for the API
passstring <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 querystring the query to search with
indexlunr.Index an index to use for the search
optionsObject <optional>
{} Properties
Name Type Attributes Default Description editDistancenumber <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 urlstring the URL of the endpoint
authObject <optional>
{} authentication details for the API
Properties
Name Type Attributes Description userstring <optional>
the username for the API
passstring <optional>
the password for the API
Returns:
lunr.Index -
importIndex(json) → {void}
-
Imports an exported lunr index and stores it.
Parameters:
Name Type Description jsonstring 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#buildIndexto build a fresh index. If the index does not already exist, it will be built asynchronously.Parameters:
Name Type Attributes Default Description urlstring the URL of the endpoint
authObject <optional>
{} authentication details for the API
Properties
Name Type Attributes Description userstring <optional>
the username for the API
passstring <optional>
the password for the API
Returns:
axios.Response -
search(query, optionsopt) → {Array.<Result>}
-
Searches the index with the given query.
Parameters:
Name Type Attributes Default Description querystring the query to search with
optionsObject <optional>
{} Properties
Name Type Attributes Description editDistancenumber <optional>
the edit distance to use for the search
Returns:
Array.<Result> -an array of search results
Type Definitions
-
Plugin
-
Properties:
Name Type Description pluginfunction a lunr builder plugin
fetchfunction a function to fetch documents
isSupportedfunction checks whether the plugin is supported in the current environment
-
Result
-
Properties:
Name Type Description documentObject the document that was matched
sourceObject describes the source of the document
Properties
Name Type Description typestring the type of the document source, examples are 'forum' or 'solution'
urlstring the URL that the document was fetched from
resultlunr.Index~Result the lunr search result