首页 > 解决方案 > 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

但是,我的弹性变压器自定义模型没有被触发。有人知道如何解决这个问题吗?

标签: symfonyelasticafoselasticabundle

解决方案


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

推荐阅读