Menu Close
    Hire us Request quote Reading list Switch to dark mode

    How to replace magento2 default search engine

    Need:

    By default, magento2 uses MySQL full text for search operations, but is MySQL enough to handle the huge amount of data with complex queries? The answer is no, in the competitive world speed can give you a huge plus over other competitors, and if you have a huge amount of data that needed to be searched in real time MySQL can set you back, but if you have a small amount of data MySQL will do good for you. So let’s see how we can replace mysql search engine in magento2.

    How to do this in Magento2:

    There are several options that you can choose to switch from MySQL search, such as elastic search, solr search, amazon cloud search etc, but we are talking about replacing an already build sophisticated search engine but the best thing about magento2 code structure is it is highly scalable, and build with components that you can easily replace with new technologies. So let’s see how we can switch from MySQL search engine to any other search engine, suppose we want to replace MySQL search with the elastic search.

    I am assuming that you already know how to build a module in magento2, so lets start :

    Create di.xml file:

    create a di.xml file at the path app/code/CompanyName/ModuleName/etc/di.xml and add this type configuration:


    in the above type configuration, you can see I have inserted a string elastic in the engines array of class “Magento\Search\Model\Adminhtml\System\Config\Source\Engine” this class is responsible for rendering search engine options in magento2  stores > configuration > catalog > storefront > catalog search > search engine :

     

    After you have added the entry in di.xml, it will start showing elastic in the search engine select box like this:

     

    By just saving the elastic option here in the config you have managed to change the MySQL search engine, now you need to add your own logic to index data on the elastic server and then search from there. After saving the elastic option if you will try to load your storefront it will start giving LogicException that there is no such adapter: elastic, you need to add your own adapter class to render data from the elastic server, for that you need to add another type configuration in the same di.xml file:


    So, now you can see I have added another type configuration in the di.xml file this time I have added a class name string with key elastic (the key need to be the same as we have used in the previous type configuration) inside adapters array for class “Magento\Search\Model\AdapterFactory”. Let’s see how Magento adapter class looks like:


    Above class is MySQL adapter class, you can see it has implemented “AdapterInterface” interface and implemented “query” function definition from the interface, your class need to do the same thing, and inside query method you can implement your own logic to build the elastic query, aggregations and then fetch data from the elastic server, the same thing with any other search engine, but the query function needs to return the data in a certain format, you need to create that format too after getting the result from the desired search engine(in this case elastic), the format looks like this:


    the above is the result of the query for the text “test” and generated using PHP print_r method, in the above response entity_id is the searched product id and the score is the weight of each result, to set its position in the search results.

    So that’s how you can implement another search engine in magento2, hope this will help you, in case of any doubt please comment below.

    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 received your message and our expert will get back to you shortly.
    Send Again
    Close