Skip directly to content

Hacking Apache Solr query

rteijeiro's picture
on Wed, 12/12/2012 - 17:40

Surely you will not need to hack the Apache Solr queries when searching in your Drupal site but, if someday you need to do that, here you are the solution.

First of all you must have a Drupal 7 environment with an Apache Solr server configured. You should take a look at this blog post if you need details about the Solr installation and configuration with Drupal 7.

Just when your environment were ready you can create a simple module with a hook_search_api_solr_query_alter for Search API Sorl Search module or hook_apachesolr_query_alter for Apache Solr Search Integration module.

Here you are an example for Search API Solr Search module:

 * Implements hook_search_api_solr_query_alter().
function facetapi_untagged_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query) {
  // Get the Solr index name
  if ($query->getIndex()->machine_name == 'node_index') {
    // Add a custom term to filter the query
    $call_args['params']['fq'][0] = $call_args['params']['fq'][0] . ' OR (t_field_tags\:name:"untagged")';
And another example for the Apahce Solr Search Integration module:
function facetapi_untagged_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
  // Add a custom term to filter the query
  $query->params['fq'][0] = $query->params['fq'][0] . ' OR (t_field_tags\:name:"untagged")';

This examples aren't very good because you must use the addParam method or use the Solr query syntax to alter the query.

Now I am diving deeper into Solr queries so surely I will write more posts about this subject ;)


Mario P's picture

Hey, good one!

Right now I'm working exactly with this but my code did not work. Im trying to filter results through a given title:

$call_args['params']['fq'][0] = $call_args['params']['fq'][0] . ' AND tm_title:"Service 7"';

Could you help me with this ?

Post new comment