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

Created at: 2022-05-10 20:01:50
开发语言: TypeScript
授权协议: NOASSERTION

天琴座

测试

尝试现场演示

安装

你可以使用 、 、 安装 Lyra:

npm
yarn
pnpm

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

用法

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

import { create, insert, search, remove } from '@nearform/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'
});

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

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

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

if

{
  elapsed: 99, // elapsed time is in microseconds
  hits: [
    {
      id: 'ckAOPGTA5qLXx0MgNr1Zy',
      quote: 'If you really look closely, most overnight successes took a long time.',
      author: 'Steve Jobs'
    },
    {
      id: 'fyl-_1veP78IO-wszP86Z',
      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: 111,
  hits: [
    {
      id: 'L1tpqQxc0c2djrSN2a6TJ',
      quote: "You miss 100% of the shots you don't take",
      author: 'Wayne Gretzky - Michael Scott'
    }
  ],
  count: 1
}

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

remove

remove(db, 'L1tpqQxc0c2djrSN2a6TJ');

许可证

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