Skip to main content
In some situations, it makes sense to index a numeric representation of a string attribute, for example for sorting by clothing size. Size is usually a string (S, M, L, etc), but sorting alphabetically will yield strange results (S, L, M). It makes sense to replace these values with a numeric attribute to sort. It’s possible to add a new attribute containing this value by implementing a custom observer. To make changes to the attributes being indexed, observe the algolia_after_create_product_object event with a custom observer.
This tutorial assumes knowledge of writing a custom observer. For more information, see Create a custom extension.

Create observer

Create the Observer/TransformAttribute.php file, and add a new Observer class in it.
PHP
namespace Algolia\CustomAlgolia\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class TransformAttribute implements ObserverInterface
{
  public function execute(Observer $observer)
  {
    $record = $observer->getData('custom_data');

    // $sizes = ['S', 'M', 'L'];
    $sizes = $record['sizes'];

    // S => 1, M => 2, L => 3
    $replacementNumbers = [1, 2, 3];

    $record['numeric_sizes'] = array_replace($sizes, $replacementNumbers);
  }
}
Modify the code in the execute block as needed. In this example, the clothing sizes are transformed into numeric values.

Register the observer

To register your observer, add the following snippet to the etc/events.xml file:
XML
<event name="algolia_after_create_product_object">
    <observer name="customalgolia_transform_attribute" instance="Algolia\CustomAlgolia\Observer\TransformAttribute" />
</event>

Reindex data

With the custom observer in place, the catalog needs to be reindexed for the changes to take effect.
⌘I