Use a node module to index the document content, if you want to use this in a browser,index documents in command line, dump lunr index object to a file and use that file to init lunr.js in browser, the file may be very large if the document is large.
使用nodejs库 nodejieba 来分词,nodejieba本身是对c++库的封装,所以安装这个模块的时候需要c++环境
如果需要在前端使用搜索功能,需要先在后端处理文件,把索引保存成json格式,然后在前端引用
最初只是为了替换 gitbook 中的lunr依赖
注意:没有补全对应的测试用例
Lunr.js
A bit like Solr, but much smaller and not as bright.
Example
A very simple search index can be created using the following:
var idx = lunr(function () {
this.field('title', { boost: 10 })
this.field('body')
})
Adding documents to be indexed is as simple as:
var doc = {
"title": "黄金时代",
"body": "那一天我二十一岁,在我一生的黄金时代,我有好多奢望。我想爱,想吃,还想在一瞬间变成天上半明半暗的云。后来我才知道,生活就是个缓慢受锤的过程,人一天天老下去,奢望也一天天消失,最后变得像挨了锤的牛一样。可是我过二十一岁生日时没有预见到这一点。我觉得自己会永远生猛下去,什么也锤不了我。",
"author": "王小波",
"id": 1
}
idx.add(doc)
Then searching is as simple:
idx.search("生活")
This returns a list of matching documents with a score of how closely they match the search query:
[{
"ref": 1,
"score": 0.0011097036390631915
}]
API documentation is available, as well as a full working example.
Description
Lunr.js is a small, full-text search library for use in the browser. It indexes JSON documents and provides a simple search interface for retrieving documents that best match text queries.
Why
For web applications with all their data already sitting in the client, it makes sense to be able to search that data on the client too. It saves adding extra, compacted services on the server. A local search index will be quicker, there is no network overhead, and will remain available and useable even without a network connection.
Installation
Simply include the lunr.js source file in the page that you want to use it. Lunr.js is supported in all modern browsers.
Browsers that do not support ES5 will require a JavaScript shim for Lunr to work. You can either use Augment.js, ES5-Shim or any library that patches old browsers to provide an ES5 compatible JavaScript environment.
Contributing
See the CONTRIBUTING.mdown
file.