symfony - FOS elastica bundle 4.* - 自动索引实体不起作用
问题描述
我目前正在尝试让 FOS Elastica 捆绑包在有具有以下设置的新条目时自动更新索引:
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
audit:
finder: ~
types:
audit_log:
persistence:
driver: orm
model: AuditBundle\Entity\AuditLog
# Problem occurs here. This should trigger automatic inserts, updates and deletes
listener: ~
provider: ~
finder: ~
model_to_elastica_transformer:
service: app.audit_transformer
但是,我的弹性变压器自定义模型没有被触发。有人知道如何解决这个问题吗?
解决方案
就我而言,我有一个事件订阅者,它在没有实体管理器的情况下将行注入 SQLite 数据库。这会导致 FOSElastica 包的事件侦听器未检测到更改的情况。为了将这些行索引到 ElasticSearch 中,我扩展了订阅者:
public function __construct(
TokenStorage $securityTokenStorage,
EntityManager $entityManager,
// These lines
ObjectPersisterInterface $postPersister,
IndexableInterface $indexable,
array $config
){
$this->securityTokenStorage = $securityTokenStorage;
$this->audit = $entityManager;
// These lines
$this->objectPersister = $postPersister;
$this->indexable = $indexable;
$this->config = $config;
parent::__construct($postPersister, $indexable, $config);
}
public function onFlush(...)
{
// ....
//* Insert audit in ElasticSearch
$audit = $this->audit->getRepository('AuditBundle:AuditLog')->findLast();
if ($this->objectPersister->handlesObject($audit)) {
if ($this->isObjectIndexable($audit)) {
$this->objectPersister->insertOne($audit);
}
}
// ....
}
/**
* @param object $object
* @return bool
*/
private function isObjectIndexable($object)
{
return $this->indexable->isObjectIndexable(
self::AUDIT_INDEX,
self::AUDIT_TYPE_NAME,
$object
);
}
推荐阅读
- api - DocuSign API - 在每个文档的末尾添加签名页面
- angular - 如何将第三个状态添加到复选框?
- html - 为什么 input[type=checkbox] 不允许 CSS?
- ios - 由图像视图剪辑到边界的 CAShapeLayer
- c# - 如何获得对应的“名称”
- chart.js - 如何在chartjs中为条形设置不同的宽度?
- dart - 调用无状态类构建的实例与有状态类的构建
- pivot-table - PowerPivot。构造公式,首先聚合一个表中的数据,然后使用另一个表中的数据计算结果
- google-distancematrix-api - MAX_DIMENSIONS_EXCEEDED 距离矩阵错误
- java - Collator:如何在规则中添加空间?