コード例 #1
0
<?php

require_once __DIR__ . '/vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", true);
if (empty($_GET['q'])) {
    echo 'Choose a query string like: <a href="search_example.php.php?q=barcelona">barcelona</a>';
    die;
}
// Create Sphinx client instance
$sphinx = new \Sphinx\SphinxClient();
// Set where the sphinx server (searchd) is running
$sphinx->SetServer('localhost', "9312");
/**
 * We can select how sphinx will order the results.
 *
 * We'll use SPH_SORT_EXTENDED when we want SQL like ordering, selecting the fields to order by.
 * For example, the default Sphinx order returns the same order as:
 *
 *      $sphinx->SetSortMode(\Sphinx\SphinxClient::SPH_SORT_EXTENDED, '@weight DESC, municipio ASC');
 *
 *
 * ...where internal fields like 'weight' are prefixed by '@', while our own fields are not. We can order
 * by any field that we've specified as sql_field_string or sql_attr_string in the index configuration.
 *
 * We'll use SPH_SORT_EXPR when we need to use some values/fields to calculate our own ranking. You can
 * use Sphinx functions and expressions listed here http://sphinxsearch.com/docs/current/expressions.html
 * For example, improving users' weight by adding user_karma and pageviews:
 *
 *      $sphinx->SetSortMode(\Sphinx\SphinxClient::SPH_SORT_EXPR, "@weight + (user_karma + ln(pageviews)) * 0.1");
 *
コード例 #2
0
 protected function _sphinx_make_request($_data, &$error, &$warning)
 {
     $sph_config = $this->config->sphinx->toArray();
     // Create a connection
     $sphinx = new \Sphinx\SphinxClient();
     // Setup the query details
     $sphinx->SetServer($sph_config['host'], $sph_config['port']);
     $sphinx->SetMaxQueryTime($sph_config['max_query_time']);
     $sphinx->SetConnectTimeout($sph_config['connection_timeout']);
     $sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
     $sphinx->SetArrayResult(TRUE);
     $sphinx->SetFieldWeights($_data['field_weights']);
     $sphinx->SetMatchMode($_data['match_mode']);
     foreach ($_data['filters'] as $filter_name => $filter_vals) {
         if ($filter_name != 'date') {
             $sphinx->SetFilter($filter_name, $filter_vals);
         }
     }
     if (array_key_exists('date', $_data['filters'])) {
         $sphinx->SetFilterRange('date', $_data['filters']['date'][0], $_data['filters']['date'][1]);
     }
     switch ($_data['order_by']) {
         case 'date':
             $sort_mode = SPH_SORT_EXPR;
             if ($_data['order_direction'] == 'asc') {
                 $sort_expression = '-date';
             } else {
                 $sort_expression = 'date';
             }
             break;
         case 'popularity':
             $sort_mode = SPH_SORT_EXPR;
             if ($_data['order_direction'] == 'asc') {
                 $sort_expression = '-(views*0.5+numtracked*2+comments*4)';
             } else {
                 $sort_expression = 'views*0.5+numtracked*2+comments*4';
             }
             break;
         case 'relevancy':
         default:
             $sort_mode = SPH_SORT_EXPR;
             if ($_data['order_direction'] == 'asc') {
                 $sort_expression = '-(@weight + ln(views*0.5+numtracked*4+comments)*100)';
             } else {
                 $sort_expression = '@weight + ln(views*0.5+numtracked*4+comments)*100';
             }
             break;
     }
     $sphinx->SetSortMode($sort_mode, $sort_expression);
     $sphinx->SetLimits(intval(($_data['page'] - 1) * $_data['perpage']), intval($_data['perpage']), $sph_config['limits']);
     // Run the query
     $result = $sphinx->Query($_data['q'], $_data['use_index']);
     $error = $sphinx->GetLastError();
     $warning = $sphinx->GetLastWarning();
     return $result;
 }