You are here

function apachesolr_proximity_parse_query in Apache Solr Term Proximity 7

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

Parses the search query into terms and phrases.

Parameters

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

Return value

array An array of parsed terms, which are associative arrays containing:

  • keyword: The parsed term or phrase.
  • type: Either "term" or "phrase".
3 calls to apachesolr_proximity_parse_query()
ApachesolrProximityUnitTestCase::testParseMultipleTermQuery in ./apachesolr_proximity.test
Tests parsing a simple query with two terms.
ApachesolrProximityUnitTestCase::testParsePhraseQuery in ./apachesolr_proximity.test
Tests parsing a phrase from a search query.
apachesolr_proximity_apachesolr_query_alter in ./apachesolr_proximity.apachesolr.inc
Implements hook_apachesolr_query_alter().

File

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

Code

function apachesolr_proximity_parse_query($q) {

  // Return an empty array if there are no matches.
  if (!($matches = apachesolr_proximity_match_keywords($q))) {
    return array();
  }

  // Iterate over matches and determine if they are phrases or terms.
  $keywords = array();
  foreach ($matches as $match) {
    if (0 === strpos($match, '"')) {

      // Phrases start with quotes. Remove the quotes so that we can get the
      // first term of the phrase in apachesolr_proximity_build_query().
      $keywords[] = array(
        'keyword' => trim($match, '"'),
        'type' => 'phrase',
      );
    }
    elseif (!preg_match('/^AND( NOT)?|NOT|OR$/', $match)) {

      // Don't capture operators as terms.
      $keywords[] = array(
        'keyword' => $match,
        'type' => 'term',
      );
    }
    else {

      // @todo Figure out how to handle queries with mixed operators.
      // @see http://drupal.org/node/1848734
      return array();
    }
  }
  return $keywords;
}