Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-marketplace-mod

    Add Non-Product Attribute To Magento2 Elastic Documents

    In this blog, we will learn how to add custom fields to 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:

    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:

    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 🙂

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close