lunrjs
https://github.com/olivernn/lunr.js
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.
小的全文搜索引擎,用于浏览器端的设计(虽然如此,但是其仍然支持nodejs平台运行 https://lunrjs.com/)。
索引对象时JSON文档, 提供简单的搜索接口,获取最匹配的文本搜索。
此功能类似mongo和elastic的服务器端全文搜索功能。
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.
优势:
1) 客户端拿到数据后,理应由客户单进行搜索。
2) 减轻服务器端压力。
3)减轻网络压力。
4)断网情况下仍然可以使用。 有利于web应用类型(PWA 小程序)。
功能
- Full text search support for 14 languages
- Boost terms at query time or boost entire documents at index time
- Scope searches to specific fields
- Fuzzy term matching with wildcards or edit distance
1)支持14中语言, 遗憾的是不支持中文。 https://lunrjs.com/guides/language_support.html
但是有大佬实现了中文支持: https://github.com/olivernn/lunr.js/issues/91
2)支持多项查询项,支持整个文档索引。
3)限定搜索字段。
4)模糊匹配。
EXAMPLE
https://lunrjs.com/docs/index.html
A very simple search index can be created using the following:
var idx = lunr(function () { this.field('title') this.field('body') this.add({ "title": "Twelfth-Night", "body": "If music be the food of love, play on: Give me excess of it…", "author": "William Shakespeare", "id": "1" }) })
Then searching is as simple:
idx.search("love")
This returns a list of matching documents with a score of how closely they match the search query as well as any associated metadata about the match:
[ { "ref": "1", "score": 0.3535533905932737, "matchData": { "metadata": { "love": { "body": {} } } } } ]
API documentation is available, as well as a full working example.
Online Demo
访问地址
https://olivernn.github.io/moonwalkers/
源码地址
https://github.com/olivernn/moonwalkers
功能说明:
对所管理的所有名人和介绍进行索引, 在页面搜索时候, 在索引中找到目标文档, 并把文档中搜索的关键字使用mark标黄显示。
项目中
1) 索引(docs/index.json)构建,在项目编译的时候生成。 build-index 脚本生成。
2)在网页运行, 加载索引(docs/index.json), 生成搜索引擎,
3) 用户索引时候,使用搜索引擎,插到相关文档, 更具ref索引号找到文档并显示。