You are here

function apachesolr_autocomplete_suggest in Apache Solr Autocomplete 7.2

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest()
  2. 7 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest()
2 calls to apachesolr_autocomplete_suggest()
apachesolr_autocomplete_suggest_additional_term in ./apachesolr_autocomplete.module
Helper function that suggests additional terms to search for.
apachesolr_autocomplete_suggest_word_completion in ./apachesolr_autocomplete.module
Helper function that suggests ways to complete partial words.

File

./apachesolr_autocomplete.module, line 387
Alters search forms to suggest terms using Apache Solr using AJAX.

Code

function apachesolr_autocomplete_suggest(DrupalApacheSolrServiceInterface $solr, $term, $params, $suggestions_to_return = 5) {
  $matches = array();
  $suggestions = array();
  $term = trim($term);

  // We need the keys array to make sure we don't suggest words that are already
  // in the search terms.
  $keys_array = explode(' ', $term);
  $keys_array = array_filter($keys_array);
  $query = apachesolr_drupal_query("apachesolr", $params, '', '', $solr);

  // Add spellcheck parameters.
  apachesolr_search_add_spellcheck_params($query);

  // Add fields to search in (qf)
  apachesolr_search_add_boost_params($query);

  // Allow modules to modify the query object.
  drupal_alter('apachesolr_autocomplete_query', $query);
  if (!$query) {
    return array();
  }

  // Query Solr
  $response = $query
    ->search($term);

  // Loop through requested facet.fields and get returned suggestions.
  foreach ($params['facet.field'] as $field) {

    #echo "Returned facets:\n"; // TODO: DEBUG

    #print_r((array)$response->facet_counts->facet_fields->{$field}); // TODO: DEBUG
    foreach ($response->facet_counts->facet_fields->{$field} as $terms => $count) {
      $terms = preg_replace('/[_-]+/', ' ', $terms);
      foreach (explode(' ', $terms) as $term) {

        // Trim the $term.
        $term = trim(preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', $term));
        if ($term) {
          if (isset($matches[$term])) {
            $matches[$term] += $count;
          }
          else {
            $matches[$term] = $count;
          }
        }
      }
    }
  }
  if (sizeof($matches) > 0) {

    // Eliminate suggestions that are stopwords or are already in the query.
    $matches_clone = $matches;
    $stopwords = apachesolr_autocomplete_get_stopwords($solr);
    foreach ($matches_clone as $term => $count) {
      if (strlen($term) > 3 && !in_array($term, $stopwords) && !array_search($term, $keys_array)) {

        // Longer strings get higher ratings.

        #$matches_clone[$term] += strlen($term);
      }
      else {
        unset($matches_clone[$term]);
        unset($matches[$term]);
      }
    }

    // Don't suggest terms that are too frequent (in >90% of results).
    $max_occurrence = $response->response->numFound * 0.9;
    foreach ($matches_clone as $match => $count) {
      if ($count > $max_occurrence) {
        unset($matches_clone[$match]);
      }
    }

    // The $count in this array is actually a score. We want the highest ones first.
    arsort($matches_clone);

    // Shorten the array to the right ones.
    $matches_clone = array_slice($matches_clone, 0, $suggestions_to_return, TRUE);

    // Add current search as suggestion if results > 0
    if ($response->response->numFound > 0 && $term != '') {

      // Add * to array element key to force into a string, else PHP will
      // renumber keys that look like numbers on the returned array.
      $suggestions['*' . $term] = array(
        'value' => $term,
        'count' => $response->response->numFound,
      );
    }

    // Build suggestions using returned facets
    foreach ($matches_clone as $match => $count) {
      if ($term != $match) {
        $suggestion = trim($match);

        // On cases where there are more than 3 keywords, omit displaying
        //  the count because of the mm settings in solrconfig.xml
        if (substr_count($suggestion, ' ') >= 2) {
          $count = FALSE;
        }
        if ($suggestion != '') {

          // Add * to array element key to force into a string, else PHP will
          // renumber keys that look like numbers on the returned array.
          $suggestions['*' . $suggestion] = array(
            'value' => $suggestion,
            'count' => $count,
          );
        }
      }
    }
  }
  return array(
    'suggestions' => $suggestions,
    'response' => &$response,
  );
}