function handle($args)
 {
     /*
      * Make sure we have a search term.
      */
     if (!isset($args['term']) || empty($args['term'])) {
         json_error('Search term not provided.');
         die;
     }
     /*
      * Clean up the search term.
      */
     $term = filter_var($args['term'], FILTER_SANITIZE_STRING);
     /*
      * Append an asterix to the search term, so that Solr can suggest autocomplete terms.
      */
     $term .= '*';
     /*
      * Intialize Solarium.
      */
     $client = new Solarium_Client($GLOBALS['solr_config']);
     /*
      * Set up our query.
      */
     $query = $client->createSuggester();
     $query->setHandler('suggest');
     $query->setQuery($term);
     $query->setOnlyMorePopular(TRUE);
     $query->setCount(5);
     $query->setCollate(TRUE);
     /*
      * Execute the query.
      */
     $search_results = $client->suggester($query);
     /*
      * If there are no results.
      */
     if (count($search_results) == 0) {
         $response->terms = FALSE;
     } else {
         $response->terms = array();
         foreach ($search_results as $term => $term_result) {
             $i = 0;
             foreach ($term_result as $suggestion) {
                 $response->terms[] = array('id' => $i, 'term' => $suggestion);
                 $i++;
             }
         }
     }
     $this->render($response, 'OK');
 }
예제 #2
0
<?php

require 'init.php';
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a suggester query instance
$query = $client->createSuggester();
$query->setQuery('ap ip v');
//multiple terms
$query->setDictionary('suggest');
$query->setOnlyMorePopular(true);
$query->setCount(10);
$query->setCollate(true);
// this executes the query and returns the result
$resultset = $client->suggester($query);
echo '<b>Query:</b> ' . $query->getQuery() . '<hr/>';
// display results for each term
foreach ($resultset as $term => $termResult) {
    echo '<h3>' . $term . '</h3>';
    echo 'NumFound: ' . $termResult->getNumFound() . '<br/>';
    echo 'StartOffset: ' . $termResult->getStartOffset() . '<br/>';
    echo 'EndOffset: ' . $termResult->getEndOffset() . '<br/>';
    echo 'Suggestions:<br/>';
    foreach ($termResult as $result) {
        echo '- ' . $result . '<br/>';
    }
    echo '<hr/>';
}
// display collation
echo 'Collation: ' . $resultset->getCollation();