WPSolr logo
Search
Close this search box.

Table of contents :

Leveraging Elasticsearch to power faceted search in WordPress and WooCommerce

wpsolr-header-solr-elasticsearch-5

Table of contents :

Introduction

Faceted search is a powerful feature that allows users to filter search results based on specific criteria. Elasticsearch is a highly scalable search engine that can be leveraged to provide faceted search capabilities in WordPress and WooCommerce. In this post, we’ll explore how Elasticsearch can be used to power faceted search and provide some code snippets to help you get started.

Using Elasticsearch for Faceted Search

Elasticsearch has powerful aggregation features that can be used to break down search results into categories based on specific criteria. These categories can be presented to users in a sidebar, allowing them to easily filter results based on their preferences.

To get started, you’ll need to install Elasticsearch on your server and create an index for your content. Once your index is set up, you can use the Elasticsearch PHP client to query your data and retrieve aggregated results.

The following code shows an example of querying Elasticsearch for a set of products and aggregating them by category:

$client = Elasticsearch\ClientBuilder::create()->build();

$params = [
    'index' => 'myindex',
    'type' => 'product',
    'body' => [
        'aggs' => [
            'categories' => [
                'terms' => [
                    'field' => 'category',
                ],
            ],
        ],
    ],
];

$response = $client->search($params);

$categories = $response['aggregations']['categories']['buckets'];

This code retrieves data from an index called “myindex” and a type called “product”. It then creates an aggregation based on the “category” field, which will return a list of categories and the number of products in each category. The resulting data is stored in the $categories variable.

You can use this data to generate a list of categories in your search results sidebar. When a user selects a category, you can query Elasticsearch again with a filter based on that category to retrieve only the products that match that criteria.

JavaScript Client for Faceted Search

In addition to using the PHP client to query Elasticsearch, you can also use the Elasticsearch JavaScript client to build faceted search interfaces directly on your website.

The following code shows an example of using the JavaScript client to retrieve data from Elasticsearch and render it in a faceted search interface:

// Create a client instance
var client = new $.es.Client({
  hosts: 'localhost:9200'
});

// Define our aggregation
var agg = {
  categories: {
    terms: {
      field: 'category'
    }
  }
};

// Query Elasticsearch and process the results
client.search({
  index: 'myindex',
  body: {
    aggs: agg
  }
}).then(function (response) {
  var categories = response.aggregations.categories.buckets;
  
  // Render categories as a list
  var html = '';
  $.each(categories, function (i, category) {
    html += '
<ul>
 	<li>' + category.key + ' (' + category.doc_count + ')' + '</li>
</ul>';
});
$('#categories').html(html);
});

This code creates a client instance for Elasticsearch and defines an aggregation for categories. It then queries Elasticsearch for data from an index called “myindex”, processes the results and renders a list of categories in a HTML element called “categories”.

WPSOLR Integration

If you’re looking for a more user-friendly way to add faceted search to your WordPress site, the WPSOLR plugin is a great option. WPSOLR integrates Elasticsearch directly into WordPress and provides a visual interface for building faceted search interfaces.

With WPSOLR, you can easily create and manage Elasticsearch indexes, map your WordPress content to Elasticsearch fields, and build custom facets based on any field in your index. The plugin also provides a variety of search widgets and shortcodes for easily integrating search functionality into your site.

Conclusion

Elasticsearch is a powerful search engine that can be leveraged to provide faceted search capabilities in WordPress and WooCommerce. By using the Elasticsearch PHP client or the Elasticsearch JavaScript client, you can build custom faceted search interfaces that allow users to filter results based on specific criteria. Alternatively, you can rely on the WPSOLR plugin for a more user-friendly solution. Regardless of which approach you take, Elasticsearch is a flexible and scalable solution for building search functionality into your website.

Related posts ... not powered by WPSOLR 😊