answear / luigis-box-bundle

Symfony 的 Luigi's Box 集成。

3.1.0 2023-09-19 13:04 UTC

This package is auto-updated.

Last update: 2024-09-19 15:17:26 UTC


README

Symfony 的 Luigi's Box 集成。Luigi's Box 文档可在此处找到:https://live.luigisbox.com/

安装

  • 使用 Composer 安装
composer require answear/luigis-box-bundle

设置

  • 提供所需的配置数据: publicKeyprivateKey
  • searchCacheTtl 是 Luigi 缓存请求的生存时间(秒,最大 300 秒 - 这是 Luigi's Box 的限制)
# config/packages/answear_luigis_box.yaml
answear_luigis_box:
    default_config: second_config_name
    configs:
        your_config_name:
            host: 'https://live.luigisbox.com' #default
            publicKey: 'your_public_key'
            privateKey: 'your_private_key'
            connectionTimeout: 4.0 #default
            requestTimeout: 10.0 #default
            searchTimeout: 6.0 #default
            searchCacheTtl: 0 #default
        second_config_name:
            publicKey: 'your_public_key'
            privateKey: 'your_private_key'

如果您只有一个配置,可以省略 default_config 节点。配置将被传递到 \Answear\LuigisBoxBundle\Service\ConfigProvider 类。

如果您有更多配置,可以按以下方式更改它们

use Answear\LuigisBoxBundle\Service\ConfigProvider;

/** @var ConfigProvider $configProvider **/
$configProvider->setConfig('your_config_name');

并像以前一样使用应用程序。

如果您需要向搜索请求传递自定义头信息,请按以下方式操作

use Answear\LuigisBoxBundle\Service\ConfigProvider;

/** @var ConfigProvider $configProvider **/
$configProvider->setHeader('header-name', 'header-value');

/* reset all headers */
$configProvider->resetHeaders();

动态配置

您可以通过传递额外的配置。

use Answear\LuigisBoxBundle\DTO\ConfigDTO;
use Answear\LuigisBoxBundle\Service\ConfigProvider;

/** @var ConfigProvider $configProvider **/
$configProvider->addConfig('your_config_name', new ConfigDTO(...));

使用方法

内容请求

  1. 完整的 内容更新 文档
use Answear\LuigisBoxBundle\ValueObject\ContentUpdate;
use Answear\LuigisBoxBundle\ValueObject\ContentUpdateCollection;

// ...

$collection = new ContentUpdateCollection([new ContentUpdate('product title', 'product/url', 'object type', ['field' => 'field 1'])]);

/** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/
$apiResponse = $request->contentUpdate($collection);

第一个参数($title)将被用作 Luigi's Box 中的产品标题,除非在 $fields 参数中存在 title 字段。

  1. 部分更新
use Answear\LuigisBoxBundle\ValueObject\ContentUpdateCollection;
use Answear\LuigisBoxBundle\ValueObject\PartialContentUpdate;

// ...

$collection = new ContentUpdateCollection([new PartialContentUpdate('product/url', 'object type', ['title' => 'product title'])]);

/** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/
$apiResponse = $request->partialContentUpdate($collection);
  1. 内容删除
use Answear\LuigisBoxBundle\ValueObject\ContentRemoval;
use Answear\LuigisBoxBundle\ValueObject\ContentRemovalCollection;

// ...

$collection = new ContentRemovalCollection([new ContentRemoval('product/url', 'product')]);

/** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/
$apiResponse = $request->contentRemoval($collection);
  1. 更改可用性

一种简单启用/禁用对象的方法 - 将使用部分更新。

use Answear\LuigisBoxBundle\ValueObject\ContentAvailability;
use Answear\LuigisBoxBundle\ValueObject\ContentAvailabilityCollection;

// ...

$isAvailable = true;
$collection = new ContentAvailabilityCollection([new ContentAvailability('product/url', $isAvailable)]);

/** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/
$apiResponse = $request->changeAvailability($collection);

// ... or pass one object

$isAvailable = true;
/** @var \Answear\LuigisBoxBundle\Service\RequestInterface $request **/
$apiResponse = $request->changeAvailability(new ContentAvailability('product/url', $isAvailable));

在所有请求中,您可以捕获一些异常

  • BadRequestException - 请求无效,
  • TooManyItemsException - 使用更少的项进行请求,
  • MalformedResponseException - Luigi's Box API 响应出现问题,
  • TooManyRequestsException - 延迟请求速率,
  • ServiceUnavailableException

请单独捕获它们

use Answear\LuigisBoxBundle\Exception\BadRequestException;
use Answear\LuigisBoxBundle\Exception\TooManyItemsException;
use Answear\LuigisBoxBundle\Exception\MalformedResponseException;
use Answear\LuigisBoxBundle\Exception\TooManyRequestsException;
use Answear\LuigisBoxBundle\Exception\ServiceUnavailableException;

try {
    // ... request
} catch (BadRequestException $e){
    //bad request
    $request = $e->getRequest();
    $response = $e->getResponse();
} catch (TooManyItemsException $e){
    //items limit reached
    $limit = $e->getLimit();
} catch (MalformedResponseException $e){
    //bad response
    $response = $e->getResponse();
} catch (TooManyRequestsException $e){
    //repeat request after $retryAfter seconds
    $retryAfter = $e->getRetryAfterSeconds();
} catch (ServiceUnavailableException $e){
    //delay request
}

内容响应

\Answear\LuigisBoxBundle\Response\ApiResponse:

  • (bool) $success - 如果所有文档都成功传递,则为 true
  • (int) $okCount - 成功传递的文档数,
  • (int) $errorsCount - 失败的文档数,
  • (array) $errors - \Answear\LuigisBoxBundle\Response\ApiResponseError 对象数组,
  • (array) $rawResponse - 从 API 解码的响应。

ApiResponseError:

  • (string) $url - 文档的 URL
  • (string) $type - 错误类型(例如 malformed_input
  • (string) $reason - 失败文本(例如 incorrect object format
  • (array|null) $causedBy - 错误的具体原因(例如 ["url": ["is missing"]]

注意!

ApiResponse::$success 如果传递的任何文档失败,则设置为 false。如果您想了解更新了多少文档,请检查 $okCount,如果要检查哪些文档失败,请检查 $errors

搜索(文档 在此

  1. 请求
use Answear\LuigisBoxBundle\ValueObject\SearchUrlBuilder;

// ...

$page = 3;
$urlBuilder = new SearchUrlBuilder($page);
$urlBuilder
    ->setQuery('nice top')
    ->addFilter('type', 'product')
    ->addFilter('category', 'top')
    ->addFilter('brand', 'Medicine')
    ->addFilter('brand', 'Answear')
    ->addPrefer('brand', 'Answear')
    ->setSort('size', 'asc');

//the above code produces a url query like `size=10&page=3&q=nice+top&f%5B0%5D=type%3Aproduct&f%5B1%5D=category%3Atop&f%5B2%5D=brand%3AMedicine&f%5B3%5D=brand%3AAnswear&sort=size%3Aasc&prefer%5B0%5D=brand%3AAnswear`

/** @var \Answear\LuigisBoxBundle\Service\SearchRequestInterface $request **/
$searchResponse = $request->search($urlBuilder);

请参阅 Luigi's Box 文档以了解每个字段 SearchUrlBuilder 暴露的确切用途。

  1. 响应

SearchRequest::search() 将返回一个包含以下字段的 SearchResponse 对象

  • (string) $searchUrl
  • (string) $query
  • (string|null) $correctedQuery
  • (array) $filters
  • (Hit[]) $hits. Hit
    • (string) $url;
    • (array) $attributes;
    • (array) $nested;
    • (字符串) $type;
    • (数组) $highlight;
    • (布尔) $exact;
    • (布尔) $alternative;
  • (命中记录[]) $quickSearchHits
    • 与上面相同
  • (属性[]) $facets. 属性
    • (字符串) $name;
    • (字符串) $type;
    • (数组) $values;
  • (整数) $totalHits
  • (整数) $currentSize

通过查询更新(文档在此

  1. 更新
use Answear\LuigisBoxBundle\ValueObject\UpdateByQuery;

// ...
$types = ['product'];
$fields = ['color' => 'green'];
$search = new UpdateByQuery\Search($types, $fields);

$updateFields = ['color' => ['olive', 'emerald']];
$update = new UpdateByQuery\Update($updateFields);

$updateByQuery = new UpdateByQuery($search, $update);

/** @var \Answear\LuigisBoxBundle\Service\UpdateByQueryRequest $request **/
$response = $request->update($updateByQuery);

$jobId = $response->getJobId();
  1. 检索作业状态
use Answear\LuigisBoxBundle\ValueObject\UpdateByQuery;

$jobId = 1;

/** @var \Answear\LuigisBoxBundle\Service\UpdateByQueryRequest $request **/
$response = $request->getStatus($jobId);

getStatus 将返回包含以下字段的 UpdateByQueryStatusResponse

  • (数组) $rawResponse
  • (字符串) $trackerId
  • (布尔) $completed
  • (整数|null) $okCount - 如果未完成则为 null
  • (整数|null) $errorsCount - 如果未完成则为 null
  • (ApiResponseError[]|null) $errors - 如果未完成则为 null

最后备注

请随时提交带有新功能、改进或错误修复的拉取请求。Answear 团队将非常感谢任何评论。

祝您玩得开心!