How To Create Search Pages Using Views In Drupal 7

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”.

Fig 1.0

Finally, go to Reports -> “Status report” and click on “run cron manually” in the “Cron maintenance tasks” row.

Fig 1.3

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)”.

Fig 1.1

4. On the filter configuration page, check the “Expose this filter to visitors, to allow them to change it” checkbox and “Apply (all displays)”.

Fig 1.2

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.

Fig 1.4

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.

Fig 1.5

3. Select Yes from within the pop-up box and click on Apply.

Fig 1.6

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.

Fig 1.7

2. Now, if you go to the search page, the filter should be in the assigned block.

Fig 1.8

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.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

59 thoughts on “How To Create Search Pages Using Views In Drupal 7”

  1. Supriya Rajgopal

    Thanks Ivan..
    You saved me from hours of work!
    Sometimes, solutions are so simple but we tend to keep complicating things!

    Thanx again! 🙂 🙂

  2. 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 🙂

  3. 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.

    1. 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.

      1. No, but i got it unbelievably happening by comparing the sql when a single word is entered vs when 2 words where entered and found the difference as ‘Count > 1’ vs ‘Count > 2’ for a Having clause (Sorry i don’t have the energy now to get the exact text) and then inserting the following code in a module file in the hook_views_query_alter(&$view, &$query) fn after doing a dpm($view->query) analysis:
        if ($view->name == ‘newsbin’) {
        if (isset($view->query->having[1][‘conditions’][0][‘value’])) {
        $view->query->having[1][‘conditions’][0][‘value’][‘:search_index_keys’] = 1;
        }
        }

        I still have a couple or more problems though one of them being when trying to find the whole phrase by enclosing the phrase within double quotes, there gets to be a very large number of duplicates.

        Another being that if both the words are found then there gets to be a duplicate.

        Hellooo.

  4. 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.

      1. 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.

  5. 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

    1. Not sure how to do it. The “Search Terms” filter will only filter using whole terms. If you find a solution let me know.

  6. 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

    1. What do you mean by “exposing the field it always in a uncheck mode”.

      Are you talking about a checkbox? If so, which one?

  7. 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

  8. 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.

    1. 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.

      1. 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):

        // Search by views: redirect to the views results page on submitting the exposed block form
        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

  9. 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!

  10. 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.

      1. 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.

  11. 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

      1. 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!

  12. 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?

    1. 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.

  13. 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?

    1. 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.

    1. 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

      1. 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

  14. 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

      1. 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

  15. Puia chhakchhuak

    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

  16. 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

  17. 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

  18. 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

    1. 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

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.