Sylius search not working after a product is added in v0.12

While still trying to grok Sylius, I noticed that the search feature to find newly added products doesn’t work? I tried via the administration backend, both locally on the vagrant provided set up and on demo.sylius.org as well as writing my own import script using the ProductBuilder but to no avail.

When looking at the code, I could see that searches are against the sylius_search_index database table but when I looked in there I couldn’t see any new product entries, only those provided when loading the fixtures. As for how this database table gets populated, I found that a listener is fired and rules checked before an entry is added. From here, it turns out that this OrmListener invokes a rule check to see if the given object being written to the database is indexable, and this is where it falls over.

The failing and existing code for v.0.12 can be found in the Sylius\Bundle\SearchBundle\Indexer\OrmIndexer file:

public function isObjectIndexable($object)
{
    $class = get_class($object);
    foreach ($this->config['orm_indexes'] as $index) {
        if ($index['class'] === $class && 'orm' === $this->config['driver']) {
            return true;
        }
    }

    return false;
}

The replacement working code has a slight change in the if statement:

public function isObjectIndexable($object)
{
    $class = get_class($object);
    foreach ($this->config['orm_indexes'] as $index) {
        if ($index['class'] === $class && 'orm' === $this->config['engine']) {
            return true;
        }
    }

    return false;
}

I’m assuming after studying this code the value ‘orm’ remains and should be checked against the engine since ‘elasticsearch’ is another engine type. This is, as opposed to the config key of driver remaining and changing the comparison value to ‘doctrine/orm’.

And if you’ve already added a load of products you need indexing, you can just run ‘app/console sylius:search:index’ from the CLI to re-populate the search index.

Hope this helps someone else other than me.

Leave a Reply

Your email address will not be published. Required fields are marked *