Solr and Magento – search by department

Related Inchoo Services

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();
// 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)

The results

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Maja Kardum
, | 0

Ready for 2nd Meet Magento Croatia with agenda focused on PWA? Book the dates!

Nenad Andrakovic
, | 3

Klevu – search engine that will increase revenue, self-learn and improve UX

Marko Martinovic
Marko Martinovic, | 2

PCI compliance with Magento a.k.a. how to compile Solr from source

1 comment

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.