Mastering Elasticsearch Queries for Advanced WooCommerce Search

Supercharge Your WooCommerce Search with Elasticsearch Queries

What Is an Elasticsearch Query?

Think of Elasticsearch as the engine behind your site’s supercharged search. It gives you various query types to quickly pull up the data your customers are looking for. Whether you’re setting up a basic search or something more advanced, Elasticsearch has you covered. Let’s go through a few key query types:

  • 1. Match Query

The Match Query is perfect when you want to search products using a search term. It matches keywords in specific fields and is great for full-text searches. You can tweak it with options like fuzziness or minimum_should_match to make the search even more flexible. Here’s an example of how you’d use it in PHP:


$query = [
 'match' => [
  'product_name' => 'red shoes'
 ]
];

$response = $client->search([
 'index' => 'your_index_name',
 'body' => [
  'query' => $query
 ]
]);
PHP
  • 2. Term Query

Need to find exact terms or phrases? The Term Query is your go-to. Unlike other queries, it doesn’t analyze the text—it’s all about finding exact matches. This works great for things like product codes or specific categories. Here’s how you’d implement it in PHP:


$query = [
 'term' => [
  'category' => 'electronics'
 ]
];

$response = $client->search([
 'index' => 'your_index_name',
 'body' => [
  'query' => $query
 ]
]);
PHP
  • 3. Range Query

If you’re looking to filter results by a specific range, like price, ratings, or availability, the Range Query is your best bet. Whether you want to show items within a certain price range or products available on specific dates, this query helps narrow it down quickly. Here’s how you can use it in PHP:


$query = [
 'range' => [
  'price' => [
   'gte' => 50,
   'lte' => 100
  ]
 ]
];

$response = $client->search([
 'index' => 'your_index_name',
 'body' => [
  'query' => $query
 ]
]);
PHP

Using WPSOLR for Advanced WooCommerce Searches

Elasticsearch is powerful, but setting it up can be complex. That’s where WPSOLR steps in! It makes integrating Elasticsearch with WooCommerce easy, offering a simple interface for advanced search features. You can tweak filters, adjust relevance, and enable faceted navigation with ease. Plus, it handles automatic indexing, real-time updates, and even supports multi-language searches no coding required!