When you create a list using Views there’s a good chance you’ll add a filter to it. You could filter the list by content type, published status (if it’s published or unpublished), by author and more.
If you click on “Add” in the “Filter criteria” section you can see all the available filters.
The one filter I want to discuss today is “Search Keywords”. This filter lets you search by using keywords in the content. It uses the data that the core Search module has indexed so you’ll need to configure the Search module and have it index your Drupal site.
If things have been configured correctly, you’ll be able to filter content by keywords in the body field, or any other custom field.
There are few ways you can implement this type of functionality, i.e., filtering by keywords. We can use the filter as mentioned above which is easy to set up. Or we can use Search API which gives more flexibility but requires more set up and we’ll need to download a new module. We won’t be covering this module here.
In this tutorial, you’ll learn how to use the “Search Keywords” filter that comes with the core Search module.
To follow this tutorial, make sure you have Views and Search installed, which generally should already be installed.
And you can use the Devel module to create test content using the Devel Generate sub-module.
New to Views? Read our “Getting Started with Views in Drupal” tutorial.
Index Site Content
Before creating the view, ensure the Search module has been enabled, go to Configuration, “Search pages” and confirm that your site has been indexed. You should see “100% of the site has been indexed.”
If you need to index your site then run Cron, or go to /admin/config/system/cron run it manually.
Create Recent Articles View
Let’s start things by creating a new Views page.
Go to Structure, Views, click on “Add view”, and fill in the new view settings using the table below:
|Content of type Article sorted by newest first
|Create a page
|Unformatted list of fields
Add Search Keywords Filter
1. Back to Views, click on “Add” in the “Filter criteria” and add the “Search keywords” filter.
2. Check “Expose this filter to visitors, to allow them to change it” and click on Apply.
NOTE: Making the filter “exposed”, will allow users to change the filter.
3. Scroll down to the Preview section and see the “Search keywords” filter. Enter a keyword and click on Apply.
The view will change and filter content using the keywords.
Add Trimmed Body
Let’s build out the view and add a trimmed version of the body field.
1. On the Views edit page, click on Add in Fields search for Body and add the field.
2. Select Trimmed as the formatter and add 200 as the “Trimmed limit”.
3. If you look in the preview you’ll see the title and a trimmed version of the body field.
No Results Behavior
If you enter in a keyword but no results are return you simply get an empty area.
Views allows you to add text when nothing is returned.
1. Edit the view, click on Add in the “No results Behavior” and search for text area.
Select “Text area” and click on add.
2. Enter in the text you want returned in Content and click on Apply.
3. Test the view and search for a keyword you know does not exist and you should see the text appear.
Display Result Summary
A result summary can also be added to the header, footer or “no results behavior” section.
The summary returns a count of the total amount of items in the view. You can even display a pager count and how many items are currently displayed.
Let’s add the default result summary.
1. Edit the view and click on add in the header section.
2. Select “Result summary” and click on add.
3. Leave the default as
Displaying @start - @end of @total.
Take note of all the tokens which can be added below the text area.
4. Test the view and you should see a count below the page title.
If you need to create a basic search page, look at using Views and a filter.
But if you need something more complex and want to use a 3rd party search library, then look at using Search API.
What is Views?
Views is a query builder that ships with Drupal. It allows you to create index pages, tables and search result pages.
Should the core Search module be installed?
Yes, you’ll need to install the Search module to use the “Search Keywords” filter.