Using Drupal's Search API to Create a Sitewide Search Feature

The Drupal 10 Logo
The Cozy Nerd | April 8, 2024 Technology

Drupal’s Search API is a powerful tool that allows you to create custom searches on any entity known to Drupal, using any kind of search engine. Typically, you would use the same database source to create the search indexing, however for large sites, this is not recommended and you should use something like the Search API SOLR additional plugin to help make sure that there isn't any performance issue with your website. For now, I'm going to explain how to setup the Search API as well as create a Search page using the default local DB.

Step 1: Install the Search API Module & Associated Contrib Modules

First, you need to install the Search API module. It is recommended you use Composer to install the module into your codebase, however you can download it directly from the Drupal project page. Note that the Search API module comes bundled along with the Database Search plugin as well as the Search API Autocomplete, so you do not need to install them into your codebase separately. The other module that I would recommend installing is Search API Exclude Entity. This module will allow you to set certain entities that you do not want to have indexed and show up in search results. (Examples of these would be having custom entities that represent your websites 403 and 404 pages).

Once you've installed the modules into the codebase, you will need to enable them. Go to /admin/modules and check on Search API, Database Search, Search API Autocomplete, and if you installed it, the Search API Exclude Entity. Click the install button at the bottom of the page to fully enable these modules.

Step 2: Configure the Search API

Once you’ve installed the module, you can start configuring it. Go to /admin/config/search/search-api

Add a Server

Click on Add Server. Name it (for example, “Local”) or something specific to the indexes that will be using it. Check the box to enable it. Make sure the Backend is set to Database.

Add an Index

Next, set up a search index. This is where you specify what content types you want to be searchable. You can also specify certain fields to be searchable. Generally, when you are wanting to do a full site-search of content (think of how Google Search is; you have just one search field and you want to search everything that is relevant), you want to set those fields as Fulltext as their types. If you are wanting to do something more granular or advanced, I would suggest reading the official documentation.

Step 3: Configure Search API Processors

Search API processors allow you to pre-process data before it is indexed, or post-process search results before they are displayed You can enable and configure them according to your needs. In our simple example that we are setting up, you would want to enable Entity status, HTML filter, Ignore case, Tokenizer, and Search API Exclude Entity (if you installed this module). Once you checked on those processors, scroll down to the Processor settings and configure the settings to how you want your search to behave.

Step 4: Create Search Forms and Results Pages

You can create search forms and results pages with the Search API. This is where you specify what the search form will look like, and how the results should be displayed. To accomplish this, we want to create a new View. Go to /admin/structure/views/add and create a new view. In the View settings, you want to change the 'Show' to be your new Search index. Check on the 'Create a page' and set the page settings to how you want them. Then click the Save and edit button to create the view and begin customizing it how you want your search to work.

The Search API integrates with the Views module, allowing you to create and view searches on any index. All of an entity’s properties, as well as those of related entities, are available as fields, filters, and arguments. How you want your results to be displayed is up to you and how it best fits your project. Personally, I keep it as a Rendered entity, and have a specific view mode created and customized for my search results. That way if I need to change something, it's related to just that entity type, and it won't interfere with other entities or having to modify the view itself.

To get that classic search box, in the Filters section you would want to add a filter called "Search: Fulltext search". This filter will allow you to search fields indexed as Fulltext, grouped together in one search field. You would want to make sure that you expose this field to your users for use, and for better performance (and to not show a list of all indexed content), mark this field as required.

That’s it! You’ve now set up the Search API in Drupal. Remember, the Search API is highly flexible and extensible, so you can customize it to suit your needs. Note: Always ensure that only accessible items are indexed or results displayed by using appropriate filters. The Search API doesn’t provide any kind of access restrictions.

If you are looking for more guidance, here are a couple of tutorials that give a deeper look into Drupal's Search:

Happy searching!