* * 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"); * * * More info: http://sphinxsearch.com/docs/current/sorting-modes.html */ $sphinx->SetSortMode(\Sphinx\SphinxClient::SPH_SORT_EXTENDED, '@weight DESC, municipio ASC'); // Choose the offset and limit for the query. $sphinx->SetLimits(0, 23); // We can restrict values on attributes. // Restrict attribute id_provincia to Barcelona and Girona (id_provincia=8, id_provincia=17): $sphinx->SetFilter('id_provincia', array(8, 17)); /** * We've two different ways to execute sphinx queries. * If you want to execute just one query, use: * * $results = $sphinx->query('Barcelona', 'municipios'); * * If you need to execute several queries at the same time, you can do it like these: * * $sphinx->AddQuery('Barcelona', 'municipios'); * $sphinx->AddQuery('Vigo', 'municipios'); * $results = $sphinx->RunQueries(); * * * Returned array will contain a different key for every query. *
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; }