Add Keyword Highlighting using Search API in Drupal 7

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.

Fig 1.6

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.

Fig 1.0

Scroll down to the Processors field-set and check the Highlighting checkbox.

Fig 1.1

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”.

Fig 1.2

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.

Fig 1.3

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.

Fig 1.4

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.

Fig 1.5

Summary

By simply configuring an index processor and adding the “Excerpt” field to a view, highlighted keywords can easily be displayed in no time.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

14 thoughts on “Add Keyword Highlighting using Search API in Drupal 7”

  1. Hello,

    Great article, thank you! But I have one problem – when I add “Excerpt” field to the view, it shows me an empty results page. I mean, data of “Excerpt” field is empty. Maybe, you know something about this?

    Thank you.

      1. No, I didn’t get any errors. If I add title filed to view – everything works good. But if I add excerpt field – empty results with pager only.

  2. jesus lafuente

    Hi, thanks for this excellent tutorial!

    One question: Can I edit or modify how the excerpt is created? I have a complex search on many fields, and the excerpt makes no sense (the fields are ordered almos randomly). I can only hide fields from the excerpt, but for example, the title is the last field of the autogenerated excerpt…

    Thank you very much!

    1. I’ve never tried to change the look-and-feel of the excerpt. In the Filters tab, try excluding non-important fields.

  3. Hi, As Vadim, the field Excerpt is empty.
    If I read the article about creating a view page result, it’s said to create a Rendered entity, not a view with field.
    It may miss something in our settings …
    Could you help ?

    1. Don’t select “Rendered entity”, instead of set the format to “fields” and add the “Excerpt” field.

      > Add this “Excerpt” field to your view. The highlighted keywords are displayed through this field.

      I tested this just now using the latest Search API and it works. Let me know how you go.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.