Hi,

i want to write a nodereference module wich uses Solr instead of MySQL. Your code was a big help and now it works with single search terms.

How can i tell Solr to return the complete title name instead of single terms? In the function below

/**
 * Helper function that suggests ways to complete partial words.
 *
 * For example, if $keys = "learn", this might return suggestions like: 
 *    learn, learning, learner, learnability.
 * The suggested terms are returned in order of frequency (most frequent first).
 *
 */
function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_return = 5) {
  /**
   * Split $keys into two: 
   *  $first_part will contain all complete words (delimited by spaces). Can be empty.
   *  $last_part is the (assumed incomplete) last word. If this is empty, don't suggest.
   * Example:
   *  $keys = "learning dis" : $first_part = "learning", $last_part = "dis"
   */
  preg_match('/^(:?(.* |))([^ ]+)$/', $keys, $matches);
  $first_part = $matches[2];
  $last_part = $matches[3];
  if ($last_part == '') {
    return array();
  }
  // Ask Solr to return facets that begin with $last_part; these will be the suggestions.
  $params = array(
    'facet' => 'true',
    'facet.field' => array('spell'),
    'facet.prefix' => $last_part,
    'facet.limit' => $suggestions_to_return * 5,
    'facet.mincount' => 1,
    'start' => 0,
    'rows' => 0,
  );
  // Get array of themed suggestions.
  $result = apachesolr_autocomplete_suggest($first_part, $params, 'apachesolr_autocomplete_highlight', $keys, $suggestions_to_return);
  if ($result['suggestions']) {
    return $result['suggestions'];
  } else {
    return array();
  }
}

I've tried to set 'facet.field' to 'facet.field' => array('title')
but it doesn't work. Please help me, since I am not very familiar with Solr.

Comments

robertdouglass’s picture

One problem with this approach is that the original code in this module doesn't ask Solr to return a list of documents, but rather to return a list of facets (ie search terms). The terms are then presented as suggestions.

Your needs are different. You want Solr to return documents, not suggestions. This is much more analogous to what happens in apachesolr_search, so you could study the apachesolr_search_execute workflow to see how that is done.

janusman’s picture

Status: Active » Closed (fixed)

@robertDouglass is correct; this module's purpose is similar to Google's autocomplete--meaning it doesn't suggest items, but searches.