You are here

function apachesolr_proximity_build_query in Apache Solr Term Proximity 7

Same name and namespace in other branches
  1. 6.3 apachesolr_proximity.apachesolr.inc \apachesolr_proximity_build_query()

Builds the proximity query to be set as the "q" parameter.

Parameters

string $q: The "q" param passed to Solr, which is the raw search query entered by the end user through the search form.

string $operator: The operator, either "OR" or "AND".

array $parsed: The parsed keywords as returned by apachesolr_proximity_parse_query().

float $boost: The boost factor for the term proximity.

Return value

string The formatted proximity query.

1 call to apachesolr_proximity_build_query()
apachesolr_proximity_apachesolr_query_alter in ./apachesolr_proximity.apachesolr.inc
Implements hook_apachesolr_query_alter().

File

./apachesolr_proximity.apachesolr.inc, line 99
Implementation of Apache Solr Search Integration hooks.

Code

function apachesolr_proximity_build_query($q, $operator, array $parsed, $boost) {
  $has_phrase = FALSE;

  // Gather all terms used in the proximity query.
  $proximity_terms = array();
  foreach ($parsed as $keyword) {
    if ('term' == $keyword['type']) {
      $proximity_terms[] = $keyword['keyword'];
    }
    elseif ($matches = apachesolr_proximity_match_keywords($q)) {

      // As per the relevancy tuning doc, we only need the first term.
      $proximity_terms[] = $matches[0];
      $has_phrase = TRUE;
    }
  }

  // Rebuild the "q" parameter according to the relevancy cookbook.
  // @see http://wiki.apache.org/solr/SolrRelevancyCookbook#Term_Proximity
  $proximity_clause = '"' . join($proximity_terms, ' ') . '"~1000000';
  if ('OR' == $operator) {
    $proximity_query = $q . ' ' . $proximity_clause . '^' . $boost;
  }
  elseif (!$has_phrase) {
    $proximity_query = $proximity_clause;
  }
  else {

    // @todo Figure out how to work with phrases for AND operator. Not too
    // critical since the OR operator is recommended to use in conjunction
    // with this module and it is the Apache Solr Search Integration module's
    // default setting.
    // @see http://drupal.org/node/1848722
  }
  return $proximity_query;
}