Filtering Apache Solr search results based on dynamic fields

Posted Apr 27, 2010 // 3 comments
Brad :

You may often have content in Drupal that should not appear for certain user roles. There are many ways to accomplish this in the CMS, but restricting them from search results is often overlooked. If you are using Apache Solr for your search engine, the apachesolr_nodeaccess module integrates with Drupal’s node access system to restrict results. However, what if your access rules are more fine-grained than just content types?

What if users are restricted from viewing certain content based on taxonomy terms or vocabularies? ( something accomplished in the CMS with modules such as Taxonomy Access Control Lite )

You could return all results and use logic in your template to determine if a result should be shown to a user, but it’s much more efficient to use the apachesolr module’s hooks to determine what search results are returned.

We’ll focus on two hooks provided by the apachesolr module: hook_apachesolr_update_index and hook_apachesolr_modify_query.

hook_apachesolr_update_index is invoked whenever a node is being indexed by Apache Solr. You can use this hook to modify attributes of the node or create your own custom fields before the node gets indexed. The following function adds two custom fields.

1
2
3
4
5
6
function mymodule_apachesolr_update_index(&$document, $node) {
  .. code to determine values ..
 
  $document->addField('is_example_1', $example1_value);
  $document->addField('is_example_2', $example2_value);
}

The ‘is_’ prefix on the field name is important. Solr has a list of pre-defined prefixes located in the schema.xml file in your apachesolr module directory. Look for the section marked “Dynamic field definitions”, which lists all possible field name prefixes and their associated data types. If you create a custom field that does not include one of these prefixes, your field will not be added to the index.

To make sure your field is being included, look at the status page ( /admin/reports/apachesolr ) to see all current fields in the index.

hook_apachesolr_modify_query is invoked before the search query is sent to Solr. Here we can filter out nodes based on the fields we created. In the following example, we add filters to the query based on user permissions.

1
2
3
4
5
6
7
8
function mymodule_apachesolr_modify_query(&$query, &$params, $caller) {
  if(!user_access("access example1 content")) {
    $query->add_filter("is_example_1",1,true);
  }
  if(!user_access("access example2 content")) {
    $query->add_filter("is_example_2",1,true);
  }  
} 

The first parameter to the add_filter function is the field name, the second is the value to search on, and the third is a boolean value. If this value is true, then matching items are removed from the result set. You can add filters on any of the fields that are in the index such as node type or status, not just ones that you have created.

Either of these hooks can be used on their own, but together they allow for very powerful control over the search results presented to your users.

About Brad

Web Developer Brad Blake brings a wealth of expertise to our team and our clients whenever he creates software tools and websites on the LAMP platform. For more than five years, he has been using PHP to build cutting-edge technologies that ...

more >

Read Brad 's Blog

Comments

by Amit Goyal (not verified) on Thu, 04/29/2010 - 00:53

very nice and clear example..

very nice and clear example..

by Gaurav (not verified) on Tue, 05/11/2010 - 08:55

Issue with hook 'apachesolr_modify_query'

I am facing an issue while modifying query parameter using apachesolr_modify_query hook. I have different type of node type say blog, page, story. Now I want to search content of blog and story type.

function mysearch_apachesolr_modify_query(&$query, &$params, $caller) { $query->add_filter("type", 'blog'); $query->add_filter("type", 'story'); }

Now I think this will act as a 'AND' condition but I want it as 'OR' means node type can be blog or story. How can I do that?

by Marcelo Tosco (not verified) on Wed, 05/19/2010 - 03:19

Very useful tip.

I wrote a post based on your article and my personal experience. I hope you do not mind

http://www.e-capy.com/setear-y-recuperar-fields-de-apache-solr

Thanks!.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.