My previous three posts focused on maximizing the speed of your Drupal 7 website by utilizing Redis, Varnish, and Memcache. These cache software greatly improves the page load speeds for the visitors.
In this article, I’m going to take you in a different and equally useful direction. This is one area where large Drupal 7 websites could improve significantly. I am referring to important question, how to install Elasticsearch on Drupal 7. I will demonstrate how to use Elasticsearch for adding a powerful full website search engine to your Drupal 7 website.
First, let’s see what Wikipedia has to say about Elasticsearch: “Elasticsearch is a search engine based on Lucene. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is the most popular enterprise search engine followed by Apache Solr, also based on Lucene.”
Figuring out how to setup Elasticsearch for Drupal 7 could be a challenge. To help you out, I have created this guide to help you navigate the problem. Let’s take a look at the steps involved in the process:
- Install Elasticsearch on Cloudways server
- Install and enable the required modules on your Site
- Add search server
- Add search Index
- Create view
Before you start, make sure you have a Cloudways account and have already launched a server.
Install Elasticsearch on Cloudways
The first thing that you will need to do is install Elasticsearch on your Cloudways server. To do this, go to your server’s dashboard and navigate to the Manage Services tab in the left sidebar. From here, go to the Packages tab. Click the edit icon beside Elasticsearch and select the version of Elasticsearch you want from the drop-down menu. Wait a few minutes for the installation to finish.
That’s it for the Elasticsearch installation on the server.
Install and Enable Required Modules on the Website
Add Search Server
Now the main portion of the solution begins!
Before going ahead, make sure you’ve got Composer setup on your website because it is the recommended (and the easiest) method for managing dependencies on your website.
Navigate to Admin > Configuration > Search and Metadata > Search API. Here, click Add Server at the top, which will bring you to the following screen. Name your server, and select Elasticsearch from the drop-down menu for Service class. Finally, create the server.
Note: If you get any errors about missing files, simply run composer update in the CLI.
Add Search Index
Merely adding the server is not enough, You also need an index of the contents of your website. This step depends upon the server you created in the previous step.
Go back to the Search API configuration and click Add Index at the top. Name your index, select search index from the drop-down menu in Item type. Click the checkbox beside enabled. Select the server you created in the previous step from the drop-down menu and then check Index Items Immediately. Finally, create the Index.
Note that you will need to index the items on your site when you make changes in the Search API configuration. To do that, simply navigate to the index you had created.
Create the View
Okay, so by now you have successfully setup Elasticsearch on your Drupal website. However, you still need to make it visible on the website and accessible to the users.
For this, create a view and place the block in the designated region. Go to Admin > Structure > Views > Create View. Here are the settings for the View (available in the documentation for Search API module):
- View name: Search
- Show: [the name of the Search index]
- Create a page [tick]
- Page title: Search
- Path: search
- Display format: Unformatted list of Rendered entity
- Items to display: 10, use pager
Continue & edit (the new View)
Format: Show: Rendered entity | Settings
- View mode: Search results
- Fulltext search: Expose this filter, Required, Remember the last selection, Use as: search keys
- Search: Relevance, descending (if you don’t have an order with fuzzysearch you will get a PDO exception)
- Access: Permission: view published content
- No results behaviour: Global: Text area “No results matched your search.”
- Exposed form in block: Yes (This option will only show up on Page displays.)
- Exposed form style settings: Submit button text: Search
Save the View.
Now, go to Admin > Structure > Blocks and place the block with the name of the view you created in your desired region.
That’s it! now you know how to install Elasticsearch on Drupal 7 website. Even though the process is a bit complex and lengthy, it’s very much worth it since it provides your users the ability to search what they are looking for quickly without having to browse through the entire website.
If you need help with the process or would like to contribute to the discussion, do leave a comment below.
Create interactive Drupal websites easily on the Cloud.
Host your website on optimized Drupal hosting servers.