loonpwn / laravel-swiftype
Laravel Swiftype 集成
Requires
- php: ^7.3|^8.0|^8.1
- elastic/enterprise-search: ^7.13
- illuminate/support: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: ^6.19
- dev-master
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.0
- 1.1.0-beta
- 1.0.0
- 0.4.0
- 0.3.0
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
- dev-dependabot/composer/nyholm/psr7-1.7.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-dependabot/composer/symfony/http-kernel-5.4.20
- dev-feat/enterprise-search-client
- dev-feat/release-1.1.0
- dev-feat/scout-integration
- dev-analysis-qJdo7W
- dev-analysis-zdNKYp
- dev-analysis-qJnlYD
- dev-analysis-8Q5J9O
This package is auto-updated.
Last update: 2024-09-21 23:35:25 UTC
README
Laravel Swiftype 是一个对 elastic/app-search 的封装,提供一些 Laravel 特定的助手函数,以便轻松地将 Eloquent 模型与 Swiftype 集成。
安装
通过 Composer
composer require harlan-zw/laravel-swiftype
如果你没有运行 Laravel 5.5(或更高版本),则需要在 config/app.php
中添加服务提供者
Loonpwn\Swiftype\SwiftypeServiceProvider::class,
使用方法
在你的 .env
文件中,添加以下变量。
SWIFTYPE_DEFAULT_ENGINE=
SWIFTYPE_API_PRIVATE_KEY=
SWIFTYPE_HOST_IDENTIFIER=
从 Swiftype 凭据页面 获取你的密钥。
当自托管一个 AppSearch 实例时,你可以使用凭据页面上显示的 API 端点作为 SWIFTYPE_HOST_IDENTIFIER
,例如。
SWIFTYPE_HOST_IDENTIFIER=https://:3002
API
此包有两个 Facades,可以让你访问底层的 Swiftype 客户端。
Swiftype
Swiftype
Facade 是对来自 https://github.com/elastic/app-search-php 的构建客户端的直接封装。任何基本客户端的命令都可以在这个 Facade 上使用。示例
-
Swiftype::listEngines($currentPage = null, $pageSize = null)
- 显示所有可用的引擎 -
Swiftype::createEngine($name, $language = 'en')
- 根据名称查找引擎
带有 IDE 自动完成
/** @var \Loonpwn\Swiftype\Clients\Api $api */ $api = app(Swiftype::class);
SwiftypeEngine
SwiftypeEngine
是对 Swiftype
Facade 的封装,具有直接针对默认引擎的上下文。许多来自核心 API 的相同功能都可用于此 Facade,无需指定引擎。
SwiftypeEngine::search($query, $options)
- 在引擎中搜索文档
SwiftypeEngine::indexDocument($document)
- 根据主键创建新文档或更新现有文档。此函数将使用转换器确保主键被转换为仅 id
。
SwiftypeEngine::indesDocuments($document)
- 与上面类似,但会将模型列表分成每 100 个请求一个块
SwiftypeEngine::deleteDocument($documentId)
- 删除文档。
SwiftypeEngine::deleteDocuments($documentIds)
- 接受一个文档 ID 数组并删除它们。
SwiftypeEngine::listDocuments($page = 1, $pageSize = 100)
- 列出属于引擎的文档,带有分页。
SwiftypeEngine::listAllDocumentsByPages($action, $page = 1, $pageSize = 100)
- 列出属于引擎的文档,将迭代所有页面并调用您的自定义操作。
SwiftypeEngine::purgeAllDocuments()
- 将从 Swiftype 删除所有文档。
特质
IsSwiftypeDocument
是一个可用的特质,它钩入模型的 saved
事件钩子。在保存时发生以下操作
shouldSyncSwiftypeOnSave
被检查并且必须通过 true 才能继续getSwiftypeAttributes
被调用以获取要发送到 Swiftype 的属性
您应该重写这些函数以实现特定业务逻辑。
/** * Should model changes be pushed to Swiftype. Excludes deleting * @return bool */ public function shouldSyncSwiftypeOnSave() { // by default all model changes are pushed to swiftype return true; } /** * Get the mapped attribute values for Swiftype * @return mixed|null */ public function getSwiftypeAttributes() { // Document transformer is the default transformer, feel free to implement your own return transform($this, new DocumentTransformer()); }
作业
目前仅创建了与 Eloquent 模型事件直接相关的作业。这些可以用来排队数据同步。
DeleteDocument($documentId)
- 删除特定文档,接受文档 IDIndexDocument($document)
- 推送单个文档。接受映射的文档SyncDocuments
- 遍历Swiftype和本地数据库中的所有文档,查找不同步的文档,并添加或删除它们。这使用swiftype.sync_models
配置
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件harlan@harlanzw.com联系,而不是使用问题跟踪器。