valantic / pimcore-elastica-bridge
Pimcore 的 Elastica 桥接器
Requires
- php: ^8.1
- ext-json: *
- pimcore/pimcore: ^11.0
- psr/log: ^3.0
- ruflin/elastica: ^8.0.0
- symfony/console: ^6.2
- symfony/lock: ^6.2
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8.2
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.11.2
- phpstan/phpstan-deprecation-rules: ^1.2.0
- phpstan/phpstan-strict-rules: ^1.6.0
- rector/rector: ^1.1.0
- roave/security-advisories: dev-latest
- sentry/sentry: ^4.7
- symfony/http-client: ^6.4.7
- dev-main
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-beta1
- 4.1.1
- 4.1.0
- 4.0.0
- 4.0.0-beta.1
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 2.0.0-beta.1
- 1.x-dev
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-messenger-queue-utilization
- dev-feature/dopopulateindex-setparams
- dev-bugfix/empty-relation
- dev-bugfix/fixed-status-command
- dev-avoid-save-draft-in-index
- dev-update-related-products
- dev-v4.2.0-beta
- dev-fix-deleted-objects-are-not-handled
- dev-fix-allow-multiple-endpoints-to-ensure-cluster-setup
- dev-fix-autosave
- dev-feature/assets
- dev-bugfix/indexconfig
- dev-feature/implement_page_support
This package is auto-updated.
Last update: 2024-09-27 20:41:01 UTC
README
此包由 valantic CEC Schweiz 开发,并处于积极开发中。
此包的唯一任务是存储 Pimcore 元素(资产、文档、数据对象)到 Elasticsearch 文档。
配置
composer require valantic/pimcore-elastica-bridge- 编辑
config/bundles.php并添加\Valantic\ElasticaBridgeBundle\ValanticElasticaBridgeBundle::class => ['all' => true], - 根据
example/app/config/config.yaml中的示例配置您的 Elasticsearch 集群连接 - 别忘了在您的
services.yaml中注册新创建的服务(实现IndexInterface等)App\Elasticsearch\: resource: '../../Elasticsearch'
- 通过运行
bin/console valantic:elastica-bridge:status验证配置
用法
请参阅 docs/example/ 文件夹中的完整示例。以下步骤链接到示例中的相应部分,并更详细地解释了它们所做的工作。
定义索引
IndexInterface 描述了 Elasticsearch 中的索引,并包含创建此类索引所需的方法。最简单的方法是扩展 AbstractIndex 类,该类已以通用方式实现了大多数方法。在这种情况下,只需实现两个新索引所需的方法
getName返回索引的名称。对于默认启用的蓝绿部署,将添加一个后缀。getAllowedDocuments返回一个数组,包含可以存储在此索引中的文档的完全限定类名。最好使用实现DocumentInterface的类的::class常量。
请参阅示例中提供的 ProductIndex,以获取包含更多查询和租户感知功能的更详细实现。
定义文档
文档描述了索引内的 Pimcore 元素,即它表示在 Elasticsearch 索引中管理的资产、文档或数据对象。开发人员必须通过提供实现 DocumentInterface 的类来告诉此包关于这些元素。大多数方法已在 AbstractDocument 中实现,因此建议将其用作基类。需要实现以下四个方法
getType是资产、文档、数据对象或变体,对应于DocumentType的枚举。getSubType对于数据对象非常有用,因为它允许定义此类文档是关于什么类型的数据对象。最好使用数据对象或Asset\*/Document\*类的::class常量。shouldIndex应返回一个布尔值,指示 Pimcore 元素是否应被索引。getNormalized返回要索引的关联数组。以下方法帮助在此过程中,所有这些方法都接受 Pimcore 元素和描述映射的数组plainAttributes用于标量属性。根据映射,文档将包含这些属性中的值。localizedAttributes用于本地化 Pimcore 属性。它们将在文档的localized字段中存储,作为所有语言的子节点。relationAttributes允许仅存储引用,即 Pimcore 元素的 ID,而不是在索引中存储整个对象。- 映射可以是未定义键的数组,在这种情况下,Pimcore 元素的属性将使用相同的名称索引,或者如果属性在索引中应使用不同的名称,则可以使用键值对。如果使用键值对,还可以传递一个函数来检索 Pimcore 元素并返回任意数组。这非常强大,可以实施几乎任何用例。请注意,也可以混合这两种方法,即定义一些具有键的条目,而其他则没有。
有关更多详细信息,请参阅ProductIndexDocument 示例。
配置
valantic_elastica_bridge: client: # The DSN to connect to the Elasticsearch cluster. dsn: 'https://:9200' # If true, breadcrumbs are added to Sentry for every request made to Elasticsearch via Elastica. should_add_sentry_breadcrumbs: false indexing: # To prevent overlapping indexing jobs. Set to a value higher than the slowest index. Value is specified in seconds. lock_timeout: 300 # If true, when a document fails to be indexed, it will be skipped and indexing continue with the next document. If false, indexing that index will be aborted. should_skip_failing_documents: false
事件
此项目使用 Symfony 的事件调度器。以下是可以监听的事件:
您可以创建事件订阅者或事件监听器来监听这些事件。有关如何使用事件调度器的更多信息,请参阅Symfony 文档。
事件的潜在用例
- 在元素刷新后清除缓存
- 在元素刷新后发送通知
- 记录事件
- 更新索引中的相关元素
事件传播
在刷新多个元素时,每个刷新都会触发一个事件,这可能导致另一个刷新,从而产生无限循环。为了避免这种情况,可以在刷新过程中禁用事件传播。您可以通过在 RefreshElement 消息构造函数中将 $stopPropagateEvents 设置为 true 或在将消息添加到队列之前调用 stopEventPropagation() 来禁用事件传播。
队列
设置一个工作进程来处理 elastica_bridge_index。或者,您可以路由传输以使用 sync 处理器:framework.messenger.transports.elastica_bridge_index: 'sync'。
索引
批量
$ console valantic:elastica-bridge:index --help
Description:
Ensures all the indices are present and populated.
Usage:
valantic:elastica-bridge:index [options] [--] [<index>...]
Arguments:
index Optional: indices to process. Defaults to all if empty
Options:
-d, --delete Delete i.e. re-create existing indices
-p, --populate Populate indices
-c, --check Perform post-populate checks
-h, --help Display this help message
特定
桥接器会自动监听 Pimcore 事件并根据需要更新文档。如果需要,可以调用 \Valantic\ElasticaBridgeBundle\Service\PropagateChanges::handle 或执行 console valantic:elastica-bridge:refresh。
这可以通过调用 \Valantic\ElasticaBridgeBundle\EventListener\Pimcore\ChangeListener::disableListener(); 全局禁用。
您还可以分发一个 Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElement 消息来处理由 ChangeListener 触发的外部对象的更新。
状态
$ console valantic:elastica-bridge:status --help
Description:
Displays the status of the configured Elasticsearch indices