lunr-mutable-indexes
Version:
Mutable indexes for lunr.js 2.x or greater
83 lines (75 loc) • 2.43 kB
JavaScript
/*
* Copyright 2018 Rob Hoelz <rob AT hoelz.ro>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/**
* A convenience function for configuring and constructing
* a new mutable lunr Index.
*
* A lunr.MutableBuilder instance is created and the pipeline setup
* with a trimmer, stop word filter and stemmer.
*
* This mutable builder object is yielded to the configuration function
* that is passed as a parameter, allowing the list of fields
* and other builder parameters to be customised.
*
* All documents _must_ be added within the passed config function, but
* you can always update the index later. ;)
*
* @example
* var idx = lunrMutable(function () {
* this.field('title')
* this.field('body')
* this.ref('id')
*
* documents.forEach(function (doc) {
* this.add(doc)
* }, this)
* })
*
* index.add({
* "title": "new title",
* "body": "new body",
* "id": "2"
* })
*
* index.remove({ id: "1" });
*
* index.update({
* "body": "change",
* "id": "2"
* })
*/
var lunrMutable = function (config) {
var builder = new MutableBuilder();
builder.pipeline.add(
lunr.trimmer,
lunr.stopWordFilter,
lunr.stemmer
)
builder.searchPipeline.add(
lunr.stemmer
)
config.call(builder, builder)
return builder.build()
}
lunrMutable.version = "2.3.2"
lunrMutable.Builder = MutableBuilder
lunrMutable.Index = MutableIndex