Search code examples
symfonyelasticafoselasticabundle

FOS elastica bundle 4.* - automatic index entity does not work


I'm currently trying to let the FOS Elastica bundle to automatically update the index when there are new entries with the following settings:

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

However, my custom model to elastica transformer isn't triggered. Anyone any idea how to fix this?


Solution

  • In my case I had an event subscriber which injected rows into a SQLite database without the Entity Manager. This lead to a situation in which the event listener of the FOSElastica bundle didn't detect changes. In order to index these rows into ElasticSearch I extended the subscriber with:

    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
        );
    }