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.
Before we can begin, download Search API, Search API Database Search, Entity API and Views.
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.
38 thoughts on “Intro To Search API (Part 1) – How To Create Search Pages”
I was hardly lookin for that. Thx again. You saved me a lot of headache. Because the Standard-Search-Function in Drupal is not good.
also want to say thanks… one of the few well documented and easy to understand tutorials on how to integrate search into drupal. for how much time drupal devs spend on maintaining modules, i think they’d save a lot of time for themselves if they provided some upfront instructions like this!
Nice tutorial really. Just wondering after the search. The searched words are not highlighted. Is there any setting to make it work?
Hmm, good question. To have the results highlighted, I think you’ll have to use Apache Solr and Search API Solr module. For Search API, the backend needs to support it.
Thank you for the tutorial but what i search is a solution to add a views block additional to the views page to integrate it with panels on my frontpage. Do you know a solution?
If I understand you correctly, you want to add a views block to a Panels page?
You should be able to simply add the block using Panels.
Thank you for your reply. I mean i have created successfully a search API Views page. Now i want add a search API Views “Block”. This created block i want to use to show it on front page and if i search with it then it should go over the created Views search page.
On your search API Views page, add an exposed filter. Then, under the Advanced section, set the “Exposed form in block:” to Yes.
This will allow you to place the exposed filters anywhere within Panels.
Thanks for taking the time to document this!
I’m new on Drupal 7 with Slor Search and this tutorial is very, very, very good.
Now I wil see part 2.
Thanks from Brazil.
Hello I’m glad you took your time to make such an detailed tutorial. I have one question: is there an ability to change the position of the Fulltext Search?
Yes, you can move the Fulltext Search text field. It’s an exposed filter; so what you can do is display the filter as a block and then place it where ever you want.
Here’s a tutorial on how to do it: http://s1l.org/views-3-exposed-filters-block
When I create an index View the Use Aggregation option is not available. Is there a way to aggregate (group) indexed fields that have the same value to eliminate duplicate (or multiple) records?
The “Use Aggregation” option is not available when you use a Search API index as a backend in Views.
I’m not sure how it can be done, I would ask in the Search API issue queue.
thank you very much for your tutorial. I found it pretty helpful for my project.
One question, do you know if there is a way to index both users and nodes in one search page?
Have you looked at the “Search API multi-index searches” module? (https://drupal.org/project/search_api_multi) Found it after a quick google search.
I have a content type with a field type of node reference displaying multiple values. When uploading content, the field works fine showing multiple values. But I want to use that indexed field as one of my filters for my search in my view, but for some reason it does not pick up that field as a node reference with multiple values anymore. I checked Search API index from the configuration and the field is there as a type node. Is there something that I might be missing.
NB: All my other taxonomy type indexed fields work 100% as filters.
Thanking you in advance.
When you say “node reference” are you referring to the References module? https://www.drupal.org/project/references
There should be an option in the views filter to allow multiple values. See if you have the same problem using Entity reference module.
No Ivan, in my content type, the field type node reference to get multiple values, just like I have field type term reference for multiple values, or field type date.
My term reference indexed fields works fine in my view as filters for my search, but the one that is of node reference does not work. I will email you screenshots of what am trying to explain.
Thanks for the screenshot. I don’t really know what the problem could be. In Drupal 7 I’ve always used entity reference instead of node reference. Try entity reference if possible. I’ve had great success with the module and Search API.
Thanks alot for providing such a detailed information. Could you please let me know the way to add custom menu items created through hook_menu in the search_api_db
Search API can’t index menus, not without custom code. More importantly, why do you want to index them? Could you not index the page that they point to?
Let’s consider a View/Custom Menu Page where we display a list of node teasers. Now, we need to index the View Page/Custom Menu Page and doesn’t need to index the node pages. Please do guide me to index such pages.
Search API out-of-the-box only indexes entities/nodes. But you do have a few options.
1. Look at using “Search by Page” module: https://www.drupal.org/project/search_by_page
This is a separate module to Search API. But I would test it out.
2. Implement the workaround in this link: http://drupal.stackexchange.com/a/8783/247
The question is about apachesolr, but the solution will work with Search API.
What options do I have if I want to ranks search results?
You can change the ranking in many ways. Your question is hard to answer because it’s very generic.
I would google “drupal search api ranking” or “drupal search api biased”
Is there any search field for the indexed view in which we can filter out the results based on the Letters ,instead of providing the entire word as a input to the Fulltext text search Field .
Not sure how to do this.
To index content in general, words need to be a minimum amount of characters.
I am using a search api module for search, however while i search, On the search result, it gives some records left empty and some record are displayed. when i keeps on clicking the search button those empty record become displayed. In view setting i have already tick the ” hide empty filed ” . Please i need your help to get this thing fix. I have attached herewith the image shows the problem. Is it because of Search API indexing issues ?
It could be many things.
Have you tried using search API page (https://www.drupal.org/project/search_api_page) instead of Views? You need to figure out if it’s the index or the displaying of results that’s the problem.
I want partial search as well
How can we achieve this thing with above configuration?
Also, I have one more exposed filter texonomy with full text,
I want to have OR condition between this two filter how can we achieve this?
In views it’s already “OR”, But somehow its always use “And” when there are full-text search and taxonomy available.
I’ve never set up partial search so I’m not sure how to do it.
Regarding, AND/OR, look in the Search API issue queue if there’s a bug about it.
How can I add a placeholder to the search form creating via view?
Look at this “Drupal Answers” question: https://drupal.stackexchange.com/a/31009/247
Hi, I have reproduced this tutorial, but I would like to know what kind of boolean operators I can use with this. Like AND, OR, and “An exact sentence”.
That depends on which search backend you use? Solr or something else. The search backend needs to support it.
Look at this comment. https://www.drupal.org/project/search_api_solr/issues/3033995#comment-12977395