当前位置:首页 > Web开发 > 正文

此功能类似mongo和elastic的服务器端全文搜索功能

2024-03-31 Web开发

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), 生成搜索引擎,

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30895.html