macfja/redisearch-integration

用于将 RediSearch 集成到 PHP 项目的辅助工具

2.0.0 2021-11-14 22:00 UTC

This package is auto-updated.

Last update: 2024-09-16 03:01:06 UTC


README

MacFJA/redisearch-integration 是一个小型库,用于简化使用 MacFJA/redisearch,它是一个 RediSearch 客户端。

安装

composer require macfja/redisearch-integration

使用

您将主要使用 ObjectManager (\MacFJA\RediSearch\Integration\ObjectManager) 和 ObjectRepository (\MacFJA\RediSearch\Integration\ObjectRepository)。

这两个接口包含在 ObjectWorker (\MacFJA\RediSearch\Integration\ObkectWorker) 类中,以简化使用。(如果您使用良好的依赖注入,您只需要接口)

use MacFJA\RediSearch\Integration\CompositeProvider;
use MacFJA\RediSearch\Integration\ObjectWorker;
use MacFJA\RediSearch\Redis\Client\ClientFacade;

$jsonProvider = new JsonProvider();
$jsonProvider->addJson(__DIR__.'/mappings.json');
$provider = new CompositeProvider(); // Annotation, Attribute, class implementation
$provider->addProvider($jsonProvider); // Add JSON as provider source

$client = (new ClientFacade())->getClient(/* .. */);

$manager = new ObjectWorker($client, $provider);
// ...
$manager->createIndex(\MyApp\Model\Product::class);
// $manager->flush(); If you need the index to create directly 
// ...
$entity = \MyApp\Model\Product();
$manager->persist($entity); // Add object in search and build suggestions
$manager->flush();
// ...
$searchResult = $client->execute(
    $manager->getSearchCommand(\MyApp\Model\Product::class)
        ->setQuery((new MacFJA\RediSearch\Query\Builder())->addNumericFacet('price', 0, 15)->render())
        ->setLimit(0, 12)
);
// ...
$suggestions = $manager->getSuggestions(\MyApp\Model\Product::class, 'shoe');

实体映射

有 5 种方式将 PHP 类映射到 RediSearch 对象。

  • 使用注解(类似于 Doctrine ORM)
  • 使用 PHP 8 属性
  • 使用 JSON 定义
  • 使用 XML 定义
  • 通过实现一个接口

注解和属性映射

如果一个类至少有一个字段映射,则认为它是有效的。

注解和 PHP 8 属性映射分别由 \MacFJA\RediSearch\Integration\Annotation\AnnotationProvider\MacFJA\RediSearch\Integration\Attribute\AttributeProvider 解析。

Index 映射

