uttori-search-provider-lunr
v2.1.1
Published
Uttori search provider powered by Lunr.
Downloads
3
Readme
Uttori Search Provider - Lunr
Uttori Search Provider powered by Lunr.js.
Install
npm install --save uttori-search-provider-lunr
API Reference
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. |
- SearchProvider
- new SearchProvider([config])
- .setup([config])
- .search(query, [limit]) ⇒ Array.<Object>
- .internalSearch(query, [limit]) ⇒ Array.<Object> ℗
- .indexAdd([documents])
- .indexUpdate([documents])
- .indexRemove([documents])
- .updateTermCount(query)
- .getPopularSearchTerms([limit]) ⇒ Array.<string>
- .shouldAugment([query], [fields]) ⇒ boolean
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. |
Example (Init SearchProvider)
const searchProvider = new SearchProvider();
const searchProvider = new SearchProvider({ lunr_locales: ['de', 'fr', 'jp'] });
searchProvider.setup([config])
Setup the search provider by building an index of documents.
Kind: instance method of SearchProvider
| Param | Type | Default | Description | | --- | --- | --- | --- | | [config] | Object | {} | Configuration object for setup. | | [config.documents] | Array.<UttoriDocument> | [] | An array of documents to be indexed. |
Example
searchProvider.setup({ documents: [{ slug: 'intro', title: 'Intro', content: '...', tags: ['intro'] }] });
searchProvider.search(query, [limit]) ⇒ 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: Array.<Object> - - Returns an array of search results no longer than limit.
| Param | Type | Default | Description | | --- | --- | --- | --- | | query | string | | The value to search for. | | [limit] | number | 100 | Limit for the number of returned documents. |
Example
searchProvider.search('matching');
➜ [{ ref: 'first-matching-document', ... }, { ref: 'another-matching-document', ... }, ...]
searchProvider.internalSearch(query, [limit]) ⇒ Array.<Object> ℗
Searches for documents matching the provided query with Lunr.
Kind: instance method of SearchProvider
Returns: Array.<Object> - - Returns an array of search results no longer than limit.
Access: private
| Param | Type | Default | Description | | --- | --- | --- | --- | | query | string | | The value to search for. | | [limit] | number | 100 | Limit for the number of returned documents. |
searchProvider.indexAdd([documents])
Unused: Adds documents to the index.
Kind: instance method of SearchProvider
| Param | Type | Default | Description | | --- | --- | --- | --- | | [documents] | Array.<UttoriDocument> | [] | An array of documents to be indexed. |
searchProvider.indexUpdate([documents])
Unused: Updates documents in the index.
Kind: instance method of SearchProvider
| Param | Type | Default | Description | | --- | --- | --- | --- | | [documents] | Array.<UttoriDocument> | [] | An array of documents to be indexed. |
searchProvider.indexRemove([documents])
Unused: Removes documents from the index.
Kind: instance method of SearchProvider
| Param | Type | Default | Description | | --- | --- | --- | --- | | [documents] | Array.<UttoriDocument> | [] | An array of documents to be indexed. |
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([limit]) ⇒ 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 | Default | Description | | --- | --- | --- | --- | | [limit] | number | 10 | Limit for the number of returned popular searches. |
Example
searchProvider.getPopularSearchTerms();
➜ ['popular', 'cool', 'helpful']
searchProvider.shouldAugment([query], [fields]) ⇒ boolean
Determines if a given search query needs to have fields augmented.
Kind: instance method of SearchProvider
Returns: boolean - - Returns true. Lunr does not return all of the required fields for displaing search results.
| Param | Type | Description | | --- | --- | --- | | [query] | string | Unused: the query to check for augmentation. | | [fields] | Array.<string> | Unused: the fields required to be on the documents. |
Tests
To run the test suite, first install the dependencies, then run npm test
:
npm install
npm test
DEBUG=Uttori* npm test