Adding facets in Sylius

In this scenario we will add a new facet called grape (for wines) when browsing the site’s categories/taxonomies/taxons.

This is essentially a two-step process of editing the config relating to search and then re-indexing our search indexes.

Step 1

Edit the sylius.yml file at src/Sylius/Bundle/CoreBundle/Resources/config/app/. Of course, this should be you own config file so as to avoid touching the Sylius codebase as much as possible to make upgrades easier but for the purposes of this guide we will keep it simple.

Assuming your existing config file looks like the one below, this is taken from loading Sylius fixtures at install time, we will add our grape facet:

sylius_search:
    search_form_template: 'SyliusSearchBundle::search_form.html.twig'
    engine: orm
    classes:
       search:
          model: Sylius\Bundle\SearchBundle\Model\SearchIndex
       log:
          model: Sylius\Bundle\SearchBundle\Model\SearchLog
    orm_indexes:
        product:
            class: %sylius.model.product.class%
            mappings:
                id: ~
                name: ~
                description: ~

    custom_accessors:
        - Sylius\Component\Product\Accessor\ProductAccessor
    filters:
        pre_search_filter:
            enabled: true
            taxonomy: category
        facet_groups:
            search_set:
                values: [taxons, price, made_of, color]
            categories_set:
                values: [price, made_of, color]
            all_set:
                values: [taxons, price, made_of]
        facets:
            taxons:
                display_name: 'Basic categories'
                type: terms
                value: ~
            price:
                display_name: 'Available prices'
                type: range
                values:
                    - { from: 0, to: 2000}
                    - { from: 2001, to: 5000}
                    - { from: 5001, to: 10000}
            made_of:
                display_name: 'Material'
                type: terms
                value: ~
            color:
                display_name: 'Available colors'
                type: terms
                value: ~

To:

sylius_search:
    search_form_template: 'SyliusSearchBundle::search_form.html.twig'
    engine: orm
    classes:
       search:
          model: Sylius\Bundle\SearchBundle\Model\SearchIndex
       log:
          model: Sylius\Bundle\SearchBundle\Model\SearchLog
    orm_indexes:
        product:
            class: %sylius.model.product.class%
            mappings:
                id: ~
                name: ~
                description: ~

    custom_accessors:
        - Sylius\Component\Product\Accessor\ProductAccessor
    filters:
        pre_search_filter:
            enabled: true
            taxonomy: category
        facet_groups:
            search_set:
                values: [taxons, price, made_of, color]
            categories_set:
                values: [price, made_of, color, grape]
            all_set:
                values: [taxons, price, made_of]
        facets:
            taxons:
                display_name: 'Basic categories'
                type: terms
                value: ~
            price:
                display_name: 'Available prices'
                type: range
                values:
                    - { from: 0, to: 2000}
                    - { from: 2001, to: 5000}
                    - { from: 5001, to: 10000}
            made_of:
                display_name: 'Material'
                type: terms
                value: ~
            color:
                display_name: 'Available colors'
                type: terms
                value: ~
            grape:
                display_name: 'Grape'
                type: terms
                value: ~

Step 2

With the config complete, we just need to re-index our search. To do, drop yourself into the command line at the root level of your site and run app/console sylius:search:index. Depending on the speed of your server (note vagrant/VM boxes) this may take some time.

And that’s it. Once the re-index has completed, if I browse to my categories I will see my new facet to allow me to filter by grape. Easy.

For reference this has been tested on Sylius v0.12.

Leave a Reply

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