How to use Search API Solr Search in Drupal 8
Apache Solr is a very popular open source search platform, based on the Java Lucene Library. Solr is very stable, scalable and reliable and provides a wide set of core search functions. Solr creates an index of the available documents and then you can query Solr to return the most relevant ones for your search.
For Drupal users, it is possible to integrate your site with Solr. The Search API Solr Search module (yes, that name is a mouthful!) provides a Solr backend for the Drupal Search API module.
This tutorial will deal with the integration between Drupal and the Solr platform. Before you begin, you will need to have installed Apache Solr on your server.
Step #1. Checking the Status of the Solr Server
- Open the Terminal application in your system and type:
service solr status
You’ll see an output with the label Active: active (exited). That means Solr is working properly. By default, Solr works on port 8983. This way, you can take a look at Solr’s user interface by typing localhost:8983 (or whatever your host is called).
Step #2. Installing the Drupal Module
To install this module, you have to use Composer, otherwise there will be unresolved dependencies and it won’t work.
- Open your Terminal application and type:
composer require drupal/search_api_solr
Step #3. Configuring Solr
In order to work with the Solr platform, you have to create a collection. A collection is basically a logical index that’s associated with a config set.
- Open your Terminal window and type:
sudo su - solr -c "/opt/solr/bin/solr create -c firstcollection -n data_driven_schema_configs"
This command will vary, depending on where Solr is installed. In my case it’s in the /opt folder of my Ubuntu system. However, the collection will be created in another directory.
- Take a look at Solr’s dashboard and select “Core Admin”.
- Check that the collection has been created and its location.
- Become the root user.
- Locate yourself in the collections folder of Solr, which is located at the /var directory in an Ubuntu based system.
- Rename the default conf file.
- Copy the configuration file called 7.x inside the installed Solr Search API module and rename it to conf.
cp -R /var/www/your_site/docroot/modules/contrib/search_api_solr/solr-conf/7.x conf
- Change owner, group and permissions for this new folder.
- Restart Solr.
- Exit session as root.
Step #4. Enabling the Search API Solr Module
- Click Extend > Uninstall and look for the Drupal default Search module. This module has to be uninstalled to avoid performance issues. Select the module and click “Uninstall”.
- Confirm the Uninstall process by clicking the blue “Uninstall” button once again.
- Click the List tab and enable the “Solr Search Defaults” module, this module provides the initial default configuration.
- Click “Install”. The Solr Search module will be enabled automatically.
Step #5. Configuring the Search API
- Go to Configuration > Search API.
- The Server and Index configurations were provided by the Solr Search Defaults module.
- Click the “Edit” button to configure the Server.
The only thing you have to do is to change the name of the Solr core to the name of the core you created previously.
- Scroll down and find the Solr core label.
- Change the name of the core to firstcollection.
- Click “Save”.
Server and index are ready to use.
- Go to Extend > Uninstall.
- Look for the Solr Search Defaults module, and uninstall it. For performance reasons, the provided configuration is already stored.
Step #6. Testing the Search API Solr Module
By default, the Search API Solr module provides a view with a search box where you can type the text you’re looking for. The search page is located at:
- Type in a keyword and hit the “Search” button.
- If you see a list of results (the word has to be in the content of course) you’ve successfully configured Search API Solr. Congratulations!
This tutorial showed the basic configuration of the Search API Solr module to integrate Drupal 8 with Apache Solr.
I recommend exploring the available configurations and reading Solr’s documentationn. This will give you a better idea of all options available when configuring the search functionality of your sites.
Thanks for reading, please leave your comments below.
I needed to update the solr install directory listing in solrcore.properties as described here:
It should be /opt/solr for Ubuntu, although it could be different on yours.
These days the solr xml file requirements seem to change a lot with search_api_solr.
I found that just getting rid of your default solr instance and running it using search_api_solr’s docker-compose.yml, or instead running solr for drupal in it’s own instance using docker on it’s own port is the easiest way to deal with search_api_solr setup:
on your search host:
edit the docker-compose.yml file within cloned directory to your liking (ports etc)
git clone https://github.com/drupalprojects/search_api_solr.git solr (or whatever)
docker-compose up -d
set up search_api_solr on your drupal server
install search_api_solr_defaults module into drupal
change ip address for server in search_api solr server config
You now have a fully configured nearly ready to index setup. It helps to follow the recipe here:
to get really nice search results that you can rank effectively
There seems to be no Solr 8 config folder in the Drupal Search API Solr distribution. How should I create one?
I configured as documented.
The search form is coming with the url /solr-search/content.
Items are also indexed from admin.
But the view result is empty.
how do i enable the view results?
Thanks in advance
thanks for sharing the valueable information.
You are very welcome!