sphinxapi.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. //
  3. // $Id$
  4. //
  5. //
  6. // Copyright (c) 2001-2005, Andrew Aksyonoff. All rights reserved.
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License. You should have
  10. // received a copy of the GPL license along with this program; if you
  11. // did not, you can find it at http://www.gnu.org/
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. // Sphinx PHP API
  15. /////////////////////////////////////////////////////////////////////////////
  16. /// this functions connects to sphinx searchd server,
  17. /// executes given query, and returns search results as a hash
  18. ///
  19. /// $server is searchd server IP address or hostname
  20. /// $port is searchd server port
  21. /// $query is query string
  22. /// $start is offset into the result set to start retrieveing from
  23. /// $rpp is result count to retrieve (Rows Per Page)
  24. /// $weights is an array of weights for each index field
  25. /// $any is search mode, false to match all words and true to match any word
  26. /// $groups is an array of groups to limit matching to
  27. ///
  28. /// returns false on failure
  29. /// returns hash which has the following keys on success:
  30. /// "matches"
  31. /// hash which maps found document_id to ( "weight", "group" ) hash
  32. /// "total"
  33. /// total matches count
  34. /// "time"
  35. /// search time
  36. /// "words"
  37. /// hash which maps query terms (stemmed!) to ( "docs", "hits" ) hash
  38. function sphinxQuery ( $server, $port, $query, $start=0, $rpp=20,
  39. $weights=array(), $any=false, $groups=array() )
  40. {
  41. $start = (int)$start;
  42. $rpp = (int)$rpp;
  43. if (!( $fp = @fsockopen ( $server, $port ) ) )
  44. return false;
  45. // check version
  46. $s = trim ( fgets ( $fp, 1024 ) );
  47. if ( $s!="VER 1" )
  48. {
  49. fclose ( $fp );
  50. return false;
  51. }
  52. // build request
  53. $req = pack ( "VVV", $start, $rpp, $any ? 1 : 0 ); // mode/limits part
  54. $req .= pack ( "V", count($groups) ); // groups
  55. foreach ( $groups as $group )
  56. $req .= pack ( "V", $group );
  57. $req .= pack ( "V", strlen($query) ) . $query; // query string
  58. $req .= pack ( "V", count($weights) ); // weights
  59. foreach ( $weights as $weight )
  60. $req .= pack ( "V", (int)$weight );
  61. // do query
  62. fputs ( $fp, $req );
  63. $result = array();
  64. while ( !feof ( $fp ) )
  65. {
  66. $s = trim ( fgets ( $fp, 1024 ) );
  67. if ( substr ( $s, 0, 6 )=="MATCH " )
  68. {
  69. list ( $dummy, $group, $doc, $weight ) = explode ( " ", $s );
  70. $result["matches"][$doc] = array ( "weight" => $weight, "group" => $group );
  71. } elseif ( substr ( $s, 0, 6 )=="TOTAL " )
  72. {
  73. $result["total"] = substr($s, 6);
  74. } elseif ( substr ( $s, 0, 5 )=="TIME " )
  75. {
  76. $result["time"] = substr ( $s, 5 );
  77. } elseif ( substr ( $s, 0, 5 ) == "WORD " )
  78. {
  79. list ( $dummy, $word, $docs, $hits ) = explode ( " ", $s );
  80. $result["words"][$word]["docs"] = $docs;
  81. $result["words"][$word]["hits"] = $hits;
  82. }
  83. // for now, simply ignore unknown response
  84. }
  85. fclose ( $fp );
  86. return $result;
  87. }
  88. //
  89. // $Id$
  90. //
  91. ?>