Lunr.js

有点像Solr,但是要小得多但不那么“明亮”。(A bit like Solr, but much smaller and not as bright. )

Github stars Tracking Chart

Lunr.js是一个用于浏览器的小型全文搜索库。 它对JSON文档进行索引,并提供一个简单的搜索界面,用于检索最符合文本查询的文档。

为什么要用?

对于所有数据已经坐在客户端的Web应用程序,能够在客户端上搜索数据也是有道理的。 它可以节省在服务器上添加额外的,压缩的服务。 本地搜索索引将更快,没有网络开销,即使没有网络连接,它仍将可用和易用。

安装
只需在您要使用的页面中包含lunr.js源文件。 所有现代浏览器都支持Lunr.js。
或者,npm包也可用npm安装lunr。
不支持ES5的浏览器将需要一个JavaScript垫片才能使Lunr工作。 您可以使用Augment.js、ES5-Shim或任何修补旧浏览器的库来提供ES5兼容的JavaScript环境。

示例

可以使用以下方法创建一个非常简单的搜索索引:

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"  })})
然后搜索很简单
idx.search("love")

这将返回匹配文档的列表,其匹配搜索查询的匹配程度以及匹配的任何关联元数据的得分:

[  {    "ref": "1",    "score": 0.3535533905932737,    "matchData": {      "metadata": {        "love": {          "body": {}        }      }    }  }]

Overview

Name With Ownersunspot/sunspot
Primary LanguageJavaScript
Program languageMakefile (Language Count: 7)
Platform
License:MIT License
Release Count83
Last Release Namev2.6.0 (Posted on 2022-05-30 10:47:30)
First Release Namev0.0.2 (Posted on )
Created At2008-10-13 15:46:40
Pushed At2024-03-20 02:35:49
Last Commit At2023-12-28 21:34:03
Stargazers Count3k
Watchers Count32
Fork Count0.9k
Commits Count2k
Has Issues Enabled
Issues Count567
Issue Open Count145
Pull Requests Count267
Pull Requests Open Count21
Pull Requests Close Count187
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Lunr.js

Join the chat at https://gitter.im/olivernn/lunr.js

Build Status

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')
  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.

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.

Alternatively an npm package is also available npm install lunr.

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.

Features

  • 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

Contributing

See the CONTRIBUTING.md file.

To the top