Free Videos about Mastering Views

How To: Add focused Drupal search to your site

Tagged:  
Play Video
Movie link (right-click to download)
Problem with this video? Contact me

When it comes to search, Drupal seems to do OK by itself. However, there are a number of supporting modules which will make your Drupal's default search even better. These include Porter-Stemmer (english only), Search 404, Search by Page, Similar By Terms and many others.

If you're seekign to help an advanced user out, then modules like Search config can help with that. But what about the user who won't dare go into the hidden area of 'Advanced Search'? This is where the power is - right?

It sure is. This is where you tell Drupal what content types and categories you want to limit the search to. This is where a user, simply looking for a job on your site, which lists information about jobs, news, blogs and other items, can focus their results.

So, why don't you stop expecting the user to figure this out, and just make it happen for them! That's what this video is all about. Using the default Drupal search box and forcing it to focus on specific content types or categories. You control what Drupal searches for and you control where it shows up!

Search Related Modules

The Apache Solr project is what is used on Drupal.org. You know, where you get the cool faceted results from a search request.

Of course, if you haven't heard about it already, the Acquia Search service make it quite easy to take advantage of the Apache Solr coolness.

Here's the code.

<?php
// All of the following code goes within template.php

/*
* Using one of the preprocess hooks in template.php, you create a
* variable which calls drupal_get_form on the function which creates
* the search form
*/
function THEME-NAME-HERE_preprocess_node(&$vars, $hook) {
 
$vars['focused_search'] = drupal_get_form('THEME-NAME-HERE_focused_search_form');
}

/*
* This function will create a search form which adds a validation
* handler which you use to append any custom search criteria
* Note to self: Context module could be used here?
*/
function THEME-NAME-HERE_focused_search_form(&$form_state, $keys = '') {
 
// Render the form to search a focused content type
 
if( module_exists('search')) {
   
$form = search_form($form_state, '/'. drupal_get_path_alias($_GET['q'])); // Use existing search form code
   
$form['#validate'] = array('search_form_validate', 'THEME-NAME-HERE_focused_search_validate');
   
$form['#submit'] = array('search_form_submit');
    return
$form;
  }
}

/*
* The validation handler added within the search form is where you make
* any adjustments to the search handled by Drupal. You can use any
* critera you wish, including the path or other variables available
* within Drupal.
*/
function THEME-NAME-HERE_focused_search_validate($form, &$form_state) {
 
// Using an array to determine which content type to search based on path from search form
 
$content_type = array_search($form['#action'], array(
                 
'vendor_item' => '/vendors',
                 
'job_item'    => '/jobs/sites',
                    )
                  );
 
$content_type = empty($content_type) ? '' : $content_type;
 
// Initialize using any existing basic search keywords.
  // taken from node.module node_search_validate() function
 
$keys = $form_state['values']['processed_keys'];
 
$keys = search_query_insert($keys, 'type', $content_type); // additional seach criteria added here
 
form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state);
}
?>

Great video - thanks (what software/resolution do you use to record it - such a difference from fuzzy youtube ones!). I was keen to look more closely at the focused search code but cant see it attached to this article.

Regards

Joe

I've got an article here about the video process.

I've also attached the code to the article. I forgot to do that.

Nice videos you have here. I didn't see any mention of the Finder Module which is really great...it's in RC2 but in my test it worked nice. You can create custom search forms for any content type, on any page etc via the Drupal interface

http://drupal.org/project/finder

Congrats , very good post.

Hi,
I have a problem with your solution. I get a warning:
warning: preg_match() expects parameter 2 to be string, array given in www/modules/search/search.module on line 672.

I believe it is something related to $keys_processed - the array is empty.

Any ideas?
Thanks in advance!

Getting the same error, don't really get what's wrong. Did you figure this out?

Also, I saw that Matt wrote a comment to him self in the code that it forms might be triggered by context instead of path. Did you get that to work?

Thanks in advance,
 Marcus

Same error here...

Anyone who get this to work?

I too am getting
warning: preg_match() expects parameter 2 to be string, array given in ...\search.module on line 672.

If I figure it out, I will post back here again..

same error here ...

has anybody any suggestions?

So, uh, did this work for anyone? This preg_match error has got me completely confused. Amazing how such a simple simple SIMPLE task like "automatically refine search to certain content types" takes 10 laps around the block and STILL doesn't work.

If I could get to the SQL command, it would be as simple as adding WHERE (TYPE="page" OR TYPE="story") done and done.

Would you mind responding to this post at drupal.org? The question is "How do I add multiple search boxes to a page?" and seems you have the solution. Sorry, if your tutorial contains the solution but I have not gotten it to work yet.

Thanks for your post.

How to add image to the result of a search

I Can you give me a hint if it is also possible to add image witha link to the node, to the result of a search. The image in question, has the field type of emvideo, since I'm using Embedded Media Field module configuration for visitors to post videos on my site. I would like visitors to visit and search for videos with the keyword and a small image with a link that goes to the node.
any help welcome, I'm a newy! .
If you can recomend somebody to doit for me I be willing to pay for the work. thanks for you great videos!!

someone told me to follow this code, can you suggest somebody to do this work for me,
I ´m wiiling to pay for the work
http://api.drupal.org/api/function/template_preprocess_search_result/6

Hi,

thanks for this awesome video!

I followed step by step, and successfully created the focused search form; but after I input the key and hit "search", it always redirect me to some "Object not found" page!

I place the search form at "localhost/drupal/basic_profile", then the submit button would take me to "localhost/basic_profile"; any general advice would be appreciated... thanks

follow up:

Does this support drupal sites with clean-urls enabled?

seems the &_GET['q'] only works with non clean-urls sites?

On a side note, please do take a look at the Search Lucene API module: http://drupal.org/project/luceneapi.

It has the same features as the Apache Solr Search module, but is much easier to install. I came across this module as it was still in beta (but working almost perfectly), the maintainer has been very active on the issue queue, and the first stable version was recently released.

Marcus

I just started "playing" with the Lucene Search Module. In a single adjective, awesome.
I'm building a site on a shared hosting account so I can't use Apache Soir, nor the Facted Search Module (can't grant the user privilidges required). Lucene however works an absolute treat and allows me to create a fantastic easy to understand search capability.

Excellent tutorial, helped me a bunch implementing proper search on my world cup drupal site!

Well, thank you for the codes... I appreciate it very much!!!

One thing Search Lucene does is let you weight Node Title compared to other factors, which is awesome. However, my server won't support that module.

Do you, or anyone else, know a way to weight node title heavily over node body withOUT that module?

thanks for the great video and tips!