coveo / magento-2-search
Magento 2 的 Coveo 集成模块
Requires
- php: ~7.0.0|~7.1.0|~7.2.0|~7.3.0
- ext-curl: *
- ext-json: *
- ext-zip: *
- coveo/sdkpushphp: ^1.6
- magento/framework: 101.0.*|102.0.*
- magento/module-catalog-search: ^100.2.4|101.0.*
README
Coveo 是一种基于云的多语言电子商务搜索工具。
此扩展将 Magento 的默认搜索替换为一种具有拼写容错、快速和相关的搜索体验,后端由 Coveo 支持。
⚠️ 试验性,自行承担风险。
此存储库共享是为了帮助和提供信息(非官方支持,非生产使用)。
如需协助,请勿联系 Coveo 支持,而是使用 GitHub 问题。
描述
此扩展将默认的 Magento 搜索引擎替换为基于 Coveo API 的搜索引擎。它提供以下功能
- 对目录产品的全文搜索(目前不支持高级搜索)
- 目录产品的计划索引(开发中)
- 自动拼写纠正(开发中)
- ML 搜索关键字建议
要求
- PHP > 7.0
- Composer
- Magento >= 2.3
安装说明
最新版本
使用 composer 安装最新版本
composer require coveo/magento-2-search
特定版本
使用 composer 安装特定版本
composer require coveo/magento-2-search:1.0.0
开发版本
将 "minimum-stability" 设置为 "dev" 并关闭 "prefer-stable" 配置
composer config minimum-stability dev
composer config prefer-stable false
使用 composer 安装最新开发版本
composer require coveo/magento-2-search:dev-develop
模块配置
请求您的 Coveo Cloud 组织
转到: Coveo
创建您的推送源
转到: 推送 将创建的推送 API 密钥复制到您的配置中。
创建您的搜索 API 密钥
转到: 搜索 Api。将创建的推送 API 密钥复制到您的配置中。
在 Coveo 平台上创建您的字段
转到: 创建字段.
创建以下字段
字段名称 | 类型 | 设置 |
---|---|---|
sku | 字符串 | |
store_id | 字符串 | |
价格 | 小数 |
** 当你开始构建索引时,请查看 Coveo.log
文件中的 MetadataExamplePush
。在这里,你可以看到所有推送至 Coveo 索引的字段。通过 字段映射
,你可以将这些字段映射到特定的 Coveo 字段。**
设置您的配置
- 在 API 配置 下
-
插入
-
发送报告:选择 是 以在 API 错误发生时向 Coveo 发送报告
-
调试模式:选择 是 以启用更详细的日志记录以进行调试
- 保存配置
以编程方式使用 Coveo 服务
如果您想调用当前插件不支持的功能的 Coveo 服务,我们建议使用此配置。
在您的类中添加对 Coveo\Search\Api\Service\ClientInterface
的依赖,并让 DI 系统完成其余操作
<?php use Coveo\SDK\ClientBuilder; use Coveo\Search\Api\Service\ConfigInterface; use Coveo\Search\Api\Service\TrackingInterface; use Coveo\Search\Api\Service\LoggerInterface; class MyServiceClass { /** * @var ConfigInterface */ protected $config; /** * @var TrackingInterface */ protected $tracking; /** * @var ClientBuilder */ protected $clientBuilder; /** * @var LoggerInterface */ protected $logger; /** * Search constructor. * * @param ConfigInterface $config * @param TrackingInterface $tracking * @param ClientBuilder $clientBuilder * @param LoggerInterface $logger */ public function __construct( ConfigInterface $config, TrackingInterface $tracking, ClientBuilder $clientBuilder, LoggerInterface $logger ) { $this->config = $config; $this->tracking = $tracking; $this->clientBuilder = $clientBuilder; $this->logger = $logger; } }
使用 Coveo\SDK\ClientBuilder
实例构建客户端实例
<?php /** * Get Client * * @return \Coveo\SDK\Client */ protected function getClient() { return $this->clientBuilder ->withApiKey($this->config->getApiKeySearch()) ->withApiBaseUrl($this->config->getApiSearchUrl()) ->withSessionStorage($this->tracking->getSession()) ->withLanguage($this->config->getLanguage()) ->withStoreCode($this->config->getStoreId()) ->withAgent($this->tracking->getApiAgent()) ->withLogger($this->logger) ->withPipeline('Recommendations') //optional Query Pipeline to use ->withRecommendations(true) ->build(); }
使用 withRecommendations
启用推荐调用的适当分析事件。使用 withPipeline
可以针对特定的管道以触发结果。
这允许您创建一个 Coveo\SDK\Client
实例,用于通过预配置的必要参数向 Coveo API 发起任何 HTTP 调用
<?php /** * Execute service */ protected function execute() { $client = $this->getClient(); $result = $client->doRequest('/search', \Coveo\SDK\Client::HTTP_METHOD_GET, [ 'param1' => 'value1', 'param2' => 'value2' ]); }
从 doRequest
方法返回的 Coveo\SDK\Response
类型对象中访问响应数据
<?php $result = $client->doRequest('/search', \Coveo\SDK\Client::HTTP_METHOD_GET, [ 'param1' => 'value1', 'param2' => 'value2' ]); $responseData = $result->getResponse();
所需的主题更改
搜索后产品点击跟踪
为了使搜索后产品点击跟踪工作,前端脚本需要找到带有产品链接值的 data-product-sku
属性的 a
标签上的 data-product-sku
属性。为此,编辑您的主题文件 view/frontend/templates/product/list.phtml
,在产品列表循环内部,按以下方式操作
<a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" class="product photo product-item-photo" tabindex="-1"> <?= $productImage->toHtml() ?> </a> <div class="product details product-item-details"> <!-- here the product details -->
如果您使用的是 AJAX 分页方法(例如无限滚动加载),您还必须在产品链接的 a
标签上添加一个 data-search-id
属性。为此,编辑您的主题文件 view/frontend/templates/product/list.phtml (或 magento2\vendor\magento\module-catalog\view\frontend\templates\product\list.phtml)
以从注册表键 coveo_search_response
(从 \Coveo\Search\Model\Service\Search
类的常量 SEARCH_RESULT_REGISTRY_KEY
可用)加载 Coveo 搜索 ID 值,如下所示
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Coveo\SDK\Search\Result $searchResult */ $searchResult = $objectManager->get('Magento\Framework\Registry')->registry(\Coveo\Search\Model\Service\Search::SEARCH_RESULT_REGISTRY_KEY); ?> <!-- your product collection loop --> <a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" data-search-id="<?= $searchResult->getSearchId() ?>" class="product photo product-item-photo" tabindex="-1"> <?= $productImage->toHtml() ?> </a> <div class="product details product-item-details"> <!-- here the product details -->
显然,这只是一个例子,最好使用 DI 方法 在产品列表控制器中注入 Magento\Framework\Registry
依赖。
前端建议
此模块可以在搜索输入中提供建议。当您启用此功能时,您必须删除默认的 Magento 建议系统。为此,从模板中删除 Magento 建议初始化或覆盖默认模板 view/frontend/templates/form.mini.phtml
。从搜索输入中删除此初始化属性
data-mage-init='{"quickSearch":{ "formSelector":"#search_mini_form", "url":"<?= /* @escapeNotVerified */ $helper->getSuggestUrl()?>", "destinationSelector":"#search_autocomplete"} }'
并从模板中删除建议容器
<div id="search_autocomplete" class="search-autocomplete"></div>