lyra - 🌌用 TypeScript 编写的快速、内存中、容错的全文搜索引擎。

Created at: 2022-05-10 20:01:50
Language: TypeScript
License: NOASSERTION

天琴座,边缘搜索体验

测试

加入 Lyra 的 Slack 频道

如果你需要更多信息、帮助或想提供有关 Lyra 的一般反馈,请加入 Lyra Slack 频道

安装

你可以使用 、 、 : 安装 Lyra:

npm
yarn
pnpm

npm i @lyrasearch/lyra
yarn add @lyrasearch/lyra
pnpm add @lyrasearch/lyra

或者直接在浏览器模块中导入:

<html>
  <body>
    <script type="module">
      import { create, search, insert } from "https://unpkg.com/@lyrasearch/lyra@latest/dist/esm/src/lyra.js";

      // ...
    </script>
  </body>
</html>

有关使用 Lyra 打包的各种构建的详细信息,请参阅构建

https://docs.lyrasearch.io/ 阅读完整的文档。

用法

天琴座使用起来非常简单。首先要做的是创建一个新的数据库实例并设置索引架构:

import { create, insert, remove, search } from "@lyrasearch/lyra";

const db = create({
  schema: {
    author: "string",
    quote: "string",
  },
});

Lyra 将仅索引字符串属性,但允许你在需要时设置和存储其他数据。

创建数据库实例后,你可以开始添加一些文档:

insert(db, {
  quote:
    "It is during our darkest moments that we must focus to see the light.",
  author: "Aristotle",
});

insert(db, {
  quote:
    "If you really look closely, most overnight successes took a long time.",
  author: "Steve Jobs",
});

insert(db, {
  quote:
    "If you are not willing to risk the usual, you will have to settle for the ordinary.",
  author: "Jim Rohn",
});

insert(db, {
  quote: "You miss 100% of the shots you don't take",
  author: "Wayne Gretzky - Michael Scott",
});

请注意,该函数是同步的。如果你有大量文档,我们强烈建议改用该函数,以防止事件循环阻塞。此操作是异步的,并返回一个承诺:

insert
insertBatch

await insertBatch(db, [
  {
    quote:
      "It is during our darkest moments that we must focus to see the light.",
    author: "Aristotle",
  },
  {
    quote:
      "If you really look closely, most overnight successes took a long time.",
    author: "Steve Jobs",
  },
  {
    quote:
      "If you are not willing to risk the usual, you will have to settle for the ordinary.",
    author: "Jim Rohn",
  },
  {
    quote: "You miss 100% of the shots you don't take",
    author: "Wayne Gretzky - Michael Scott",
  },
]);

插入数据后,你终于可以开始查询数据库了。

const searchResult = search(db, {
  term: "if",
  properties: "*",
});

在上面的例子中,你将搜索包含单词的所有文档,在每个架构属性(AKA 索引)中查找:

if

{
  elapsed: 184541n, // Elapsed time in nanoseconds
  hits: [
    {
      id: '41013877-56',
      score: 0.025085832971998432,
      document: {
        quote: 'If you really look closely, most overnight successes took a long time.',
        author: 'Steve Jobs'
      }
    },
    {
      id: '41013877-107',
      score: 0.02315615351261394,
      document: {
        quote: 'If you are not willing to risk the usual, you will have to settle for the ordinary.',
        author: 'Jim Rohn'
      }
    }
  ],
  count: 2
}

你还可以将查找限制为特定属性:

const searchResult = search(db, {
  term: "Michael",
  properties: ["author"],
});

结果:

{
  elapsed: 172166n,
  hits: [
    {
      id: '41045799-144',
      score: 0.12041199826559248,
      document: {
        quote: "You miss 100% of the shots you don't take",
        author: 'Wayne Gretzky - Michael Scott'
      }
    }
  ],
  count: 1
}

如果需要,你还可以使用以下方法删除给定文档:

remove

remove(db, "41045799-144");

