FOS elastica bundle 4. * - 自动索引实体不起作用

时间:2018-05-02 11:09:37

标签: symfony elastica foselasticabundle

我正在尝试让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

然而,我的弹性变压器定制模型没有被触发。任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在我的情况下,我有一个事件订阅者,它在没有实体管理器的情况下将行注入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
    );
}