Colorfield logo

Drupalicious

Published on

Install Solr 7 for Drupal 8 Search API on Ubuntu 16.04

Authors
Solr and Drupal logos

Before we start, here is an Amazon like demo of Search API given by Joris Vercammen (@borisson_) at Drupal Camp Antwerp 2017.

https://www.youtube.com/embed/BxcSXfe8ltQ

1. Install Solr

Install Java 10

sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
sudo apt install oracle-java10-installer

Test Java

java -version

It should print something like:

_java version "**10.x.x**"_
_(...)_

Install Solr as a service

cd ~
# get the latest 7.x.x release via https://lucene.apache.org/solr/mirrors-solr-latest-redir.html
wget http://archive.apache.org/dist/lucene/solr/7.4.0/solr-7.4.0.tgz
# extract the service installation file
tar xzf solr-7.4.0.tgz solr-7.4.0/bin/install_solr_service.sh --strip-components=2
# install Solr as a service
sudo ./install_solr_service.sh solr-7.4.0.tgz
# cleaning up
rm solr-7.4.0.tgz
rm install_solr_service.sh

Check then the Solr status.

service solr status

Solr status

2. Get the Search API Solr module

This command will get the module and its dependencies (Search API module and vendors).
Get the 2.x version as 1.x does not support Solr 7 (and will not).
Also, Search API 2.x merges Search API Multilingual Solr Search and Search API Datasource.

We are using 2.1 at the time of writing, with Drupal 8.5.x

composer require drupal/search_api_solr:^2.1

3. Configure Solr for Search API

Create a collection (also known as 'core') and replace the default Solr configuration by the one that is shipped with the Search API module.

# Create your collection
sudo su - solr -c "/opt/solr/bin/solr create -c my_collection -n data_driven_schema_configs"

# Go to the collection directory and get the Search API schema
# Become root first
sudo su
# Then go to the Solr collections directory
cd /var/solr/data/my_collection
# Keep a copy of the original config (or just rm -Rf conf)
mv conf conf.orig
# Copy the Search API Solr configuration from your contributed modules directory
cp -R /var/www/my_site/docroot/modules/contrib/search_api_solr/solr-conf/7.x conf

# Change owner and group
chown -R solr:solr conf
# Change permissions
chmod -R 775 conf

# Restart Solr service
service solr restart

# Exit root session
exit

4. Install and configure Search API Solr

Enable the Search API Solr and the Solr Search Defaults modules, this last one comes with a default content index configuration and can be uninstalled once the configuration has been created.

# cd in your docroot then
drush en search_api_solr search_api_solr_defaults

This command will enable the following modules: search_api_solr, search_api_solr_defaults, search_api, language.

Go to the Search API configuration under /admin/config/search/search-api and edit the Solr Server.

Search API configuration

Then set the name of your collection under the Solr core field.
Other values can be left as is in this basic configuration.

Solr Search API configuration

Uninstall the Solr Search Defaults module as we now have the configuration.

drush pmu search_api_solr_defaults -y

If not done yet, disable the core Search, because it is now duplicated with Search API and it is a performance issue.

drush pmu search -y

5. Configure your Solr content index

You can then continue by finetuning the default index to your needs (Configuration > Search API).

Search API default content index configuration

There you can define :

  • The data sources: entity types that can be exposed. Defaults to 'Content' (nodes), but can apply to User, Blocks, Taxonomy vocabulary, File, ...
  • Fields that you can then use in your Search API index View.
  • Processors will handle content access, highlighting, case and charachters insensitive search, stemming, ...

Once you are done, if necessary, you can edit the Solr search content View that has been created by the Solr Search Defaults module.

Solr search content view

6. Generate some content

If you have nothing to search on to test your configuration, let's fix this by using Devel Generate.

# Get the Devel module
composer require drupal/devel
# Enable Devel Generate
drush en devel_generate -y
# Generate some articles
drush genc 20 --types=article

Here is a short list of modules that are part of the Search API ecosystem for Drupal 8.

Resources