Last updated September 20, 2012. Created on October 27, 2008.
Edited by batigolix, dokumori, aufumy. Log in to edit this page.

Source: by Aeternum (Guide on how to make a custom search by content type)


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.


Drupal 6.6
ApacheSolr module 6.x-1.0-alpha3

Step 1.

Get and install 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
cp apachesolr_search.module my_search.module

Step 3.

Open up my_search.module and update the ID tag on line 2.
Open up 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();

      // 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
        return $results;

        // Set breadcrumb
        $apache_breadcrumb = $query->get_breadcrumb();
        $breadcrumb[0] = $apache_breadcrumb[0];
        $breadcrumb[1] = $originalKeys;
        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, 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\"/>


(thats all escaped for php)

Looking for support? Visit the forums, or join #drupal-support in IRC.


xnickmx’s picture

This looks interesting, but I'm not really sure exactly what it does. Before investing time in actually following all of these steps, I would like to play with a site that implements this. Are there any links I can turn to that actually have this implemented?

emackn’s picture

Shouldn't the exposed hooks be able to take care of this?

selvakumar’s picture

Apache solr gives very good result. But i want to show search result from multiple words. For example "want search multiple words". But solr search result gives minimum result set. I want to show either or the words result.

Tnahks in advance

Francewhoa’s picture

If you are not a coder the following module can achieve similar result