PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/REL_0_9_4/api/sphinxapi.php

http://sphinxsearch.googlecode.com/
PHP | 224 lines | 134 code | 34 blank | 56 comment | 11 complexity | 14314e01be4d103dcd1ee61cacfb2808 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. //
  3. // $Id: sphinxapi.php 118 2005-06-03 08:18:42Z shodan $
  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. /// known match modes
  17. define ( "SPH_MATCH_ALL", 0 );
  18. define ( "SPH_MATCH_ANY", 1 );
  19. define ( "SPH_MATCH_PHRASE", 2 );
  20. /// known sort modes
  21. define ( "SPH_SORT_RELEVANCE", 0 );
  22. define ( "SPH_SORT_DATE_DESC", 1 );
  23. define ( "SPH_SORT_DATE_ASC", 2 );
  24. define ( "SPH_SORT_TIME_SEGMENTS", 3 );
  25. /// sphinx searchd client class
  26. class SphinxClient
  27. {
  28. var $_host; ///< searchd host (default is "localhost")
  29. var $_port; ///< searchd port (default is 3312)
  30. var $_offset; ///< how much records to seek from result-set start (default is 0)
  31. var $_limit; ///< how much records to return from result-set starting at offset (default is 20)
  32. var $_mode; ///< query matching mode (default is SPH_MATCH_ALL)
  33. var $_weights; ///< per-field weights (default is 1 for all fields)
  34. var $_groups; ///< groups to limit searching to (default is not to limit)
  35. var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE)
  36. var $_error; ///< last error message
  37. /// create a new client object and fill defaults
  38. function SphinxClient ()
  39. {
  40. $this->_host = "localhost";
  41. $this->_port = 3312;
  42. $this->_offset = 0;
  43. $this->_limit = 20;
  44. $this->_mode = SPH_MATCH_ALL;
  45. $this->_weights = array ();
  46. $this->_groups = array ();
  47. $this->_sort = SPH_SORT_RELEVANCE;
  48. $this->_error = "";
  49. }
  50. /// get last error message (string)
  51. function GetLastError ()
  52. {
  53. return $this->_error;
  54. }
  55. /// set searchd server
  56. function SetServer ( $host, $port )
  57. {
  58. assert ( is_string($host) );
  59. assert ( is_int($port) );
  60. $this->_host = $host;
  61. $this->_port = $port;
  62. }
  63. /// set match offset/limits
  64. function SetLimits ( $offset, $limit )
  65. {
  66. assert ( is_int($offset) );
  67. assert ( is_int($limit) );
  68. assert ( $offset>=0 );
  69. assert ( $limit>0 );
  70. $this->_offset = $offset;
  71. $this->_limit = $limit;
  72. }
  73. /// set match mode
  74. function SetMatchMode ( $mode )
  75. {
  76. assert ( $mode==SPH_MATCH_ALL || $mode==SPH_MATCH_ANY || $mode==SPH_MATCH_PHRASE );
  77. $this->_mode = $mode;
  78. }
  79. /// set match mode
  80. function SetSortMode ( $sort )
  81. {
  82. assert ( $sort==SPH_SORT_RELEVANCE || $sort==SPH_SORT_DATE_DESC || $sort==SPH_SORT_DATE_ASC
  83. || $sort==SPH_SORT_TIME_SEGMENTS );
  84. $this->_sort = $sort;
  85. }
  86. /// set per-field weights
  87. function SetWeights ( $weights )
  88. {
  89. assert ( is_array($weights) );
  90. foreach ( $weights as $weight )
  91. assert ( is_int($weight) );
  92. $this->_weights = $weights;
  93. }
  94. /// set groups
  95. function SetGroups ( $groups )
  96. {
  97. assert ( is_array($groups) );
  98. foreach ( $groups as $group )
  99. assert ( is_int($group) );
  100. $this->_groups = $groups;
  101. }
  102. /// connect to server and run given query
  103. ///
  104. /// $query is query string
  105. ///
  106. /// returns false on failure
  107. /// returns hash which has the following keys on success:
  108. /// "matches"
  109. /// hash which maps found document_id to ( "weight", "group" ) hash
  110. /// "total"
  111. /// total amount of matches retrieved (upto SPH_MAX_MATCHES, see sphinx.h)
  112. /// "total_found"
  113. /// total amount of matching documents in index
  114. /// "time"
  115. /// search time
  116. /// "words"
  117. /// hash which maps query terms (stemmed!) to ( "docs", "hits" ) hash
  118. function Query ( $query )
  119. {
  120. if (!( $fp = @fsockopen ( $this->_host, $this->_port ) ) )
  121. {
  122. $this->_error = "connection to {$this->_host}:{$this->_port} failed";
  123. return false;
  124. }
  125. // check version
  126. $s = trim ( fgets ( $fp, 1024 ) );
  127. if ( substr ( $s, 0, 4 )!="VER " )
  128. {
  129. fclose ( $fp );
  130. $this->_error = "expected searchd protocol version, got '$s'";
  131. return false;
  132. }
  133. $ver = (int)substr ( $s, 4 );
  134. if ( $ver!=1 )
  135. {
  136. fclose ( $fp );
  137. $this->_error = "expected protocol version 1, got $ver";
  138. return false;
  139. }
  140. /////////////////
  141. // build request
  142. /////////////////
  143. // mode/limits part
  144. $req = pack ( "VVVV", $this->_offset, $this->_limit, $this->_mode, $this->_sort );
  145. // groups
  146. $req .= pack ( "V", count($this->_groups) );
  147. foreach ( $this->_groups as $group )
  148. $req .= pack ( "V", $group );
  149. // query string
  150. $req .= pack ( "V", strlen($query) ) . $query;
  151. // weights
  152. $req .= pack ( "V", count($this->_weights) );
  153. foreach ( $this->_weights as $weight )
  154. $req .= pack ( "V", (int)$weight );
  155. ////////////
  156. // do query
  157. ////////////
  158. fputs ( $fp, $req );
  159. $result = array();
  160. while ( !feof ( $fp ) )
  161. {
  162. $s = trim ( fgets ( $fp, 1024 ) );
  163. if ( substr ( $s, 0, 6 )=="MATCH " )
  164. {
  165. list ( $dummy, $group, $doc, $weight, $stamp ) = explode ( " ", $s );
  166. $result["matches"][$doc] = array ( "weight"=>$weight, "group"=>$group,
  167. "stamp"=>$stamp );
  168. } elseif ( substr ( $s, 0, 6 )=="TOTAL " )
  169. {
  170. list ( $dummy, $returned, $found ) = explode ( " ", $s );
  171. $result["total"] = $returned;
  172. $result["total_found"] = $found;
  173. } elseif ( substr ( $s, 0, 5 )=="TIME " )
  174. {
  175. $result["time"] = substr ( $s, 5 );
  176. } elseif ( substr ( $s, 0, 5 ) == "WORD " )
  177. {
  178. list ( $dummy, $word, $docs, $hits ) = explode ( " ", $s );
  179. $result["words"][$word] = array ( "docs"=>$docs, "hits"=>$hits );
  180. }
  181. // for now, simply ignore unknown response
  182. }
  183. fclose ( $fp );
  184. return $result;
  185. }
  186. }
  187. //
  188. // $Id: sphinxapi.php 118 2005-06-03 08:18:42Z shodan $
  189. //
  190. ?>