Change apache solr search keyword operator OR/AND

Last updated on
30 April 2025

By default, the apachesolr Drupal module is set to do an AND search when there are 2 keywords, and then applies some fuzziness with DisMax for 3 or more keywords. This is set via the mm parameter in solrconfig.xml which default to 2<-35%.

You can inhibit this behavior by setting the mm parameter in the query before it is sent to solr:

function HOOK_apachesolr_query_alter($query) {
  // Force an OR keyword search.
  $query->params['mm'] = '1';
}

Using Solr 3:

function HOOK_apachesolr_query_alter($query) {
  // Force an AND keyword search.
  $query->replaceParam('mm', '100%');
}

Drupal 7

In Drupal 7, the mm parameter is defaulted to 1 (see apachesolr_search_basic_params()). This means that the default behaviour is essentially an OR. You can use the same replaceParam method above to change this to an AND.

Note that the hook you will implement this code in will depend on the version of the apachesolr you are running:
hook_apachesolr_prepare_query(), hook_apachesolr_modify_query(), or hook_apachesolr_query_alter() for the most recent versions of apachesolr.

Help improve this page

Page status: Not set

You can: