WPSolr logo
Search
Close this search box.

Table of contents :

Understanding Elasticsearch queries and filters in WooCommerce

wpsolr-header-solr-elasticsearch-5

Table of contents :

Introduction

Elasticsearch is a powerful open-source search engine that is widely used in modern web applications. It is used by top companies such as Netflix, GitHub, and SoundCloud for handling their search and analytics services. Elasticsearch is a distributed system that stores, retrieves, and searches data in real-time. It provides a powerful search API for querying data based on a wide range of criteria. In this post, we will explore how Elasticsearch queries and filters work in WooCommerce and how they can be implemented using the PHP client.

Understanding Elasticsearch Queries and Filters in WooCommerce

Elasticsearch uses a query language called the Query DSL (Domain Specific Language) to construct queries for searching data. In WooCommerce, Elasticsearch is used for storing and searching product data. The Query DSL provides a wide range of search functionality, such as full-text search, faceted search, fuzzy search, and more.

Let’s take a look at some of the most commonly used query and filter types in Elasticsearch.

Match Query

The Match Query is used to search for a specific keyword in the indexed data. This query is useful for simple searches where you want to match a keyword or phrase in a specific field. For example, if you want to search for products with the word “t-shirt” in their description, you can use the following Match Query:


$params = [
 'index' => 'my_woocommerce_index',
 'body' => [
  'query' => [
   'match' => [
    'description' => 't-shirt'
   ]
  ]
 ]
];

This query will search for the word “t-shirt” in the “description” field of all the products in the “my_woocommerce_index”.

Term Query

The Term Query is used to search for exact matches of data in the indexed field. This query is useful for filtering data based on specific criteria. For example, if you want to search for products with the color “blue”, you can use the following Term Query:


$params = [
 'index' => 'my_woocommerce_index',
 'body' => [
  'query' => [
   'term' => [
    'color' => 'blue'
   ]
  ]
 ]
];

This query will search for all the products in the “my_woocommerce_index” with the “color” field set to “blue”.

Range Query

The Range Query is used to search for data within a specific range of values. This query is useful for filtering data based on a specific range of values such as price, weight, or size. For example, if you want to search for products with a price range of $10 to $50, you can use the following Range Query:


$params = [
 'index' => 'my_woocommerce_index',
 'body' => [
  'query' => [
   'range' => [
     'price' => [
      'gte' => 10,
      'lte' => 50
    ]
   ]
  ]
 ]
];

This query will search for all the products in the “my_woocommerce_index” with the “price” field between $10 and $50.

Filter Query

The Filter Query is used to filter data based on a set of criteria. This query is useful for refining search results based on specific criteria such as color, size, price, or category. For example, if you want to search for products with a price range of $10 to $50 and color “blue”, you can use the following Filter Query:


$params = [
 'index' => 'my_woocommerce_index',
  'body' => [
   'query' => [
    'bool' => [
     'must' => [
      [
       'range' => [
        'price' => [
         'gte' => 10,
         'lte' => 50
         ]
      ]
    ]
  ],
    'filter' => [
      [
        'term' => [
        'color' => 'blue'
      ]
     ]
    ]
   ]
  ]
 ]
];

This query will search for all the products in the “my_woocommerce_index” with the “price” field between $10 and $50 and the “color” field set to “blue”.

Using Elasticsearch with the WooCommerce PHP Client

The Elasticsearch PHP client provides an easy-to-use interface for interacting with an Elasticsearch instance. You can use this client to perform queries and filters on your WooCommerce data.

Here’s an example of how to use the Elasticsearch PHP client to perform a simple full-text search on your WooCommerce data:


use Elasticsearch\ClientBuilder;

$hosts = [
'https://localhost:9200'
];

$client = ClientBuilder::create()
->setHosts($hosts)
->build();

$params = [
 'index' => 'my_woocommerce_index',
 'body' => [
  'query' => [
   'match' => [
    'description' => 't-shirt'
   ]
  ]
 ]
];

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

This will search your WooCommerce data for the word “t-shirt” in the “description” field and return the search results in the $response variable.

How WPSOLR Can Help

WPSOLR is a plugin that integrates Elasticsearch with WordPress and WooCommerce. With WPSOLR, you can easily set up Elasticsearch as your search engine and improve the search experience for your WooCommerce store.

WPSOLR provides a user-friendly interface for configuring search settings, such as search fields, filters, and facets. It also provides an Elasticsearch PHP client module that makes it easy to interact with Elasticsearch in your code.

Whether you’re looking to improve your search engine’s performance or add advanced search features to your WooCommerce store, WPSOLR can help you achieve your goals.

Conclusion

Elasticsearch is a powerful search engine that can help improve the search experience for your WooCommerce store. Understanding how Elasticsearch queries and filters work is essential for building efficient search queries and filters. With the Elasticsearch PHP client and the WPSOLR plugin, you can easily integrate Elasticsearch with your WooCommerce store and improve the search experience for your customers.

Related posts ... not powered by WPSOLR 😊

Personalization
What is personalization ?

Personalization has become impossible to ignore in today’s digital landscape, permeating various aspects of our online experiences. As consumers increasingly expect personalized experiences, businesses across