Using Apache Solr for API-driven searchfor Drupal 8 , 9 , 10 , and 11

Last updated :  

To achieve advanced search capabilities in Drupal's headless setup, leveraging Apache Solr can significantly boost performance and relevancy. This lesson will walk you through integrating Apache Solr with Drupal for efficient, API-driven search functionalities, enhancing the search experience of your frontend applications.

Why Choose Apache Solr?

Apache Solr is an open-source search platform renowned for its speed and scalability. It offers advanced search functionalities, such as faceting, spell correction, and full-text search, making it an excellent choice for large datasets and complex queries.

Setting Up Apache Solr with Drupal

Step 1: Install Apache Solr

Begin by setting up Apache Solr on your server, as it's necessary to have a running Solr instance before integrating it with Drupal.

  1. Download and Install Solr: Obtain Solr from the official website and follow the installation instructions for your operating system.
  2. Start Solr: Launch Solr using the command:
    bin/solr start

Step 2: Configure Solr Core

  1. Create a new Solr core for your Drupal site:
    bin/solr create -c drupal_core
  2. Copy the Drupal Solr configuration files to the Solr core directory. The configuration files are typically found in the modules/contrib/search_api_solr/solr-conf/ directory within your Drupal installation.

Integrating with Drupal

Step 1: Install the Search API Solr Module

  1. Install the module via Composer:
    composer require drupal/search_api_solr
  2. Enable the module using Drush:
    drush en search_api_solr -y

Step 2: Add a Solr Server and Index

  1. Navigate to Configuration > Search and Metadata > Search API and click Add server.
  2. Configure the connection to your Solr instance by providing the Solr server URL and core name.
  3. Add an index that connects with the Solr server, selecting relevant content entities and fields to index.

Exposing Solr Search via API

To make the Solr-powered search accessible to your frontend, create a Drupal View and expose it using REST:

  1. Create a view of the type Index, selecting your Solr index as the source.
  2. Add a REST export path to allow frontend applications to access the API, similar to setting up REST in the previous Search API lesson.

Testing the Integration

Perform a series of test searches to ensure Solr is indexing and returning results correctly. Adjust Solr configurations to fine-tune search relevancy and performance based on tested outcomes.

Conclusion and What's Next?

Integrating Apache Solr with Drupal dramatically improves search capabilities in your headless CMS setup, resulting in faster and more powerful search experiences. With Solr's advanced features, your frontend applications can deliver user-friendly and efficient search operations.

In the next lesson, we will explore Applying Cache Tags to Search Responses, helping you optimize search performance through effective caching strategies. Keep building on your headless Drupal knowledge!