Want to learn more about search in Drupal 7? Then check out our online video course called “Build Powerful Search Pages with Views and Search API in Drupal 7“.
Search API has been my go-to module for building search pages for the last two years. Even if the client doesn’t ask for anything fancy, I still download and install Search API, use Database Search for the index and Views for the page.
If you start with Search API from the beginning, then it’s easier to customise later on. The core Search module, on the other hand, is easy to setup but hard to modify.
Recently, I had to create a search page that highlighted the keywords in the results. If you search using a particular keyword, then the word is highlighted.
In this tutorial, you’ll learn how to use Search API and Views to create a search page where the keyword is highlighted in the results.
I’ll assume that you’ve already setup Search API and configured your index. If you’ve never used this module before, then read “Intro To Search API (Part 1) – How To Create Search Pages” before continuing.
Step 1: Configure Search API Index
First we need to turn on keyword highlighting by enabling the Highlighting processor on an index. Edit your search index and go to the Filters tab.
Scroll down to the Processors field-set and check the Highlighting checkbox.
The Highlighting processor settings can be configured below, but the default options are fine, leave it as is. Scroll to the bottom and click on “Save configuration”.
Now that we’ve modified the index settings, you’ll need to re-index the content again. You can re-index it by clicking on the View tab and then click Index now.
At this point, the index is ready. Now let’s configure a custom view to display the highlighted keywords.
Step 2: Configure Views Display
Now go edit your custom view which is used to display the search index.
If you need to learn how to create a search page using Views and Search API, then read the “Create Search Page” section in “Intro To Search API (Part 1) – How To Create Search Pages“.
Click on Add in Fields and search for the “Excerpt” field.
Add this “Excerpt” field to your view. The highlighted keywords are displayed through this field.
Don’t forget to save the view by clicking on Save in the top right corner.
Now go ahead and test it all out. You should see highlighted keywords displayed from the “Excerpt” field.
By simply configuring an index processor and adding the “Excerpt” field to a view, highlighted keywords can easily be displayed in no time.