Reading list Switch to dark mode

    Add Non-Product Attribute To Magento2 Elastic Documents

    Updated 4 March 2024

    In this blog, we will learn how to add custom fields to Magento 2 elastic search documents (products). Sometimes you need to upload some data that is not a product attribute or it is a product attribute but it is not searchable neither filterable on the elastic search server, which can be later used for searching and filtering products in Magento.

    Lets see how to do that:

    First of all you need to create a class which is going to be the provider for the data that you want to index while Magento indexer runs:

    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    declare(strict_types=1);
    
    namespace Vendor\CustomModule\Model\Adapter\BatchDataMapper;
    
    use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
    
    /**
     * Provide data mapping for custom fields
     */
    class CustomDataProvider implements AdditionalFieldsProviderInterface
    {
    
        /**
         * @inheritdoc
         */
        public function getFields(array $productIds, $storeId)
        {
    
            $fields = [];
            
            foreach ($productIds as $productId) {
                $data = ""; //you can get you data here
                $fields[$productId] = ["custom_field" => $data];
            }
    
            return $fields;
        }
    
    }

    So in the above class you can see I have extended this class “Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface” which is required to create a provider class and also you need to define “getFields” method which will give you the access of all the products and store id.

    Now we have to add this class in the di.xml file of the module so that Magento can know at the time of indexing to add this field:

    Searching for an experienced
    Magento 2 Company ?
    Find out More
         <virtualType name="additionalFieldsProviderForElasticsearch" type="Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProvider">
            <arguments>
                <argument name="fieldsProviders" xsi:type="array">
                    <item name="custom_field" xsi:type="object">Vendor\CustomModule\Model\Adapter\BatchDataMapper\CustomDataProvider</item>
                </argument>
            </arguments>
        </virtualType>

    Now you have informed Magento to use you provider class to add your field to the elastic index and document.

    To check if your code is working simply open this URL in the browser to know the name of the current index:

    http://localhost:9200/_all

    In the above URL in place of localhost use your elastic server host and in place of 9200 use your elastic server port. Executing the URL in the browser you will get the schema of the elastic index to find custom_field in that and you will find it under mappings > properties keys.

    Thanks 🙂

    . . .

    Leave a Comment

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


    2 comments

  • Suresh
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home