1
types:
            product:
                mappings:
                    title:    { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                    status:
                    brand.name:    { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                    brand:
                      type: "nested"
                      properties:
                          status: ~
                persistence:
                    driver: orm
                    model: MyBundle\Entity\Product\Product
                    provider: 
                      query_builder_method: customProductQueryBuilderElastica
                    listener: ~ 
                    finder: ~

This is my mappings for type product. customProductQueryBuilderElastica contains code which populates only products which have active status and have related brand status active. It is working perfectly if i change products from my admin.

what i want to do is when i change my brand status to inactive, all related products should be removed from ES.

For that i have used brand as nested of product and created listener for it as explained here and now i am able to change brand status for every products in my ES automatically but i want to remove such products when brand status sets to inactive. How can this be achieved in better way?.

Community
  • 1
  • 1
Yashrajsinh Jadeja
  • 1,699
  • 1
  • 16
  • 21

1 Answers1

1

After many tries. i finally achieved what i want. I am posting my code here and try to help others.

Thanks to @maercky. i have taken reference to his answer which is given here

Here is my config.yml file.

types:
            product:
                mappings:
                    title:    { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                    status:
                    brand.name:    { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                    brand:
                      type: "nested"
                      properties:
                          status: ~
                persistence:
                    driver: orm
                    model: XXX\MyBundle\Entity\Product\Product
                    provider: 
                      query_builder_method: customProductQueryBuilderElastica
                    listener: ~ 
                    finder: ~

This code will go to service.yml

fos_elastica.listener.brand.product:
    class: 'XXX\MyBundle\Listener\ElasticaBrandListener'
    arguments:
        - @fos_elastica.object_persister.search.product
        - ['postPersist', 'postUpdate', 'postRemove', 'preRemove']
        - @fos_elastica.indexable
    calls:
        - [ setContainer, [ '@service_container', @fos_elastica.object_persister.search.product ] ]
    tags:
        - { name: 'doctrine.event_subscriber' }

and finally this is my Listener for Brand

    <?php

namespace  XXX\MyBundle\Listener;

use FOS\ElasticaBundle\Doctrine\Listener as BaseListener;
use Doctrine\Common\EventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use XXX\MyBundle\Entity\Supplier\Brand;
use FOS\ElasticaBundle\Persister\ObjectPersister;

class ElasticaBrandListener extends BaseListener
{

    /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
    private $container;
    private $objectPersisterProducts;

    public function setContainer(ContainerInterface $container,ObjectPersister $objectPersisterProduct) {
        $this->container = $container;
        $this->objectPersisterProducts = $objectPersisterProduct;
    }

    /**
     * @param Doctrine\Common\EventArgs $eventArgs
     */
    public function postUpdate(EventArgs $eventArgs)
    {
        /** @var $brand Brand */
        $brand = $eventArgs->getEntity();

        if ($brand instanceof Brand) {
            $this->scheduledForUpdate[] = $brand;
            foreach ($brand->getProducts() as $product) {
                $brand_status = $brand->getStatus();
                $product_status = $product->getStatus();
                if($brand_status == 'active' && $product_status == 'active'){
                    $this->objectPersisterProducts->replaceOne($product);
                }else{
                    $this->objectPersisterProducts->deleteOne($product);
                }

            }
        }
    }
}
?>

All this works for me well and so i am contributing this for others.

Community
  • 1
  • 1
Yashrajsinh Jadeja
  • 1,699
  • 1
  • 16
  • 21