Table of contents :

How to customize Apache Solr search queries for WooCommerce

wpsolr-header-solr-elasticsearch-5

Table of contents :

Introduction

Apache Solr is a highly flexible and powerful search engine that is widely used in e-commerce websites like WooCommerce. It is built on top of the Lucene search library and provides excellent search capabilities in terms of scalability, performance, and relevance. However, Solr’s power comes with a price – customization. Configuring Apache Solr for WooCommerce can be a daunting task, especially if you are not familiar with its underlying technologies. In this post, we’ll take a holistic approach to guide you on how to customize Apache Solr search queries for WooCommerce in an uncomplicated way.

How to Customize Apache Solr Search Queries for WooCommerce?

Apache Solr’s search capabilities revolve around indexing and querying. The former is the process of adding content into Solr, while the latter is the process of searching for content within Solr. To customize Solr search queries for WooCommerce, we’ll focus on the query phase.

By default, WooCommerce uses WordPress’ built-in search engine to power its search queries. This method is not ideal, as it can cause performance issues and deliver irrelevant results. To improve WooCommerce search capabilities, we’ll need to integrate Apache Solr into WooCommerce query parsing.

Here are six essential steps to customize Apache Solr search queries for WooCommerce:

Step 1: Install and Configure Apache Solr

Install and configure Apache Solr on your local computer or server. Ensure you have installed the latest version of Java and Apache Solr. You can download the latest version of Apache Solr at solr.apache.org.

After installation, configure your Solr instance to work with WooCommerce by creating a new Solr core. This core should be named according to the WordPress installation that you’ll be linking it with. In our example, we’ll use the name ‘woocommerce’.

Step 2: Install the Solr Plugin for WordPress

Next, install the Solr plugin for WordPress to integrate Solr into your WooCommerce search queries. This plugin is available for free on the WordPress plugin repository.

After installation, navigate to the plugin’s settings page and enter the URL for your Solr instance and the name of your Solr core.

Step 3: Enable WooCommerce Query Parsing

To enable WooCommerce query parsing, you’ll need to create a new WordPress plugin and add the necessary code to parse user search queries. The following PHP client code can be embedded in HTML tags to help parse the user’s search query:


add_filter('posts_search', 'woocommerce_search', 500, 2);
function woocommerce_search($where, $wp_query) {

   if (empty($where))
        return $where;

    global $wpdb;

   // Parse the search query and format it 
    $query = $wp_query->query_vars['s'];
    $query = esc_sql( trim( strip_tags( $query ) ) );
    $search_terms = explode(' ', $query);
    $search_terms = array_filter($search_terms, 'strlen');

   if(empty($search_terms))
        return $where;

    // Search in the WooCommerce products table
    $where .= " AND (";

   foreach ($search_terms as $term) {
        $where .= " ($wpdb->posts.post_title LIKE '%$term%'
                        OR $wpdb->posts.post_excerpt LIKE '%$term%'
                        OR $wpdb->posts.post_content LIKE '%$term%') AND ";

        $where .= " ($wpdb->posts.post_type IN ('product', 'product_variation')) OR ";
    }

    $where = rtrim($where, "OR ") . ") ";

    return $where;
}

Step 4: Add Custom Fields to the Solr Schema

Custom fields that are unique to your WooCommerce products can be added to the Solr schema. A custom field could be any attribute that uniquely identifies your products like size, color, brand, or any other custom attribute.
To add custom fields, navigate to your Solr instance’s admin panel and access the Core Admin page. Then, click the name of the core you created in step 1 to access the core’s admin page. Next, select the ‘Schema Browser’ tab and scroll down to the ‘Fields’ section.
In the ‘Fields’ section, click the ‘Add Field’ button to add a new custom field to the schema. Supply a unique name for the field and the field’s type, depending on the data that will be stored in the field.

Step 5: Configure Solr’s Query Parser

Solr uses a query parser to interpret user search queries. By default, Solr’s query parser is configured to search for exact matches. However, we need to configure it to perform fuzzy matches that account for spelling mistakes or synonyms.
To configure Solr’s query parser, navigate to the ‘Solr Admin’ dashboard and select your Solr core from the ‘Core Selector’. Next, click the ‘Query’ link on the left-hand menu to access the query settings page.
On the query settings page, scroll down to the ‘Query Parser’ section and select the ‘edismax’ query parser. The ‘edismax’ parser is ideal for e-commerce websites that need to deliver results that are post-processed and weighted.

Step 6: Test Your New Search Queries

By now, you should have configured Apache Solr for WooCommerce search queries, indexed your products, added custom fields, and configured the query parser. The last step is to test your new search queries and check if they deliver relevant search results.
Use the search bar on your WooCommerce website and enter a term or phrase that encapsulates your products’ key features. For instance, if you are selling shoes, a search query could be ‘red sandals size 7’. The search results should include product listings that match the query’s terms.

How WPSOLR Can Help

WPSOLR is a WordPress plugin that enhances your Solr search capabilities by delivering features like autocompletion, faceting, geo-searching, and indexing custom fields. Installing WPSOLR can help you customize and configure your Apache Solr search queries for WooCommerce, delivering a powerful and tailored search experience that results in increased conversions.

Conclusion

Customizing Apache Solr search queries for WooCommerce requires a comprehensive approach that involves configuring and integrating the various components that power Solr’s search capabilities. By following the steps outlined above, you can achieve a powerful and flexible search engine that improves your WooCommerce website’s user experience and increases customer satisfaction. Remember that WPSOLR is an excellent tool that can help you get the most out of your Solr installation by delivering additional features and capabilities.

Trending posts