Lyra公开了一个内置函数,以人类可读的格式格式化经过的时间:

formatNanoseconds

import { formatNanoseconds } from "@lyrasearch/lyra";

const searchResult = search(db, {
  term: "if",
  properties: "*",
});

console.log(`Search took ${formatNanoseconds(searchResult.elapsed)}`);
// Search took 164μs

语言

Lyra支持多种语言。默认情况下,它将使用该语言,

english

你可以在 Lyra 初始化期间使用该属性指定其他语言。

defaultLanguage

默认情况下,Lyra将使用英语Porter Stemmer函数分析你的输入。
你可以将默认词干分析器替换为自定义词干分析器,也可以将默认 Lyra 安装附带的预构建词干分析器替换。

使用 ESM 的示例(请参阅下面的构建):

import { create } from "@lyrasearch/lyra";
import { stemmer } from "@lyrasearch/lyra/stemmer/it";

const db = create({
  schema: {
    author: "string",
    quote: "string",
  },
  defaultLanguage: "italian",
  tokenizer: {
    stemmingFn: stemmer,
  },
});

使用 CJS 的示例(请参阅下面的内部版本):

const { create } = require("@lyrasearch/lyra");
const { stemmer } = require("@lyrasearch/lyra/stemmer/it");

const db = create({
  schema: {
    author: "string",
    quote: "string",
  },
  defaultLanguage: "italian",
  tokenizer: {
    stemmingFn: stemmer,
  },
});

目前,Lyra支持23种语言和开箱即用的词干分析器:

  • 亚美尼亚语
  • 阿拉伯语
  • 丹麦语
  • 西班牙语
  • 英语
  • 芬兰语
  • 法语
  • 德语
  • 希腊语
  • 印地语
  • 匈牙利语
  • 印度尼西亚语
  • 意大利语
  • 爱尔兰语
  • 荷兰语
  • 尼泊尔语
  • 挪威语
  • 葡萄牙语
  • 罗马尼亚
  • 俄语
  • 塞尔维亚语
  • 瑞典语
  • 土耳其语

建立

Lyra与ES模块,CommonJS和通用浏览器构建打包在一起。

在大多数情况下,只需导入或要求 @lyrasearch/lyra,你的环境就会选择最合适的构建在某些情况下,你可能需要或某些文件(例如词干分析器)。Lyra 软件包中包含以下构建:

import
require

路径
dist/esm
使用 ES 模块构建 ESNext。将其用于大多数现代应用程序(节点.js、vite.js、浏览器模块等)
dist/cjs
ESNext build using CommonJS ()。将其用于不支持 ES 模块的环境。
require
dist/browser
ES2019 使用 CommonJS () 构建。将其用于不支持现代 ESNext 语言结构的环境,例如 webpack 4(由 Expo 使用)。请注意,默认情况下,将在 Webpack 环境(如 Next.js)中选择此构建。
require

在处理异步操作时,钩子是一种在工作流期间拦截和执行操作的出色机制。Lyra原生支持钩子。该函数允许你指定一系列钩子。

create

import { create } from "@lyrasearch/lyra";

const db = create({
  schema: {},
  hooks: {
    // HERE
  },
});

重要提示:钩子在与主函数执行相同的上下文中运行。这意味着,如果你的钩子需要 X 毫秒才能解析,Lyra 函数将采用 X + Y(其中 Y = Lyra 操作)。

后插入挂钩

在将文档插入数据库后调用挂钩。将使用插入文档的 调用。

afterInsert
hook
id

例:

import { create, insertWithHooks } from "@lyrasearch/lyra";

async function hook1 (id: string): Promise<void> {
  // called before hook2
}

function hook2 (id: string): void {
  // ...
}

const db = create({
  schema: {
    author: "string",
    quote: "string",
  },
  hooks: {
    afterInsert: [hook1, hook2],
  },
});

await insertWithHooks(db, { author: "test", quote: "test" })

许可证

Lyra在Apache 2.0许可证下获得许可。