在原始搜索速度方面,FlexSearch 胜过目前所有的搜索库,并且还提供了灵活的搜索功能,如多字段搜索、音素转换或部分匹配。
根据所使用的选项,它还提供了最内存高效的索引。FlexSearch 引入了一种名为“上下文索引”的新评分算法,基于一个预评分的词典体系结构,实际上执行查询速度比其他库快了高达 1,000,000 倍。FlexSearch 还为您提供了一种非阻塞的异步处理模型,以及 Web Workers,在专用平衡线程中并行执行索引的任何更新或查询。
支持的平台:
- 浏览器
- Node.js
库比较《格列佛游记》:
- 性能基准
- 评分基准
- 内存消耗
插件(外部项目):
- React:https://github.com/angeloashmore/react-use-flexsearch
- Vue:https://github.com/Noction/vue-use-flexsearch
- Gatsby:https://www.gatsbyjs.org/packages/gatsby-plugin-flexsearch/
构建文件 | 类型 | 下载链接 |
---|---|---|
flexsearch.bundle.js | 下载 | https://rawcdn.githack.com/nextapps-de/flexsearch/0.7.31/dist/flexsearch.bundle.js |
flexsearch.light.js | 下载 | https://rawcdn.githack.com/nextapps-de/flexsearch/0.7.31/dist/flexsearch.light.js |
flexsearch.compact.js | 下载 | https://rawcdn.githack.com/nextapps-de/flexsearch/0.7.31/dist/flexsearch.compact.js |
flexsearch.es5.js * | 下载 | https://rawcdn.githack.com/nextapps-de/flexsearch/0.7.31/dist/flexsearch.es5.js |
ES6 Modules | 下载 | 此Github仓库的/dist/module/文件夹 |
- bundle “flexsearch.es5.js” 包含了 EcmaScript 5 支持的 polyfills。
获取最新版本(NPM)
npm install flexsearch
比较 Web-Bundles
Node.js 包含了 flexsearch.bundle.js 的所有功能。
Feature | flexsearch.bundle.js | flexsearch.compact.js | flexsearch.light.js |
---|---|---|---|
Presets | ✓ | ✓ | - |
Async Search | ✓ | ✓ | - |
Workers (Web + Node.js) | ✓ | - | - |
Contextual Indexes | ✓ | ✓ | ✓ |
Index Documents (Field-Search) | ✓ | ✓ | - |
Document Store | ✓ | ✓ | - |
Partial Matching | ✓ | ✓ | ✓ |
Relevance Scoring | ✓ | ✓ | ✓ |
Auto-Balanced Cache by Popularity | ✓ | - | - |
Tags | ✓ | - | - |
Suggestions | ✓ | ✓ | - |
Phonetic Matching | ✓ | ✓ | - |
Customizable Charset/Language | ✓ | ✓ | ✓ |
(Matcher, Encoder, Tokenizer, Stemmer, Filter, Split, RTL) | |||
Export / Import Indexes | ✓ | - | - |
File Size (gzip) | 6.8 kb | 5.3 kb | 2.9 kb |
性能基准(排名)
运行比较:性能基准《格列佛游记》
每秒操作次数越高越好,除了测试“内存”外,低者为佳。
排名 | 库 | 内存 | 查询(单个词项) | 查询(多个词项) | 查询(长字符串) | 查询(重复项) | 查询(未找到) |
---|---|---|---|---|---|---|---|
1 | FlexSearch | 17 | 7084129 | 1586856 | 511585 | 2017142 | 3202006 |
2 | JSii | 27 | 6564 | 158149 | 61290 | 95098 | 534109 |
3 | Wade | 424 | 20471 | 78780 | 16693 | 225824 | 213754 |
4 | JS Search | 193 | 8221 | 64034 | 10377 | 95830 | 167605 |
5 | Elasticlunr.js | 646 | 5412 | 7573 | 2865 | 23786 | 13982 |
6 | BulkSearch | 1021 | 3069 | 3141 | 3333 | 3265 | 21825569 |
7 | MiniSearch | 24348 | 4406 | 10945 | 72 | 39989 | 17624 |
8 | bm25 | 15719 | 1429 | 789 | 366 | 884 | 1823 |
9 | Lunr.js | 2219 | 255 | 271 | 272 | 266 | 267 |
10 | FuzzySearch | 157373 | 53 | 38 | 15 | 32 | 43 |
11 | Fuse | 7641904 | 6 | 2 | 1 | 2 | 3 |
加载库
有3种类型的索引:
- 索引是一个平坦的高性能索引,它存储id-content对。
- Worker / WorkerIndex 也是一个平坦的索引,它存储id-content对,但作为专用工作线程在后台运行。
- 文档是一个多字段索引,可以存储复杂的JSON文档(也可以由工作索引组成)。 根据您的情况,大多数人可能只需要其中之一。
浏览器
遗留的ES5脚本标签(打包)
<script src="node_modules/flexsearch/dist/flexsearch.bundle.min.js"></script>
<script>
// FlexSearch is available on window.FlexSearch
// Access FlexSearch static methods via bundled export (static class methods of FlexSearch)
const index = FlexSearch.Index(options);
const document = FlexSearch.Document(options);
const worker = FlexSearch.Worker(options);
</script>
ESM/ES6 Modules:
<script type="module">
// FlexSearch is NOT available on window.FlexSearch
// Access FlexSearch static methods by importing them explicitly
import Index from "./node_modules/flexsearch/dist/module/index";
import Document from "./node_modules/flexsearch/dist/module/document";
import Worker from "./node_modules/flexsearch/dist/module/worker";
const index = new Index(options);
const document = new Document(options);
const worker = new Worker(options);
</script>
ESM/ES6 Bundled Module:
<script type="module">
// FlexSearch is NOT available on window.FlexSearch
// Access FlexSearch static methods via bundled export (static class methods of FlexSearch)
import FlexSearch from "./node_modules/flexsearch/dist/flexsearch.bundle.module.min.js";
const index = FlexSearch.Index(options);
const document = FlexSearch.Document(options);
const worker = FlexSearch.Worker(options);
</script>
或者通过 CDN
<script src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@0.7.41/dist/flexsearch.bundle.min.js"></script>
AMD / CommonJS:
var FlexSearch = require("./node_modules/flexsearch/dist/flexsearch.bundle.min.js");
Node.js
npm install flexsearch
在您的代码中包含如下内容:
const { Index, Document, Worker } = require("flexsearch");
const index = new Index(options);
const document = new Document(options);
const worker = new Worker(options);
或者
const FlexSearch = require("flexsearch");
const index = new FlexSearch.Index(options);
const document = new FlexSearch.Document(options);
const worker = new FlexSearch.Worker(options);
基本用法和变体
index.add(id, text);
index.search(text);
index.search(text, limit);
index.search(text, options);
index.search(text, limit, options);
index.search(options);
document.add(doc);
document.add(id, doc);
document.search(text);
document.search(text, limit);
document.search(text, options);
document.search(text, limit, options);
document.search(options);
worker.add(id, text);
worker.search(text);
worker.search(text, limit);
worker.search(text, options);
worker.search(text, limit, options);
worker.search(text, limit, options, callback);
worker.search(options);
该工作线程继承自类型 Index,并不继承自类型 Document。因此,WorkerIndex 基本上的工作方式类似于标准的 FlexSearch 索引。在文档中启用工作线程支持只需在创建时传递适当的选项 { worker: true }
。
对工作索引调用的每个方法都被视为异步的。您将得到一个 Promise,或者您可以提供一个回调函数作为最后一个参数。