FOSElasticaBundle and SoftDeletable Doctrine not working very well

409 views Asked by At

I am currently using FOSElasticaBundle in my projects and the entities that is to be searched is using softdeletable . It seems that this is not going very well since when the entity is softdeleted the index on elastic search is not removed. Populating the index again is a very expensive operation and takes 30 minutes to complete due to the large item set that I have (1.5 million items).

What is the best way to remove index from the elastic search manually? I was planning to use the softdeletable listener so when a softdelete happens I would manually then remove it from the index. But I am not sure how to do that via elastica.

1

There are 1 answers

0
Serhii Smirnov On

Create entity listener:

<?php


namespace Acme\MainBundle\EventListener;

use Acme\MainBundle\Entity\InstagramShopPicture;
use Acme\MainBundle\Entity\InstagramShop;

class ElasticSearchSoftdeletableListener
{
private $container;

public function __construct($container)
{
    $this->container = $container;
}

public function postSoftDelete(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if ($entity instanceof InstagramShopPicture) {
        $type = 'picture';
    } else if ($entity instanceof InstagramShop) {
        $type = 'shop';
    } else {
        return;
    }

    $this->container->get("fos_elastica.listener.index.$type")->postRemove($args);
}
}

Register listener via service:

    softdeletable.listener:
    class: Acme\MainBundle\EventListener\ElasticSearchSoftdeletableListener
    arguments:
        - @service_container
    tags:
        - { name: doctrine.event_listener, event: postSoftDelete }