@Index(name, [prefix], [stopsWords])(或 PHP 8 属性的 #[Index(name, [prefix], [stopsWords])])允许您指定将类放置在哪个索引中。

如果映射丢失,将使用完整的类限定名(命名空间 + 类名)作为索引名称,不使用前缀,并使用默认的停止词。

DocumentId 映射

@DocumentId(或 PHP 8 属性的 #[DocumentId])允许您指定用于在 Redis 中识别文档的哈希。映射可以设置在属性上,或设置在无参数可以调用的方法上。

如果映射丢失,将生成一个随机哈希。

TextField 映射

@TextField([name], [noStem], [weight], [phonetic], [sortable], [noIndex], [unNormalized])(或 PHP 8 属性的 #[TextField([name], [noStem], [weight], [phonetic], [sortable], [noIndex], [unNormalized])])允许您在搜索引擎中添加文本。映射可以设置在属性上,或设置在无参数可以调用的方法上。

  • name 参数用于指定 RediSearch 中数据的名称。如果缺失,则使用属性名称,或根据获取器规则(get/is)使用方法名称。
  • noStem 参数是一个布尔值,用于指示数据是否应使用词干提取。
  • weight 参数是一个浮点数,用于指示数据在结果排序中的权重。
  • phonetic 参数是一个字符串,用于指示音韵搜索的语言。
  • sortable 参数是一个布尔值,用于指示数据是否可以用于排序结果。
  • noIndex 参数是一个布尔值,用于指示数据是否应可搜索。
  • unNormalized 参数是一个布尔值,用于指示数据是否应可搜索。

NumericField 映射

@NumericField([name], [sortable], [noIndex], [unNormalized])(或 PHP 8 属性的#[NumericField([name], [sortable], [noIndex], [unNormalized])])允许你在搜索引擎中添加数字。映射可以设置在属性上,或者设置在无参数的方法上。

  • name 参数用于指定 RediSearch 中数据的名称。如果缺失,则使用属性名称,或根据获取器规则(get/is)使用方法名称。
  • sortable 参数是一个布尔值,用于指示数据是否可以用于排序结果。
  • noIndex 参数是一个布尔值,用于指示数据是否应可搜索。
  • unNormalized 参数是一个布尔值,用于指示数据是否应可搜索。

TagField映射

@TagField([name], [separator], [sortable], [noIndex], [unNormalized])(或 PHP 8 属性的#[TagField([name], [separator], [sortable], [noIndex], [unNormalized])])允许你在搜索引擎中添加文本。映射可以设置在属性上,或者设置在无参数的方法上。

  • name 参数用于指定 RediSearch 中数据的名称。如果缺失,则使用属性名称,或根据获取器规则(get/is)使用方法名称。
  • separator参数是一个字符串,用于指示用于分隔值的字符。
  • sortable 参数是一个布尔值,用于指示数据是否可以用于排序结果。
  • noIndex 参数是一个布尔值,用于指示数据是否应可搜索。
  • unNormalized 参数是一个布尔值,用于指示数据是否应可搜索。

TagField的数据链接可以是标量数据,或是一个简单的标量数组(一维)。

GeoField映射

@GeoField([name], [noIndex], [sortable], [unNormalized])(或 PHP 8 属性的#[GeoField([name], [noIndex], [sortable], [unNormalized])])允许你在搜索引擎中添加地理坐标。映射可以设置在属性上,或者设置在无参数的方法上。

  • name 参数用于指定 RediSearch 中数据的名称。如果缺失,则使用属性名称,或根据获取器规则(get/is)使用方法名称。
  • noIndex 参数是一个布尔值,用于指示数据是否应可搜索。
  • unNormalized 参数是一个布尔值,用于指示数据是否应可搜索。

Suggestion映射

@Suggestion([group], [score], [increment], [payload])(或 PHP 8 属性的#[Suggestion([group], [score], [increment], [payload])])允许你在搜索引擎中添加地理坐标。映射可以设置在属性上,或者设置在无参数的方法上。

  • group参数用于指定 RediSearch 中的建议注册表名称。如果缺失,则默认为'suggestion'
  • score参数是一个浮点数,用于指示建议中值的优先级。如果缺失,则分数默认为1.0
  • increment参数是一个布尔值,用于指示当前建议的分数是否应添加到具有相同值的现有建议中。如果缺失,则分数不会添加。
  • payload参数是一个字符串,用于向建议添加额外数据(在建议引擎中未使用)。如果缺失,则不会向建议附加任何有效负载。

JSON映射

JSON映射文件可以包含多个类映射。JSON应遵循模式

[
  {
    "class": "\\MyApp\\Model\\Product",
    "index": "product",
    "stop-words": ["the", "a", "an", "this"],
    "fields": {
      "name": {"property": "name", "type": "text"},
      "manufacturer": {"getter": "getManufacturerName", "type": "text"},
      "price": {"getter": "getFinalPrice", "type": "numeric"},
      "colors": {"property": "colors", "type": "tag", "separator": "|"},
      "manufacturer_address": {"property": "manufacturerAddress", "type": "geo"}
    },
    "suggestions": [
      {"property": "name"},
      {"property": "colors", "group": "color"},
      {"getter": "getManufacturerName"}
    ]
  }
]

要启用JSON映射,必须使用\MacFJA\RediSearch\Integration\Json\JsonProvider。还必须安装JSON PHP扩展。

必须将JSON文件传递给\MacFJA\RediSearch\Integration\Json\JsonProvider::addJson方法。

JsonProvider可以添加到CompositeProvider

XML映射

XML映射文件可以包含多个类映射。JSON应遵循XSD模式

<?xml version="1.0" encoding="UTF-8" ?>
<redis-search xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://github.com/MacFJA/php-redisearch-integration/blob/main/src/Xml/schema.xsd">
    <class name="MyApp\Model\Integration\Product" indexname="product">
        <id type="property">id</id>
        <stops-words>
            <word>the</word>
            <word>a</word>
            <word>an</word>
            <word>this</word>
        </stops-words>
        <fields>
            <text-field>firstname</text-field>
            <text-field getter="getManufacturerName">firstname</text-field>
            <numeric-field getter="getFinalPrice">price</numeric-field>
            <tag-field property="colors" separator="|">color</tag-field>
            <geo-field property="manufacturerAddress">manufacturer_address</geo-field>
        </fields>
        <suggestions>
            <property name="name" />
            <property name="colors" group="color" />
            <getter name="getManufacturerName" />
        </suggestions>
    </class>
</redis-search>

要启用XML映射,必须使用\MacFJA\RediSearch\Integration\Xml\XmlProvider。还必须安装SimpleXML PHP扩展。

必须将XML文件传递给\MacFJA\RediSearch\Integration\Xml\XmlProvider::addXml方法。

XmlProvider可以添加到CompositeProvider

接口映射

你可以通过实现\MacFJA\RediSearch\Integration\Mapping接口来创建自己的映射,并将类添加到SimpleProvider中。

SimpleProvider可以添加到CompositeProvider

事件

ObjectWorker发出多个事件,允许你更改其行为。

事件分为两个主要组:Before组和After组。在Before组中,你可以在与Redis交互之前更改配置。在After组中,你可以对结果执行更多操作。

Before

以下参数可以用粗体表示可以更改。

After

以下参数可以用粗体表示可以更改。

贡献

你可以为库做出贡献。为此,你可以使用 Github issues 来

  • 提出你的问题
  • 建议新的映射提供者
  • 请求任何更改(错别字、不良代码等)
  • 等等...

您还可以使用PR来

  • 添加新的映射提供者
  • 建议更正
  • 等等...

更多信息请参阅 CONTRIBUTING

许可证

MIT许可证(MIT)。更多信息请参阅 许可证文件