Drupal Search Tutorial

How to manage the search function in Drupal

Drupal comes prepackaged with a built-in search engine. It is provided by the core Search module. By default, if you installed Drupal with the standard profile, the module should be enabled. The default permissions are configured in such a way that visitors/anonymous users cannot use the search function. Only registered users can search the site. Of course, you can change that by editing the permissions. The Search module provides some settings that can be configured.

Search Settings

To change and manage the search options, log in to the admin panel of your Drupal, click on the Configuration tab in the top navigation bar and on the page that opens click on Search settings:

Accessing the Search Settings

On the page that opens are the search settings. The first two sections on that page are Indexing Status and Indexing Throttle:

Search Settings-Indexing Status and Throttle

The indexing status shows you how much of the site has been indexed and how many items are still to be indexed. One content item (node), for example, is an item to be indexed. If a hundred percent of the site is indexed, a search on the frontend will encompass all the nodes. When you create new nodes, or you edit or delete existing nodes and comments, the created/edited nodes have to be indexed in order for the indexing status of your site to be a hundred percent complete. Indexing is regularly performed by a cron job that comes preconfigured with Drupal and is performed thanks to the cron.php file that's in the root directory of your Drupal hosting account. By default, this cron job is performed automatically once every three hours, and it's responsible for checking for updates and for indexing items that have been placed for indexing. For a small site this interval is more than sufficient; you can even change it to once a day.

If you want to, you can also run the cron job manually. You can do that from a few places in the admin panel. One option is to click on the Configuration tab in the toolbar, then on the page that opens click on Cron (it's in the System section), and on the following page click on the button Run cron:

Drupal Admin Panel-Run Cron Job Manually

On the same page you can also change the interval at which the cron job is automatically run. Just use the drop-down menu that's provided and click on the Save configuration button.

To run a cron job you can also click on the Reports tab in the top navigation bar. On the page that opens click on Status report, and on the following page you'll see a run cron manually button, as well as an URL link to the cron.php file on your Drupal account. Just click on one of the two.

Back on the Search settings page, if you click on the Re-index site button that's in the Indexing Status section, all content items on your site will be placed for re-indexing:

Search Settings-Reindexing Site

The items will be indexed the next time the cron job is performed. From the Indexing Throttle section you can set the maximum number of items that can be indexed per cron run. Just use the drop-down menu that's provided. By default, it's set to a hundred.

The next section on the page is Indexing Settings:

Search Settings-Indexing Settings

In that section you can set the minimum length of words that are to be indexed. By default, words that consist of fewer than three characters are not indexed. So, for example, prepositions such as in, at, on, etc. are not indexed. There's also a checkbox labeled Simple CJK handling which is marked by default. This enables a tokenizer that applies only to the Chinese, Japanese and Korean languages.

Further down the page the next section is Active Search Modules:

Search Settings-Active Search Modules

By default, a search can be executed for content within nodes (content items) and for the usernames and email addresses of registered users. So the checkboxes labeled Node and User are marked. If, for example, you don't want people to be able to search for users, just unmark the checkbox User. Under these checkboxes there are a couple of buttons. These determine whether nodes or users will be searched when a search is performed without specifying which of the two areas you want to search through. By default, if not specified explicitly, nodes are searched. We'll come to how searches are performed on the frontend a bit further down in this tutorial.

The last section on the Search settings page is labeled Content Ranking:

Search Settings-Content Ranking

There are several factors that are taken into account when a search is executed: Number of comments, Keyword relevance, Content is sticky at top of lists, Content is promoted to the front page, Recently posted. The weight of these factors determines the order of the items in the search results. By default, all factors have the same relevance; the weight of each is set to zero. If you want a factor to be more important than the others, just increase its weight by using its weight drop-down menu in the table listing the factors. For example, if you want nodes that have more comments in them to be displayed at the top of the search results list, increase the weight of the Number of comments factor. However, from practice you may find that changing the weight of the factors might not always produce the expected results.

After you make any changes on the Search settings page don't forget to click on the Save configuration button at the bottom.

Using the Search Function on the Frontend

As we already mentioned, the search function can be used by registered users. By default, anonymous users don't have the permission to use it. Once a user registers a search block will appear on the frontend. This search block is displayed by the Search Form block that comes prepackaged with Drupal; it's enabled and configured by default. With the Bartik theme that comes with Drupal the block is displayed in the left column:

Frontend-Search Block

Of course, you can edit the block, you can move it to other areas of your site, etc. For more information on blocks check out the tutorial on managing Drupal blocks.

You can always use directly the search page on your frontend to perform searches. If, for example, your Drupal site is accessible at yourdomain.com, you can open the search page by using the URL address yourdomain.com/search. You can include a link to the search page in any of the menus on your site.

When a search is executed, Drupal will search, by default, all the nodes for exact matches of the searched word (case insensitive). If more than one word is typed in the search box, Drupal will search for all nodes that contain all of the specified words. For example, if you search for the words new articles, Drupal will list only the nodes that contain both words, not those that contain just new or just articles. If you want the search to list all nodes that contain at least one of the words, separate the words with OR (capitalized). Using the above example, if you want to find all nodes that contain either the word new or the word articles (or both), type new OR articles in the search box and execute the search. In case you want to search for an exact match of a whole phrase, enclose the phrase in quotation marks (e.g. "new articles and tutorials").

You can also use the minus symbol (hyphen) to exclude words from the search. For example, if you type articles -tutorials, the search results will list all nodes that contain the word articles with the exception of those that contain also the word tutorials.

By default, the search settings are configured to search the nodes. On the page with the search results you can use the Users tab to search for particular users. When searching for content you can also use the advanced search options; just click on the Advanced search button to see them:

Frontend-Advanced Search

The advanced search function can be used only by users who have the permission to do so. The advanced search allows you to select specific content types, so that only nodes belonging to the selected content types will be searched. By using the respective fields you can search for any of the listed words, you can search for a specific phrase, and/or you can list words that if contained in the node, the node shouldn't be listed on the results page.

Search Permissions

To edit the permissions of the Search module, click on the People tab in the top navigation bar of your Drupal admin panel. On the page that opens click on the Permissions sub-tab that's at the top right corner of the page, and on the following page scroll down to the Search section:

Editing Search Permissions

You can give users belonging to a particular role the permission to use only the simple search, or you can also enable the permission for the advanced search. If you make any changes don't forget to click on the Save permissions button.