Before we start, here is an Amazon like demo of Search API given by Joris Vercammen (borisson_) at the Drupal Camp Antwerp 2017.
1. Install Solr
Install Java 9
Note that if you already have Java 8 installed, it is still useable with Solr 7.
# Add the PPA sudo add-apt-repository ppa:webupd8team/java # Update the package repository sudo apt-get update # Download and install sudo apt install oracle-java9-installer # Set Oracle JDK9 as default sudo apt install oracle-java9-set-default
It should print something like:
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
Install Solr as a service
cd ~ # get the latest 7.x release via https://lucene.apache.org/solr/mirrors-solr-latest-redir.html wget http://archive.apache.org/dist/lucene/solr/7.2.1/solr-7.2.1.tgz # extract the service installation file tar xzf solr-7.2.1.tgz solr-7.2.1/bin/install_solr_service.sh --strip-components=2 # install Solr as a service sudo ./install_solr_service.sh solr-7.2.1.tgz
Check then the Solr status.
service 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.0-alpha2 at the time of writing, with Drupal 8.4.x
composer require drupal/search_api_solr:^2.0
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.
Then set the name of your collection under the Solr core field.
Other values can be left as is in this basic 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).
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.
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
7. Related modules
Here is a short list of modules that are part of the Search API ecosystem for Drupal 8.