Hey guys,

I have been trying to make a custom search that will search only by a certain content type and only by titles. Yesterday, I finally managed it. I've searched on this issue a lot and have not found any one place that explains how to do this - only a lot of people asking how to do it. So, since I got it working, I thought I would write a quick guide on how to acheive a custom search. I'm no Drupal expert, so there will probably be things I haven't done correctly - feel free to point them out in any comments. I just hope that I spare at least one person the frustration I went through.

Goals:
Make a search that will:
- Allow searching by a single content type, or field
- Not make other site searches unusable. That is, only affect the one search and allow room for further searches, if needed.

Assumptions:
Drupal 6.6
ApacheSolr module 6.x-1.0-alpha3

Step 1.

Get and install ApacheSolr - http://drupal.org/project/apachesolr. Ensure that it works.

Step 2.

Go to your Drupal Apache Solr directory
cd sites/all/modules/apachesolr/
Copy the apachesolr_search.module and apachesolr_search.info
cp apachesolr_search.module my_search.module
cp apachesolr_search.info my_search.info

Step 3.

Open up my_search.module and update the ID tag on line 2.
Open up my_search.info and update the name and description.

Step 4.

Open up my_search.module.
update line 13 from: function apachesolr_update_index() {
to: function my_search_update_index() {
Note: The above line is to avoid a php duplicate function name error. However, the function doesn't seem to be a hook, so I'm not sure if renaming it like this is the correct way to do it. However, on my tests, updating the index even with the apachesolr_search module disabled still works. If anyone could clarify, that would be useful.

update line 20 from: function apachesolr_search_search($op = 'search', $keys = NULL) {
to: function my_search_search($op = 'search', $keys = NULL) {

Step 5.

Administer --> Modules --> Enable my_search

Step 6.

Reindex & verify that my_search works (There will be another "Search" link next to the normal apachesolr link)

Step 7.

Here we create a custom query. In my case, it was: q=((title:(one two three)) AND › mytype), however this can be easily modified. Just Google "Solr queries" to find out what the syntax is. To find out what query you need and whether it works, you can simply type it into your standard apachesolr search and look at the results. You can also add queries directly into the address bar. Eg /apachesolr/this_is_my_query. Ignore the ugly breadcrumb and search box for custom queries for the moment, we fix that up in a moment.

Change the code starting from line 43 from:

// This is the object that knows about the query coming from the user.
      $query =& apachesolr_drupal_query($keys);
      $results = array();

to:

      // This is the object that knows about the query coming from the user.
      $originalKeys = $keys;
      $keys = "q=((title:(".$keys.")) AND type:mytype)";
      $query =& apachesolr_drupal_query($keys);
      $results = array();

(Obviously customise the above depending on your query)

Step 8.

Test your new search and verify it works as intended. Your breadcrumb will be ugly - ignore for the moment.

Step 9.

Now we fix the breadcrumb. I wasn't particularly fussed about the breadcrumb, so I was very lazy in fixing it. However, you can look at what I did just to get an example.

Go to around line 112 and change the code from:

        // Set breadcrumb
        drupal_set_breadcrumb($query->get_breadcrumb());
        return $results;

to

        // Set breadcrumb
        $apache_breadcrumb = $query->get_breadcrumb();
        $breadcrumb[0] = $apache_breadcrumb[0];
        $breadcrumb[1] = $originalKeys;
        drupal_set_breadcrumb($breadcrumb);
        return $results;

Step 10.

Verify your search works without errors and displays the breacrumb and search box correctly (that is, the query is hidden from the user).

Step 11. (optional)

One thing that really bothered me was the standard drupal user and content search hanging around. Also, the default apache search.

So, go to http://drupal.org/project/coresearches, download and install coresearches and leave the standard searches disabled. Then disable the apachesolr_search module. Add some new nodes of your content type, verify that a reindex works and that your search picks it up.

Now you will have only your custom search left.

Step 12.

Now, for an easy way to allow the user to search. What I did was just make a new search block, using the following code:

<form method=\"post\" action=\"/search/my_search\" id=\"search\">
						
							<input type=\"text\" id=\"keyword\" name=\"keys\" value=\"\" />
							<input type=\"submit\" name=\"op\" title=\"Search\" value=\"Search\" class=\"form-submit\"/>
						
</form>

(thats all escaped for php)

I hope someone finds this usefull.

Cheers,

Frederik Grunta

Comments

Michelle’s picture

I don't use that module so can't speak to the quality of the tutorial but it looks like you've put time into writing it up nicely. Rather than letting it get lost in the forums, why not make a handbook page for it?

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

memoo’s picture

Hi Frederik,

As Michelle said; good writing.

Q: Are you willing to help me with an install on a webserver ( so not a localhost)?

Thanks,

Hans

nils.destoop’s picture

A better way, is the following.
function referencepages_search($op = 'search', $keys = NULL) {

switch ($op) {
case 'name':
return t('Reference pages');

case 'search':

$keys = 'type:topic '.$keys;
return apachesolr_search_search('search', $keys);

break;
}
}

This way, you don't have to update your module the whole time if apachesolr updates.
My path: /search/referencepages/Madonna
My breadcrumb result:
Home › Search › Reference topic › Madonna ›

janusman’s picture

Apache Solr 6.x-1.x-DEV switched over to using the dismax query handler by default, so the sample searches which include AND operators don't work (I *think*).

doq’s picture

    case 'search':
      $_GET['filters'] = 'type:topic';
      return apachesolr_search_search('search', $keys);
bchavez’s picture

Hi to all

I have a problem, and I don't found the solution. I have installed and working apache solr integration and apache solr multisite search. Works fine. I use the search block and autocomplete and works ok, but the problem is that when I make a search, respond with a page with no results until I click in the Multi-sites tab then give me the results, and this not function for me... I need that allways only search via multisite, and don't give me any of the tabs: content, multi-site search, help, users, etc. Only search via multisite and that's all...

How can I make this?

Regards
Bruno Chávez