There are eCommerce stores which sell a wide variety of products like food, personal care, electronics, and so on. On those stores, visitors want to be able to search by a specific category. This can be achieved by adding a new feature: search by department or category. In this quick tutorial I will explain the base concept of how to do it using Solr search engine as an example.
Assuming you already use Solr search server on your eCommerce site, first step should be checking if there is a category_ids field in Solr index. Category_ids should exist in Solr index in order to be able to filter by category_id.
To check if the field exists, call this url: http://localhost:8983/solr/collection1/select?q=*%3A*&wt=json&indent=true.
You can see category_ids in the response below:
Field category_ids should be declared in Solr schema.xml as in the following example:
<field name="category_ids" type="int" indexed="true" multiValued="true"/>
Category_ids field is multivalued and indexed, can contain more values, and product can be assigned to more categories.
If the previous conditions are fulfilled, only the Magento side should be modified to send a proper category filter with search phrase. If you use Solarium client, it is pretty easy.
$client = new Solarium\Client($config); // get a select query instance $query = $client->createSelect(); $query->setQuery('Teflon'); $query->setFields(array('id','name','price')); // create a filterquery by category id 3887 $fq = $query->createFilterQuery('category_ids')->setQuery('category_ids:3887');
If you are interested in finding out more about the Solarium concepts, click here.
Finally, query to Solr should look like this:
q parameter is search terms “Teflon”
fq parameter is filter by category id 3887 (more about Solr common parameters)
This is how a “search by category” on Magento frontend can look like:
With this feature you can improve site search performance and decrease the need for search refinement which, ultimately, has direct impact on your eCommerce conversion rate.