Home  >  Article  >  Backend Development  >  Example of php reading sphinx

Example of php reading sphinx

WBOY
WBOYOriginal
2016-07-25 09:03:061003browse
  1. //Check whether sphinx can connect, you cannot retry twice, if it can connect, mysql protocol is not used, for reference only
  2. function checkSphinxNoMysql() {
  3. $flag = true;
  4. $retries = 0;
  5. while ( $flag && $retries < 2 ) {
  6. $s = new SphinxClient ();
  7. $s->setServer ( $_ENV ['db_host'], $_ENV ['current_sphinx_port'] );
  8. if (! $s->open ()) {
  9. //Here is to change the configuration file if the connection cannot be made. Write it according to the needs of the project
  10. //global $configDefault;
  11. //updateConfig ( $configDefault );
  12. $retries ++;
  13. } else {
  14. $flag = false;
  15. break;
  16. }
  17. }
  18. return $s;
  19. if ($retries >= 2) {
  20. //sendemail or not
  21. return false;
  22. }
  23. }
  24. $order_column = 'id DESC,time DESC';//Ordering rules
  25. //$s = checkSphinx ();
  26. $s = new SphinxClient ();
  27. $s->setServer ( 'sphinx_host' , 'sphinx_port');
  28. //The above two lines of code can also be replaced by $s = checkSphinx ();
  29. $indexname = "page_keyword";//Index name
  30. $s->setMatchMode ( SPH_MATCH_PHRASE );
  31. $ s->SetSortMode ( SPH_SORT_EXTENDED, $order_column );
  32. $s->setMaxQueryTime ( 100000 );
  33. $s->setLimits ( 0, $limit_total, $limit_total );
  34. $keyword_sphinx = iconv ( "gbk", "utf-8", $keyword );
  35. $result = $s->query ( $keyword_sphinx, $indexname );
  36. $s->close ();
  37. if ($result ['total'] > 0) {
  38. var_dump($result ['matches']);
  39. //Read accordingly according to the printed results
  40. }
  41. ?>
Copy code

2. Use mysql binary network The code of the protocol:

  1. //Check whether sphinx can connect. You cannot retry twice. If it can connect, use mysql14 protocol
  2. protected function checkSphinx() {
  3. $flag = true;
  4. $retries = 0 ;
  5. while ( $flag && $retries < 2 ) {
  6. $conn = mysql_connect ( "{$_ENV ['db_host']}:{$_ENV ['current_sphinx_port']}" );
  7. if (! $conn) {
  8. //Here is to change the configuration file if the connection cannot be made. Write it according to the needs of the project
  9. //global $configDefault;
  10. //updateConfig ( $configDefault );
  11. $retries ++;
  12. } else {
  13. $flag = false;
  14. break;
  15. }
  16. }
  17. if ($retries >= 2) {
  18. die ( "Please contact with administrator." );
  19. }
  20. return $conn;
  21. }
  22. $order_column = 'id DESC ,time DESC';//Collation rules
  23. $conn = mysql_connect ("sphinx_host:sphinx_port");
  24. //The above code can also be replaced by $conn = checkSphinx ();
  25. if (! $conn) {
  26. return - 1;//The connection cannot be returned and the status is returned
  27. }
  28. $keyword_sphinx = iconv ("gbk", "utf-8", $keyword);
  29. //keyword is the index name
  30. $sql = "select * from keyword where match( '{$keyword_sphinx}') order by {$order_column} limit {$limit_total} option max_matches={$limit_total}";
  31. $result = @mysql_query ( $sql, $conn );
  32. $i = 0;
  33. while ( ($row = mysql_fetch_array ( $result )) !== false ) {
  34. var_dump($row);
  35. //Read accordingly based on the printed results
  36. }
  37. $totals = $this->getTotalFound ( $conn);//Get the total number of records
  38. ?>
Copy code

How to get the total number of records Reference: How to get the number of results in sphinxql? Detailed description of show meta?



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn