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.”
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
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
Next, you will need to install Search API and Search API Elasticsearch modules and enable them on your Drupal 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
Filter criteria
- Fulltext search: Expose this filter, Required, Remember the last selection, Use as: search keys
Sort criteria
- Search: Relevance, descending (if you don’t have an order with fuzzysearch you will get a PDO exception)
Page settings
- Access: Permission: view published content
Advanced
- No results behaviour: Global: Text area “No results matched your search.”
Exposed form
- 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.
Conclusion
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.
Hamza Zia
Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.