在原始搜索速度方面,FlexSearch 胜过目前所有的搜索库,并且还提供了灵活的搜索功能,如多字段搜索、音素转换或部分匹配。

根据所使用的选项,它还提供了最内存高效的索引。FlexSearch 引入了一种名为“上下文索引”的新评分算法,基于一个预评分的词典体系结构,实际上执行查询速度比其他库快了高达 1,000,000 倍。FlexSearch 还为您提供了一种非阻塞的异步处理模型,以及 Web Workers,在专用平衡线程中并行执行索引的任何更新或查询。

支持的平台:

  • 浏览器
  • Node.js

库比较《格列佛游记》:

  • 性能基准
  • 评分基准
  • 内存消耗

插件(外部项目):

构建文件 类型 下载链接
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)

  1. 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种类型的索引:

  1. 索引是一个平坦的高性能索引,它存储id-content对。
  2. Worker / WorkerIndex 也是一个平坦的索引,它存储id-content对,但作为专用工作线程在后台运行。
  3. 文档是一个多字段索引,可以存储复杂的JSON文档(也可以由工作索引组成)。 根据您的情况,大多数人可能只需要其中之一。

浏览器

遗留的ES5脚本标签(打包)

  1. <script src="node_modules/flexsearch/dist/flexsearch.bundle.min.js"></script>
  2. <script>
  3. // FlexSearch is available on window.FlexSearch
  4. // Access FlexSearch static methods via bundled export (static class methods of FlexSearch)
  5. const index = FlexSearch.Index(options);
  6. const document = FlexSearch.Document(options);
  7. const worker = FlexSearch.Worker(options);
  8. </script>

ESM/ES6 Modules:

  1. <script type="module">
  2. // FlexSearch is NOT available on window.FlexSearch
  3. // Access FlexSearch static methods by importing them explicitly
  4. import Index from "./node_modules/flexsearch/dist/module/index";
  5. import Document from "./node_modules/flexsearch/dist/module/document";
  6. import Worker from "./node_modules/flexsearch/dist/module/worker";
  7. const index = new Index(options);
  8. const document = new Document(options);
  9. const worker = new Worker(options);
  10. </script>

ESM/ES6 Bundled Module:

  1. <script type="module">
  2. // FlexSearch is NOT available on window.FlexSearch
  3. // Access FlexSearch static methods via bundled export (static class methods of FlexSearch)
  4. import FlexSearch from "./node_modules/flexsearch/dist/flexsearch.bundle.module.min.js";
  5. const index = FlexSearch.Index(options);
  6. const document = FlexSearch.Document(options);
  7. const worker = FlexSearch.Worker(options);
  8. </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

在您的代码中包含如下内容:

  1. const { Index, Document, Worker } = require("flexsearch");
  2. const index = new Index(options);
  3. const document = new Document(options);
  4. const worker = new Worker(options);

或者

  1. const FlexSearch = require("flexsearch");
  2. const index = new FlexSearch.Index(options);
  3. const document = new FlexSearch.Document(options);
  4. const worker = new FlexSearch.Worker(options);

基本用法和变体

  1. index.add(id, text);
  2. index.search(text);
  3. index.search(text, limit);
  4. index.search(text, options);
  5. index.search(text, limit, options);
  6. index.search(options);
  1. document.add(doc);
  2. document.add(id, doc);
  3. document.search(text);
  4. document.search(text, limit);
  5. document.search(text, options);
  6. document.search(text, limit, options);
  7. document.search(options);
  1. worker.add(id, text);
  2. worker.search(text);
  3. worker.search(text, limit);
  4. worker.search(text, options);
  5. worker.search(text, limit, options);
  6. worker.search(text, limit, options, callback);
  7. worker.search(options);

该工作线程继承自类型 Index,并不继承自类型 Document。因此,WorkerIndex 基本上的工作方式类似于标准的 FlexSearch 索引。在文档中启用工作线程支持只需在创建时传递适当的选项 { worker: true }

对工作索引调用的每个方法都被视为异步的。您将得到一个 Promise,或者您可以提供一个回调函数作为最后一个参数。