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.
Summary
By simply configuring an index processor and adding the “Excerpt” field to a view, highlighted keywords can easily be displayed in no time.
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.
Not really sure what the problem could be. Are you getting PHP errors or index errors?
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.
Sorry can’t help, you’ll need to debug it further.
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!
I’ve never tried to change the look-and-feel of the excerpt. In the Filters tab, try excluding non-important fields.
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 ?
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.
How to add placeholder for that search box.
Hi,
You’ll have to modify the form and add a placeholder attribute.
Look at the link below as an example. It may not be the exact solution, but it’ll help you out.
http://drupal.stackexchange.com/a/95867/247
As far as I know the Excerpt does NOT contain any data when using database search. I guess you used Solr or so….
Hi Tom,
Are you getting any PHP errors?
Hi Ivan,
I want to display an excerpt from the pdf file. Can you please help me with this.
thanks in advance
Hi Vedang,
To index PDFs look at a few of the following modules:
Here’s a link to a tutorial:
https://cheppers.com/blog/apache-solr-and-drupal-part-ii-how-to-set-up-drupal-and-solr-to-search-in-attachments
Cheers,
Ivan