WPSolr logo
Search
Close this search box.

Table of contents :

Building a faceted search widget with jQuery and Bootstrap

wpsolr-header-solr-elasticsearch-5

Table of contents :

Introduction

Faceted search, also known as facet filtering, is a method of navigation that allows users to narrow down a large data set or search results by applying filters to certain metadata or attributes. It’s a powerful tool that improves user experience and reduces the time it takes to find the desired information.

In this post, we’ll be discussing how to build a faceted search widget using jQuery and Bootstrap. We’ll also share some code snippets that you can use in your own projects.

Building the Faceted Search Widget

To start, we’ll need to include the necessary dependencies in our project. You can download the latest versions of jQuery and Bootstrap from their respective websites, or include them via a CDN.


  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  
  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" crossorigin="anonymous">
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" crossorigin="anonymous"></script>

Next, we’ll define the HTML structure of our faceted search widget. In this example, we’ll be using a simple search form with a dropdown list of options for filtering.


  <div class="container">
    <form class="form-inline">
      <div class="form-group">
        <input type="text" class="form-control mr-2" placeholder="Search">
        <select class="custom-select">
          <option selected>All</option>
          <option>Option 1</option>
          <option>Option 2</option>
          <option>Option 3</option>
        </select>
      </div>
      <button type="submit" class="btn btn-primary">Search</button>
    </form>
  </div>

Now we’ll add some JavaScript to handle the filtering logic. This code will listen for changes to the dropdown list and update the search query accordingly.


  $(document).ready(function() {
    $('.custom-select').on('change', function() {
      var selected = $(this).val();
      
      // Update search query with selected filter
    });
  });

Finally, we’ll add some PHP code to handle the search functionality on the server side. This example uses a simple array of data to simulate a database, but you can replace this with a real query to your database.


  $data = array(
    array('id' => 1, 'name' => 'Result 1', 'type' => 'Option 1'),
    array('id' => 2, 'name' => 'Result 2', 'type' => 'Option 2'),
    array('id' => 3, 'name' => 'Result 3', 'type' => 'Option 3'),
    // more data here...
  );
  
  $query = $_POST['query'];
  $filter = $_POST['filter'];
  
  // Filter data by selected option
  if($filter != 'All') {
    $data = array_filter($data, function($item) use ($filter) {
      return $item['type'] == $filter;
    });
  }
  
  // Search data by query string
  if(!empty($query)) {
    $data = array_filter($data, function($item) use ($query) {
      return strpos($item['name'], $query) !== false;
    });
  }
  
  // Return filtered and searched data as JSON
  header('Content-Type: application/json');
  echo json_encode($data);

Using WPSOLR to Enhance Faceted Search

While the approach outlined above is perfectly valid, it can be enhanced significantly with the use of a dedicated search plugin such as WPSOLR.

WPSOLR is a powerful and flexible search plugin that allows you to easily customize your search results and add a range of filters to your search form, including faceted search. It offers extensive support for both WordPress and WooCommerce, making it an ideal choice for any type of website.

With WPSOLR, you can easily add custom filters based on categories, tags, custom taxonomies, and more. You can also apply multi-faceted filtering, sorting, and other advanced features to help users find the exact information they need quickly and efficiently.

Conclusion

Faceted search is a powerful tool that can greatly improve user experience and reduce the time it takes to find the desired information. With jQuery, Bootstrap, and a few lines of PHP code, you can easily build a faceted search widget and customize it to fit your unique needs.

However, with WPSOLR, you can take your faceted search to the next level and add a range of custom filters and features to your search form. Whether you’re building a website on WordPress or WooCommerce, WPSOLR is an excellent tool to enhance your search functionality and improve user engagement.

Related posts ... not powered by WPSOLR 😊