Symfony 2 FOSElasticaBundle : elastic search documents are not automatically synchronized with entities : need to run ' fos:elastica:populate' Symfony 2 FOSElasticaBundle : elastic search documents are not automatically synchronized with entities : need to run ' fos:elastica:populate' elasticsearch elasticsearch

Symfony 2 FOSElasticaBundle : elastic search documents are not automatically synchronized with entities : need to run ' fos:elastica:populate'


After a few hours, the solution came from this ressource that allows you to define a callback that returns if an entity must be indexed in elastic search.

So the config now looks like this :

fos_elastica:    clients:        default: { host: %elasticsearch_host%, port: %elasticsearch_port% }    indexes:        foobar:             client:     default             types:                foobar_published:                    indexable_callback: 'isPublished'                    mappings:                        state: ~                        field1: ~                    persistence:                        listener: ~                        driver:   orm                        model:    App\MyBundle\Entity\Foobar                        provider:                            query_builder_method: createIsIndexablePublishedQueryBuilder                        finder:   ~                foobar_draft:                    indexable_callback: 'isDraft'                    mappings:                        state: ~                        field1: ~                    persistence:                        listener: ~                        driver:   orm                        model:    App\MyBundle\Entity\Foobar                        provider:                            query_builder_method: createIsIndexableDraftQueryBuilder                        finder:   ~

And I also implement the callbacks method in the Foobar entity :

class Foobar extends BaseEntity {...       public function isPublished()    {        if ($this->getState() === self::STATE_TO_BE_INDEXED_IN_ELA) {            return true;        }        return false;    }    public function isDraft()    {        if ($this->getState() === self::STATE_DRAFT_TO_BE_INDEXED_IN_ELA) {            return true;        }        return false;    }}

And now everything is running fine, the documents are synchronised with documents in the right index ant types.I use those queries to checks results :

curl -XPOST "http://localhost:9200/foobar/foobar_published/_search?size=300"curl -XPOST "http://localhost:9200/foobar/foobar_draft/_search?size=300"

Note : I also change the listener property in my config file to 'listener: ~' instead of 'listener: {immediate: ~}'.

I test on insert,delete and update statement and everything now works fine !