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“.
The core Search module in Drupal 7 is great for simple search pages however, the configuration options are fairly limited. If you want to change the look and feel of the search results, you could use the Display Suite Search sub-module that ships with Display Suite.
You can go one step further and create a custom search page using just Views. All you need to do is create a page display and expose the Search: Search Terms filter, and you’re done. But the filter still relies on the index data that the Search module creates.
If you want flexibility and control over your search pages, then you should take a serious look at the Search API module. The biggest benefit of using the module is that it supports a number of search backends like Apache Solr, Xapian and MongoDB or you can store the index directly into a database.
In this tutorial, we’ll create a custom search page using Views and Search API for the results. For simplicity we’ll use the Search API Database Search module as the backend.
If you use Drush, run the following command:
$ drush dl search_api search_api_db entity views ctools
Once you have downloaded the modules, enable the following:
- Search API
- Search views
- Database search
- Entity API
- Views UI
Create a Search API Server
The first step required for our search page, is to create a Search API server. This will let Search API know how and where the index will be stored. For this tutorial, we’ll use the Search API Database Search module to store our index in the database. If we were to use Apache Solr, then we would use the Search API Solr module.
1. Go to Configuration -> Search API and click on the “Add server” link.
2. Enter “Database server” into the “Server name” field and select “Database service” from the “Service class” drop-down list.
3. Save the form by clicking on “Save settings”.
Create a Search API Index
In the last section, we created a Search API server. Now, we need to create a Search API index and define which fields should be stored in the index.
1. Go to Configuration -> Search API and click on the “Add index” link.
2. Enter “Article index” into the “Index name” and select Node from the “Item type” drop-down list.
3. Further down the page, select “Database server” from the Server drop-down list and click on “Create index”.
Define Index Fields
On the Fields page, we have to define which fields will be indexed. When you’re building a large index, it takes a bit of trial and error to get it right.
For now, we’ll just index the title, body and tags field.
1. Check the Title checkbox and click on “Save changes”.
2. Click on the “Add related fields” field-set and select “The main body text” and click on “Add fields”.
3. After adding the related field, check the “The main body text Â» Text” checkbox and click on “Save changes”.
4. Again, click on the “Add related fields” field-set and this time select Tags.
5. After adding the related tags field, check the “Tags Â» Name” and “Tags” field and click on “Save changes”.
The Workflow page allows you to run specific data alterations and processors on the index data. But for this tutorial, we’ll keep it simple and leave this page empty.
Now that we have setup our Search API index, the next step is to index some data.
Click on the Status tab and click on “Index now”.
Create Search Page
At this point, we have created a Search API server, index and we indexed some content. The final piece of the puzzle, is to create the actual search page.
You can create search pages by simply using Views or by using the Search API pages module. But in this tutorial we’ll use Views because this gives you the most flexibility.
1. Go to Structure -> Views (admin/structure/views) and click on “Add new view”.
2. Select “Article index” from the Show drop-down.
If you can’t see “Article index”, then make sure you enabled the “Search views” module.
Fill out the rest of the page with the values defined in Table 1.0.
Table 1-0. Create a new view
|View name||Search page|
|Create a page||Checked|
|Page title||Search page|
|Display format||Unformatted list (default)|
Once the form has been filled out, click on “Continue & edit”.
3. Click on Add within the “Filter criteria” area and select “Search: Fulltext search” and click on “Apply (all displays)”.
4. Check the “Expose this filter to visitors, to allow them to change it” checkbox and click on “Apply (all displays)”.
5. Click on Fields within the Format area and select “Rendered entity” and click on “Apply (all displays)”.
6. Select Teaser from the View mode drop-down list and click on “Apply (all displays)”.
7. Save the view and go to
/search-page and test the page.
As you have seen, Search API gives you a lot of flexibility. However, you have to spend time setting up the server, index and views display. In part two, we’ll look at faceted search using the Facet API.
If you have any questions, please leave a comment.