tohidhabiby/redis-intended-json-search

dev-main 2024-05-29 12:11 UTC

This package is auto-updated.

Last update: 2024-09-29 13:03:38 UTC


README

这个Symfony包被设计为使用Redis堆栈和JSON格式存储和搜索数据的解决方案。它能够将数据以JSON格式存储在Redis中,并通过各种接口进行搜索和检索。

不仅这个包提供了数据存储和检索的功能,而且通过支持JSON缩进来提高了其性能和易用性。

如果你对redis堆栈不太熟悉,我来简单解释一下

  • index在RDBs中与table相同,你将创建的Index类与Laravel或Symfony中的modelentity相同。
  • createIndexdropIndex函数与迁移中的updown函数相同。
  • 这个包被创建成与eloquent或doctrine相同的ORM。

需求

  • PHP 8.0及以上。
  • PHP-Redis扩展。
  • Redis堆栈服务器7.2及以上。

安装

  • composer require tohidhabiby/redis-intended-json-search
  • TohidHabiby\RedisIntendedJsonSearch\TohidHabibyRedisIntendedJsonSearchBundle::class添加到你的config/bundles.php,或者将其作为服务添加到你的服务提供者中。
  • 将Redis堆栈详细信息添加到你的环境变量中(REDIS_HOSTREDIS_PORTREDIS_TIMEOUTREDIS_PERSISTENT_IDREDIS_RETRY_INTERVALREDIS_READ_TIMEOUT

用法

  • 索引

一开始,你必须为每种你想要存储的实体类型创建一个索引。它必须通过实现TohidHabiby\RedisIntendedJsonSearch\Indices\IndexInterface来实现。为了连接和运行索引文档上的查询,你必须为每个索引创建一个存储库。

  • 存储库

存储库必须由TohidHabiby\RedisIntendedJsonSearch\Repositories\AbstractRepository扩展。索引必须有一个属性,将它们连接到它们的存储库#[Index(BlogRepository::class)]

  • 字段类型

你可以定义任何属性到这些索引中,但它们必须定义为公开属性,并且如果你想对这些属性运行查询,你必须定义类型文件。你可以在src/FieldTypes目录中查看字段类型。

  • 如果你想存储数字或字符串的数组,你可以使用NumericFieldTextField定义属性。

***如果你想在一个对象内部存储另一个对象,你必须使用以下字段类型之一

  • 索引字段
  • CollectionField:用于定义对象的集合。

构建函数说明

  • createIndex(IndexInterface $index, ?int $childDeepLevel = 1): bool:要创建索引,你可以使用此函数,你必须首先定义索引,然后你可以运行查询(它看起来像RDB中的迁移)(参考)。
  • select(array $columns): BuilderInterface:仅选择一些特殊列(参考)。
  • save(IndexInterface $index): mixed:将文档存储到索引中。
  • sortBy(FieldTypeInterface $field, ?string $direction = 'asc'): BuilderInterface:对查询结果进行排序(参考)。
  • aggregateSort(FieldTypeInterface $field, ?string $direction = 'asc'): BuilderInterface : 这是一个非常有用的函数,使用此函数有一点棘手(参考
  • getInfo(IndexInterface $index): array : 检查索引是否存在并获取其信息。
  • limit(int $limit, ?int $offset = 0): BuilderInterface : 限制结果。
  • whereBetweenNumbers(FieldTypeInterface $fieldType, int|float|null $min = null, int|float|null $max = null): BuilderInterface : 此函数用于查找列值在最大值和最小值之间的文档(参考
  • groupBy(array $fields): BuilderInterface : 在列上执行分组查询时,必须与聚合函数一起使用(参考)。
  • get(): array : 将结果作为数组获取。
  • getIndexList(): array : 获取存在的索引列表。
  • getDocument(IndexInterface $index): string|bool : 根据ID获取文档。
  • dropIndex(IndexInterface $index): bool : 删除存在的索引。
  • whereInNumbers(FieldTypeInterface $field, array $values): BuilderInterface : 检查数字是否存在于数字数组中。
  • whereInArrayText(FieldTypeInterface $field, array $values): BuilderInterface : 检查文本是否存在于字符串数组中。
  • deleteDocument(IndexInterface $index): bool : 从索引中删除文档。
  • whereTextFieldLike(FieldTypeInterface $field, string $value): BuilderInterface : 这类似于SQL中的LIKE查询,考虑到Redis Stack不涵盖所有字符。如果您想搜索一些特定的字符串,最好将它们存储在另一个属性中,并以某种方式编码它们(例如base64),然后搜索编码后的字符串。
  • max(FieldTypeInterface $field): mixed : 获取索引中数字属性的最大值。
  • min(FieldTypeInterface $field): mixed : 获取索引中数字属性的最小值。
  • getFirst(): array : 获取结果中的第一个对象。

仓库函数说明

  • fill(array $data): RepositoryInterface : 使用数组准备简单的文档。
  • fillWithRelations(array $data): RepositoryInterface : 如果您想在另一个实体中有一个实体,您必须定义两个不同的索引,并且每当您想有一个父对象时,您必须在创建的仓库中实现此函数并在其中创建子对象(检查示例代码)。
  • saveWithRelations(array $data): IndexInterface : 与上一个函数类似,此函数将文档存储到索引中。
  • getDocumentById(int $id): array|bool : 无需更多解释。
  • getFieldByName(string $name): FieldTypeInterface : 获取定义在索引中的字段对象。
  • getRepositoryByIndexClass(string $className): RepositoryInterface : 根据索引类名称获取另一个仓库。
  • TohidHabiby\RedisIntendedJsonSearch\Repositories\AbstractRepository类中可以看到的其他函数,调用已解释的Builder的相关函数。