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“.
A while ago I wrote two tutorials on using the Search API module. In the first tutorial, we created a custom search page using just Views and a Search API index. In the second, we looked at faceted search using the Facet API module.
Search API can be tough to setup especially for new users to Drupal. For example, you need to understand the difference between a Search API Server and Index, and then you need to spend time configuring everything.
In this tutorial, I’ll show you how to create a search page using just Views and Drupal’s core Search module.
Getting Started
Before we can begin, download and enable Views. Also make sure you have enabled the core Search module.
If you use Drush, run the following command:
$ drush dl ctools views $ drush en views views_ui
Search Module
First, make sure you have the Search module enabled. If you installed Drupal using the standard installation profile, then you’ll have the module already installed.
Second, make sure you create some content and index that content. Go to Configuration -> “Search settings” and click on “Re-index site”.
Finally, go to Reports -> “Status report” and click on “run cron manually” in the “Cron maintenance tasks” row.
When you click on “Re-index site” button, this doesn’t index any content it just tells Drupal that the content needs to be re-indexed. The actual index process happens when you run cron.
Create Search View Page
Now that we have the core Search module installed and working, we’ll spend some time creating the actual search page using Views.
1. Go to Structure -> Views (admin/structure/views) and click on “Add new view”.
2. Fill out the “Add new view” form, using the values defined in Table 1.0 and click on “Continue & edit”.
Table 1-0. Create a new view
Option | Value |
---|---|
View name | Search page |
Machine name | search_page |
Show | Content of type All sorted by unsorted |
Create a page | Checked |
Page title | Search page |
Path | search-page |
Display format | Unformatted list (default) |
3. Click on the Add link within the “Filter criteria” and find the “Search: Search Terms” filter and click on “Apply (all displays)”.
4. On the filter configuration page, check the “Expose this filter to visitors, to allow them to change it” checkbox and “Apply (all displays)”.
All we have actually done is added the “Search: Search Terms” filter and exposed it so that users can enter in search keywords. The text field will appear above the views page.
At this point, we have a working views page. Save the view, and go to /search-page
.
If you enter in some keywords into the “Search Terms” text field, the page should return some results.
Display Filter In Block
By default, views places the “Search Terms” text field (exposed filter) above the content area of the page. Views offers you the ability to expose the filter as a block. This is useful if you want to move the exposed filter somewhere else instead of just above the content area.
1. Go to the edit page for the “Search page” view.
2. In the Advanced section, click on the No link next to the “Exposed form in block” label.
3. Select Yes from within the pop-up box and click on Apply.
4. Once you have configured the view click on Save.
Assign Block To Region
You may notice that the filter is not displaying above the content area, that is because it’s now exposed as a block. The last bit of work we need to do is add the exposed filter block to a region within the theme.
1. Go to Structure -> Blocks and assign the “Exposed form: search_page-page” block a specific region.
2. Now, if you go to the search page, the filter should be in the assigned block.
The last bit of work required is to style the search results. But as you have seen, it’s pretty easy to create a custom search page using just Views and the Search module.
If you have any questions, please leave a comment.
Thanks Ivan..
You saved me from hours of work!
Sometimes, solutions are so simple but we tend to keep complicating things!
Thanx again! 🙂 🙂
Thanks and cross-referencing to your other articles is helpful too.
On the display content as block you could add that this option is under advanced. Small info, big help.
Thanks for the tip. I have updated the text.
Thank you for the tutorial. Nice and simple. This also offers a way to display search results only in the current language on multilingual sites. I used Display Suite Search for that in the case, I’m working on right now, but still 🙂
Thanks that’s good to know.
I have been looking for something like that for hours, you make it so clear… thanks a lot!!!
Hi Ivan,
I landed up at your site after somehow already implementing the above but found that this works for only single words. If 2 or more consecutive words are entered such as say – leading provider – then there always gets to be no result.
The views filter does not limit you to single words. There must be an issue with the search index on your site. Try reindexing the content on your site.
I was looking for a way to keep some taxonomy terms out of my search index. But that seems hard. Second best option is to keep them out of the visible search results. Using a view would be perfect.
I do have the same problem as Joe however. If I search for two words I get an empty result. I have one node that had a name wich includes one letter, such as “place b”. If i search for those two words I get tens of results, all the same node.
I did a reindex of the search index, and i made a fresh view with filter search terms and exposed it. I actually tried this solution earlier and abandoned it because of the 2 keyword problems. Reading the above it seems this is not normal. But I cannot find the cause of this behaviour.
If you guys are having problems that can be replicated, then create an issue in the Views issue queue.
Also, found this in the Views issue queue about the search term problem:
https://drupal.org/node/1948510
Hope this helps.
Great find. The patch in that url fixed the problem for me. This opens a whole lot of possibilities to customize the search page. In my case easy ways to exclude taxonomy terms and node-types from search result (although I keep looking for a way to keep them out of the index in the first place).
A thing I tried is to add a contextual filter search terms with argument search/node/%. This could be used to overwrite the core search results. However, core uses %20 between all keywords while Views seems to work with / between the different words. Looking for a solution to this. Or I just use the exposed block.
For some reason, setting ´On empty input´ to – `Show None´ still shows results…
What am I missing?
Follow the instructions in this comment:
https://drupal.org/node/358546#comment-7245598
I can´t deny there is some logic here, thanks!
How we can set up search by term to search by only five character of term, for example “wor” would search for any word that begin with “wor”, like “Regular expression” in other exposed filter options?
Thank you
Not sure how to do it. The “Search Terms” filter will only filter using whole terms. If you find a solution let me know.
Thx, for repluy, if I find solution I will post it here.
Very useful 🙂
Hi,
I followed your procedure, it is good but when i configure the search fields ie,exposing the field it always in a uncheck mode
What do you mean by “exposing the field it always in a uncheck mode”.
Are you talking about a checkbox? If so, which one?
Yes,It is the checkbox. My theme is tb_neris i think it is not supporting the view search and it is displaying for content and users and after admin logouts there is search form
Switch to the default Bartik theme and see if you have the same problems.
Hi Ivan,
thanks for this (next) cool tutorial.
Do I understand it right – this exposed form is only usable if I am on the views’ search page itself.
If I place the block on the frontpage for example, it does not function, as all it does is add the filter identifier and the search phrase to the url, without actually redirecting to the search page: http://www.example.com/?keys=blabla and not http://www.example.com/search-page?keys=blabla
Is there any workaround for this?
Thanks again.
It should redirect to the page. Is “/search-page” an actual views page display? Or is it a Panels page? Or something else.
Test this out on a fresh Drupal 7 site and see if you get the same problem.
Hi Ivan,
you’re right, it’s panels that makes the trouble. On a fresh drupal site everything works fine…
Seems somehow that the exposed block doesn’t know where to redirect if this block is being rendered by a panel. I’ve tried various different approaches (like displaying the results in a views pane which is rendered by a page manager custom page) but with no luck.
Meanwhile I’ve found a solution by putting something like this in a custom module (assuming it’s the initial approach with a views page and not a page manager page and the block rendered by a panel):
function my_module_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
$form[‘#action’] = “/” . $form_state[‘view’]->display_handler->options[‘path’];
}
If I find more time I will try further to achieve this without having to code and will give some feedback. If someone has some experience with that, please give a small hint. 🙂
Thanks
Thanks for the update. 🙂
Is it possible to run a search with no terms(blank search box)?
I’d like the search to return All results of the chosen content types if no keywords are entered into the search and have tried a few different ways to do this with no avail.
This article a great help!
Unless there is some hidden settings, I don’t think you can do it using the “Search terms” filter. A keyword is required.
Look at using Search API instead. (https://drupal.org/project/search_api)
http://webwash.net/tutorials/intro-search-api-part-1-how-create-search-pages
I cannot thank you enough for this post. Over a year ago, I gave up after trying everything (or so I thought) to get a view to have a search box that searched all fields. I stumbled on this post today and I believe it has solved my need in under 5 minutes. If there is a way to give it more visibility, I would do so. Heartfelt thanks.
My pleasure, thanks. 🙂
The search operator appears to be OR when the core search is AND. Is there some way to use Search: Search Terms with an AND by default? I saw https://drupal.org/node/356998 for Drupal 6 but adding AND between the terms does not AND them in my Drupal 7 install.
I’m not really sure how to do this in Drupal 7. The best place to ask would be in the Views issue queue.
just what i was looking for!, thanks alot!
Hi there,
I’ve set up my search exposed filter and view, however it seems the pager does not work. If I set the pager to display 30 items, it will show the first 30 and there is no pager at the bottom. Have you experienced this? Currently I have it set to “View All” but the results list becomes very long.
Any help is greatly appreciated.
Thanks,
Erin
Have you done any template overriding or modified Views. Try creating a basic view and see if a pager appears.
I tried creating a basic view and the pager worked. So then I tried cloning my existing view and the pager worked again! No idea what happened there but I got it working. Thanks for the tip!
Hi!
How can I theme the search result of the exposed filter form? The exposed filter form is displayed in a block, and it has something else on the page. If I filter and search, there will be only the result. I also want to show a menu on the left part of the page. What can I do?
If the exposed filter is being displayed as a block, then all you’ll have to do is display it on the search page. Just assign it to a region via the Blocks page in the admin section.
Hi Ivan Zugec!
I have tried above tutorial and I can see that it works perfectly. But what I need is to keep the default drupal search as is (I have themed search–results.tpl.php) but on search page I want to display just titles of all articles when no search has been made.
Any clue on how to do it?
Hi Syed,
I may be wrong, but I don’t know if it’s possible using core Search. However, you can do this using Search API and Views.
Hi, is there a way to show your searched term somewhere above the results?
Hi Alex,
If you don’t want the “Search terms” block in appear in the sidebar then leave “Exposed form in block” to No. This will leave the exposed block above the search results.
Ivan
Hi
i think we misunderstood, i was speaking of actual searched term.
For example when user search for “Black Suit” i want to display something like: “Showing 25 results for Black Suit” as page title.
Thanks
The [FacetAPI](https://www.drupal.org/project/facetapi) module comes with a sub-module called “Current Search Blocks”. It displays the current search via a block. I’ve always used it with Search API, not sure if it works with standard search.
I mention the sub-module in the tutorial below:
http://www.webwash.net/drupal/tutorials/intro-search-api-part-2-faceted-search
Hi, thanks for this walk through. Can you please let me know how to go about making this search for only users of the system.
Thanks
Are you referring to authenticated users?
If so, just configure the permission on the view, https://www.drupal.org/node/1490434
Not for users in terms of who is permitted to use the search view but to make the search for contents= users; like searching through a members’ directory.
Thanks for your response
Not sure how to do this using core Search and Views.
But you can do it using Search API. https://www.drupal.org/project/search_api
When i directly enter the views page url without a form submission, i get this:
Notice: Undefined index: keys in __lambda_func() (line 2 of /var/www/html/sitefolder/sites/all/modules/views_php/plugins/views/views_php_handler_area.inc(39) : runtime-created function).
I have looked around, but none of the solutions I have found deal with search terms. Does any one know a solution.
Thanks
Hi Puia,
I’ve never seen this error. Have you configured some multilingual functionality. Could be related to that: https://www.drupal.org/node/1223184
Cheers,
Ivan
Hi,
I have tried above tutorial and its working perfectly but the problem is I’m getting the duplicate search result. Can you please help me out.
Thank you in advance
Hi Shruti,
Never had this problem. Try setting the distinct option in Views to remove duplicates.
Cheers,
Ivan
Hi Ivan,
This helps me a lot!
Do you know how to add spell checker functionality within views?
When the user input wrong words, i want to give them help like “did you mean ….?”
Thank you in advance,
Satria
Hi Satria,
You’ll need to use Search API (https://www.drupal.org/project/search_api) with Apache Solr (https://www.drupal.org/project/search_api_solr)
Core search can’t do “did you mean…”.
Cheers,
Ivan
Hi Ivan,
thank you for the tutorial.
I have a view with Search : Search Terms as exposed filter as you describe in the article.
The views shows the news of my site. All works find, but the search don’t look for in the summary (body summary) of the news.
So, If I search for a word that is only in the summary, this node doesn’t appear.
Any idea ?
Thank you
Hi Anna,
You can customise which fields get indexed by tweaking the “Search index” view mode. found this on Drupal Answers: https://drupal.stackexchange.com/a/69704/247
But, when it comes to the Body field you can choose Default or Summary. I can’t think of a way to index the full body field and it’s summary at the same time.
You could look at using Search API (https://www.drupal.org/project/search_api) but this will take a bit of time to configure.
Here are two tutorials on the module:
– https://www.webwash.net/intro-to-search-api-part-1-how-to-create-search-pages/
– https://www.webwash.net/intro-to-search-api-part-2-faceted-search/
Cheers,
Ivan