Combining the powers of Apache Solr and Drupal 8     results in unmatched digital experiences with high-performing, enterprise-level search features and functionality. In this article, we will learn about why Apache Solr should be chosen and how we can configure Apache Solr in Drupal 8. 

What is Apache Solr? 

Solr is a solid and ascendable open-source search platform that provides distributed indexing and load-balanced querying. Built initially for and by CNET Networks, this Java based project was later offered to the Apache Software foundation.  Apache Solr is the best solution for super-fast, credible and awesome search applications. Big guns like Netflix, Instagram and Twitter including various e-commerce sites and CMSs, use Apache Solr for their search functionality.

Dupal-apache-solr

Why choose Apache Solr?

With many options available in Drupal 8 core to implement your search functionalities and features, why should you choose Apache Solr with Drupal 8? Here are some reasons Apache Solr might be the best fit for your project:

  1. Apache Solr offers Faceted navigation to let users add multiple filters that can help them navigate easily through piles of information. Facets are elements for navigation that can be queried.
  2. It allows for Full-text searches that offers precise results along with its near real-time indexing and searching capabilities. Indexing with Apache Solr is not only faster, it can also be merged and further optimized. 
  3. The Hit Highlight feature enables highlighting the search words or phrases to make it easy to identify.
  4. The Dynamic clustering feature allows to group search results and offer related searches or recommendations.
  5. Allows spell check and auto-complete suggestions for a better search experience.

Using Apache Solr with Drupal 8 enables better control over your website search and offers an interactive admin interface. Check out how we enabled a leading Healthcare provider to boost their search experience with Apache Solr and Drupal.

Implementing Apache Solr in Drupal 8

Let us divide this process into the following parts:

  1. Install Apache Solr
  2. Install the Drupal Solr Module 
  3. Configure Apache Solr with Drupal Module

1. Installing Apache Solr

Step 1: Install Java

As Apache Solr is completely based on Java, we need to install Java to begin with. For Apache Solr 7, we need Java 8 or higher to run. If you don't have Java installed in your system, install Java using the below command –

$sudo apt install openjdk-11-jdk

Verify the active Java version using the below command - 

$java -version

Step 2: Install Apache Solr on Ubuntu

Now, you can download the latest Apache Solr version from its official site. Else, you can also use below command –

$cd /opt
$wget https://archive.apache.org/dist/lucene/solr/7.7.2/solr-7.7.2.tgz

Now, extract Apache Solr service installer shell script from the downloaded Solr archive file and run the installer using the following commands.

$tar xzf solr-7.7.2.tgz solr-7.7.2/bin/install_solr_service.sh --strip-components=2
$sudo bash ./install_solr_service.sh solr-7.7.2.tgz

Now, Solr should be installed on your system. You can use these commands to Start, Stop and check the status of Solr service –

$sudo service solr stop
$sudo service solr start
$sudo service solr status

Default Solr runs on port 8983. You can access your Solr admin panel by typing localhost:8983 in your browser.

Install-apache-solr

2. Installing the Solr module in Drupal 8

The Drupal 8 Search API Solr module should be installed before we go any further. This module integrates Drupal with the Apache Solr search platform and provides a Solr backend for the Search API module.

Install this module in your site using Composer using the below command –

$composer require drupal/search_api_solr

Once it is done, enable the module.

Go to Extend → Search Search API Solr Search → Enable it

3. Configure Apache Solr with the Drupal Search API Solr Module

Step 1: Create a core in Apache Solr

After Installation of Solr, you need to create a core in the Solr in order to work with the Solr platform. This is an important step where we can index the contents to the Solr core. You can see the indexed content in the core which you have created. You can create the core using the below command in ubuntu –

$sudo su - solr -c "/opt/solr/bin/solr create -c first_solr_core -n data_driven_schema_configs"

This command will differ depending on where Solr is installed. Here, it is in the /opt folder of my Ubuntu system. You can see the created core in the Solr admin panel.

solr server

Step 2:  Create a Solr Server

In this step you need to create a Solr server and index in your Drupal website. To create a Solr server –

Go to Configuration -> Search And Metadata -> Search API -> click on Add server

Apache solr

When you click on the Add server you will get one form you need to fill that form 

  1. Server name: Enter the Server name
  2. Enable: You need to check the enable check box. If you don’t enable it, you cannot index the items
  3. Add server description: A brief description of the server.
Apache-solr

Next, you need to configure the Solr backend. Click on the CONFIGURE SOLR BACKEND in the form. You can see the form elements as shown in the image below:

solr collector

Solr Connector: There are four connectors available. You need to select a connector to use for this Solr server. I am using the Standard connector.
 
HTTP protocol: Choose https or Http (depending if your server uses SSL or not).
Solr host: localhost (if your Solr Server is on a different machine, please enter the IP or hostname of that host here)
Solr port: 8983 (This is Default port)
Solr path: "/" (The path to locate server and this is based on the solr server version If the solr server version is 6.6.4 in this case solr path is different like "/solr" ).
Solr core: Enter the solr core name which you created before.
The values as defined default work fine. However, you can also change them accordingly.

Step 3: Create the Search Index

Here we are going to create a Search API index that will index the Datasources that you selected in the index. To create an index –

Go to Configuration -> Search and Metadata -> Search API -> Add index

apache-solr

Index name: Enter an index name.

Data sources: Here you need select the Datasources to index the items for the search functionality. In my example, I selected content. When you select the content, you will get the option to select the bundles in the CONFIGURE THE CONTENT DATASOURCE. Here you need to select the bundles you want to index.
Server: Select the server to index the items. I have selected the Apache-solr-server.
Enabled: This enables the index. Don’t forget to enable the selected server as well for this to work.

Once done, hit Save and add the field. 

solr-apache

After successfully creating an index, you now need to add fields to the index. 

To add the field that you need, go back to the search API, click on Edit the index which you have created.

You will then see the Fields tab. Click on the fields tab and you will see a window as shown in the image below.

solr-apache

Next, click on the Add fields. In the popup that appears, you need to add the fields you need. After adding the fields, click on Done and then Save.

index-solr

Now, the Index and server are created on the website. After this, the next Important step is copy the configuration files to Solr core. For this, you need download the config files from the server in the website. 

Go to Configuration -> Search and Metadata -> Search API -> <Server name> 

You will get a window as shown in the image below –

solr apache

Next, click on the “Get config.zip” button. This will download the config folder. Once done, extract the zip folder and copy all the files into the conf folder in the core.

get-config.zip

To copy the files, open the terminal and move to the Solr core folder. In Ubuntu, Solr core folder will be stored in the /var/solr/data. In the data folder, you will see the Solr core folder which you had created earlier. Go to that Solr core folder and you will find a folder with the name conf. in this folder you will find some files. Delete those files and copy all the files from the config folder that is downloaded from the website server. Once done, you need to restart your Solr server in the terminal.
Next step is to index the contents –

Go to Configuration -> Search and Metadata -> Search API -> click on the index that you have created. You will see a window as shown in the image below.

search and metadata

Next, click on the index to index the contents. Now all the contents are indexed in the Solr core. Now you can create a view of Solr index and add the indexed fields to the view to get the results from the Drupal 8 Solr.

Sahana NApr 28, 2020