sphinxapi.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. <?php
  2. //
  3. // $Id$
  4. //
  5. //
  6. // Copyright (c) 2001-2008, 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. // PHP version of Sphinx searchd client (PHP API)
  15. /////////////////////////////////////////////////////////////////////////////
  16. /// known searchd commands
  17. define ( "SEARCHD_COMMAND_SEARCH", 0 );
  18. define ( "SEARCHD_COMMAND_EXCERPT", 1 );
  19. define ( "SEARCHD_COMMAND_UPDATE", 2 );
  20. define ( "SEARCHD_COMMAND_KEYWORDS",3 );
  21. define ( "SEARCHD_COMMAND_PERSIST", 4 );
  22. define ( "SEARCHD_COMMAND_STATUS", 5 );
  23. define ( "SEARCHD_COMMAND_QUERY", 6 );
  24. /// current client-side command implementation versions
  25. define ( "VER_COMMAND_SEARCH", 0x116 );
  26. define ( "VER_COMMAND_EXCERPT", 0x100 );
  27. define ( "VER_COMMAND_UPDATE", 0x102 );
  28. define ( "VER_COMMAND_KEYWORDS", 0x100 );
  29. define ( "VER_COMMAND_STATUS", 0x100 );
  30. define ( "VER_COMMAND_QUERY", 0x100 );
  31. /// known searchd status codes
  32. define ( "SEARCHD_OK", 0 );
  33. define ( "SEARCHD_ERROR", 1 );
  34. define ( "SEARCHD_RETRY", 2 );
  35. define ( "SEARCHD_WARNING", 3 );
  36. /// known match modes
  37. define ( "SPH_MATCH_ALL", 0 );
  38. define ( "SPH_MATCH_ANY", 1 );
  39. define ( "SPH_MATCH_PHRASE", 2 );
  40. define ( "SPH_MATCH_BOOLEAN", 3 );
  41. define ( "SPH_MATCH_EXTENDED", 4 );
  42. define ( "SPH_MATCH_FULLSCAN", 5 );
  43. define ( "SPH_MATCH_EXTENDED2", 6 ); // extended engine V2 (TEMPORARY, WILL BE REMOVED)
  44. /// known ranking modes (ext2 only)
  45. define ( "SPH_RANK_PROXIMITY_BM25", 0 ); ///< default mode, phrase proximity major factor and BM25 minor one
  46. define ( "SPH_RANK_BM25", 1 ); ///< statistical mode, BM25 ranking only (faster but worse quality)
  47. define ( "SPH_RANK_NONE", 2 ); ///< no ranking, all matches get a weight of 1
  48. define ( "SPH_RANK_WORDCOUNT", 3 ); ///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts
  49. define ( "SPH_RANK_PROXIMITY", 4 );
  50. define ( "SPH_RANK_MATCHANY", 5 );
  51. define ( "SPH_RANK_FIELDMASK", 6 );
  52. /// known sort modes
  53. define ( "SPH_SORT_RELEVANCE", 0 );
  54. define ( "SPH_SORT_ATTR_DESC", 1 );
  55. define ( "SPH_SORT_ATTR_ASC", 2 );
  56. define ( "SPH_SORT_TIME_SEGMENTS", 3 );
  57. define ( "SPH_SORT_EXTENDED", 4 );
  58. define ( "SPH_SORT_EXPR", 5 );
  59. /// known filter types
  60. define ( "SPH_FILTER_VALUES", 0 );
  61. define ( "SPH_FILTER_RANGE", 1 );
  62. define ( "SPH_FILTER_FLOATRANGE", 2 );
  63. /// known attribute types
  64. define ( "SPH_ATTR_INTEGER", 1 );
  65. define ( "SPH_ATTR_TIMESTAMP", 2 );
  66. define ( "SPH_ATTR_ORDINAL", 3 );
  67. define ( "SPH_ATTR_BOOL", 4 );
  68. define ( "SPH_ATTR_FLOAT", 5 );
  69. define ( "SPH_ATTR_BIGINT", 6 );
  70. define ( "SPH_ATTR_MULTI", 0x40000000 );
  71. /// known grouping functions
  72. define ( "SPH_GROUPBY_DAY", 0 );
  73. define ( "SPH_GROUPBY_WEEK", 1 );
  74. define ( "SPH_GROUPBY_MONTH", 2 );
  75. define ( "SPH_GROUPBY_YEAR", 3 );
  76. define ( "SPH_GROUPBY_ATTR", 4 );
  77. define ( "SPH_GROUPBY_ATTRPAIR", 5 );
  78. // important properties of PHP's integers:
  79. // - always signed (one bit short of PHP_INT_SIZE)
  80. // - conversion from string to int is saturated
  81. // - float is double
  82. // - div converts arguments to floats
  83. // - mod converts arguments to ints
  84. // the packing code below works as follows:
  85. // - when we got an int, just pack it
  86. // if performance is a problem, this is the branch users should aim for
  87. //
  88. // - otherwise, we got a number in string form
  89. // this might be due to different reasons, but we assume that this is
  90. // because it didn't fit into PHP int
  91. //
  92. // - factor the string into high and low ints for packing
  93. // - if we have bcmath, then it is used
  94. // - if we don't, we have to do it manually (this is the fun part)
  95. //
  96. // - x64 branch does factoring using ints
  97. // - x32 (ab)uses floats, since we can't fit unsigned 32-bit number into an int
  98. //
  99. // unpacking routines are pretty much the same.
  100. // - return ints if we can
  101. // - otherwise format number into a string
  102. /// pack 64-bit signed
  103. function sphPackI64 ( $v )
  104. {
  105. assert ( is_numeric($v) );
  106. // x64
  107. if ( PHP_INT_SIZE>=8 )
  108. {
  109. $v = (int)$v;
  110. return pack ( "NN", $v>>32, $v&0xFFFFFFFF );
  111. }
  112. // x32, int
  113. if ( is_int($v) )
  114. return pack ( "NN", $v < 0 ? -1 : 0, $v );
  115. // x32, bcmath
  116. if ( function_exists("bcmul") )
  117. {
  118. if ( bccomp ( $v, 0 ) == -1 )
  119. $v = bcadd ( "18446744073709551616", $v );
  120. $h = bcdiv ( $v, "4294967296", 0 );
  121. $l = bcmod ( $v, "4294967296" );
  122. return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
  123. }
  124. // x32, no-bcmath
  125. $p = max(0, strlen($v) - 13);
  126. $lo = abs((float)substr($v, $p));
  127. $hi = abs((float)substr($v, 0, $p));
  128. $m = $lo + $hi*1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912
  129. $q = floor($m/4294967296.0);
  130. $l = $m - ($q*4294967296.0);
  131. $h = $hi*2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328
  132. if ( $v<0 )
  133. {
  134. if ( $l==0 )
  135. $h = 4294967296.0 - $h;
  136. else
  137. {
  138. $h = 4294967295.0 - $h;
  139. $l = 4294967296.0 - $l;
  140. }
  141. }
  142. return pack ( "NN", $h, $l );
  143. }
  144. /// pack 64-bit unsigned
  145. function sphPackU64 ( $v )
  146. {
  147. assert ( is_numeric($v) );
  148. // x64
  149. if ( PHP_INT_SIZE>=8 )
  150. {
  151. assert ( $v>=0 );
  152. // x64, int
  153. if ( is_int($v) )
  154. return pack ( "NN", $v>>32, $v&0xFFFFFFFF );
  155. // x64, bcmath
  156. if ( function_exists("bcmul") )
  157. {
  158. $h = bcdiv ( $v, 4294967296, 0 );
  159. $l = bcmod ( $v, 4294967296 );
  160. return pack ( "NN", $h, $l );
  161. }
  162. // x64, no-bcmath
  163. $p = max ( 0, strlen($v) - 13 );
  164. $lo = (int)substr ( $v, $p );
  165. $hi = (int)substr ( $v, 0, $p );
  166. $m = $lo + $hi*1316134912;
  167. $l = $m % 4294967296;
  168. $h = $hi*2328 + (int)($m/4294967296);
  169. return pack ( "NN", $h, $l );
  170. }
  171. // x32, int
  172. if ( is_int($v) )
  173. return pack ( "NN", 0, $v );
  174. // x32, bcmath
  175. if ( function_exists("bcmul") )
  176. {
  177. $h = bcdiv ( $v, "4294967296", 0 );
  178. $l = bcmod ( $v, "4294967296" );
  179. return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
  180. }
  181. // x32, no-bcmath
  182. $p = max(0, strlen($v) - 13);
  183. $lo = (float)substr($v, $p);
  184. $hi = (float)substr($v, 0, $p);
  185. $m = $lo + $hi*1316134912.0;
  186. $q = floor($m / 4294967296.0);
  187. $l = $m - ($q * 4294967296.0);
  188. $h = $hi*2328.0 + $q;
  189. return pack ( "NN", $h, $l );
  190. }
  191. // unpack 64-bit unsigned
  192. function sphUnpackU64 ( $v )
  193. {
  194. list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );
  195. if ( PHP_INT_SIZE>=8 )
  196. {
  197. if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
  198. if ( $lo<0 ) $lo += (1<<32);
  199. // x64, int
  200. if ( $hi<=2147483647 )
  201. return ($hi<<32) + $lo;
  202. // x64, bcmath
  203. if ( function_exists("bcmul") )
  204. return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );
  205. // x64, no-bcmath
  206. $C = 100000;
  207. $h = ((int)($hi / $C) << 32) + (int)($lo / $C);
  208. $l = (($hi % $C) << 32) + ($lo % $C);
  209. if ( $l>$C )
  210. {
  211. $h += (int)($l / $C);
  212. $l = $l % $C;
  213. }
  214. if ( $h==0 )
  215. return $l;
  216. return sprintf ( "%d%05d", $h, $l );
  217. }
  218. // x32, int
  219. if ( $hi==0 )
  220. {
  221. if ( $lo>0 )
  222. return $lo;
  223. return sprintf ( "%u", $lo );
  224. }
  225. $hi = sprintf ( "%u", $hi );
  226. $lo = sprintf ( "%u", $lo );
  227. // x32, bcmath
  228. if ( function_exists("bcmul") )
  229. return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );
  230. // x32, no-bcmath
  231. $hi = (float)$hi;
  232. $lo = (float)$lo;
  233. $q = floor($hi/10000000.0);
  234. $r = $hi - $q*10000000.0;
  235. $m = $lo + $r*4967296.0;
  236. $mq = floor($m/10000000.0);
  237. $l = $m - $mq*10000000.0;
  238. $h = $q*4294967296.0 + $r*429.0 + $mq;
  239. $h = sprintf ( "%.0f", $h );
  240. $l = sprintf ( "%07.0f", $l );
  241. if ( $h=="0" )
  242. return sprintf( "%.0f", (float)$l );
  243. return $h . $l;
  244. }
  245. // unpack 64-bit signed
  246. function sphUnpackI64 ( $v )
  247. {
  248. list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );
  249. // x64
  250. if ( PHP_INT_SIZE>=8 )
  251. {
  252. if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
  253. if ( $lo<0 ) $lo += (1<<32);
  254. return ($hi<<32) + $lo;
  255. }
  256. // x32, int
  257. if ( $hi==0 )
  258. {
  259. if ( $lo>0 )
  260. return $lo;
  261. return sprintf ( "%u", $lo );
  262. }
  263. // x32, int
  264. elseif ( $hi==-1 )
  265. {
  266. if ( $lo<0 )
  267. return $lo;
  268. return sprintf ( "%.0f", $lo - 4294967296.0 );
  269. }
  270. $neg = "";
  271. $c = 0;
  272. if ( $hi<0 )
  273. {
  274. $hi = ~$hi;
  275. $lo = ~$lo;
  276. $c = 1;
  277. $neg = "-";
  278. }
  279. $hi = sprintf ( "%u", $hi );
  280. $lo = sprintf ( "%u", $lo );
  281. // x32, bcmath
  282. if ( function_exists("bcmul") )
  283. return $neg . bcadd ( bcadd ( $lo, bcmul ( $hi, "4294967296" ) ), $c );
  284. // x32, no-bcmath
  285. $hi = (float)$hi;
  286. $lo = (float)$lo;
  287. $q = floor($hi/10000000.0);
  288. $r = $hi - $q*10000000.0;
  289. $m = $lo + $r*4967296.0;
  290. $mq = floor($m/10000000.0);
  291. $l = $m - $mq*10000000.0 + $c;
  292. $h = $q*4294967296.0 + $r*429.0 + $mq;
  293. $h = sprintf ( "%.0f", $h );
  294. $l = sprintf ( "%07.0f", $l );
  295. if ( $h=="0" )
  296. return $neg . sprintf( "%.0f", (float)$l );
  297. return $neg . $h . $l;
  298. }
  299. /// sphinx searchd client class
  300. class SphinxClient
  301. {
  302. var $_host; ///< searchd host (default is "localhost")
  303. var $_port; ///< searchd port (default is 3312)
  304. var $_offset; ///< how many records to seek from result-set start (default is 0)
  305. var $_limit; ///< how many records to return from result-set starting at offset (default is 20)
  306. var $_mode; ///< query matching mode (default is SPH_MATCH_ALL)
  307. var $_weights; ///< per-field weights (default is 1 for all fields)
  308. var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE)
  309. var $_sortby; ///< attribute to sort by (defualt is "")
  310. var $_min_id; ///< min ID to match (default is 0, which means no limit)
  311. var $_max_id; ///< max ID to match (default is 0, which means no limit)
  312. var $_filters; ///< search filters
  313. var $_groupby; ///< group-by attribute name
  314. var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with)
  315. var $_groupsort; ///< group-by sorting clause (to sort groups in result set with)
  316. var $_groupdistinct;///< group-by count-distinct attribute
  317. var $_maxmatches; ///< max matches to retrieve
  318. var $_cutoff; ///< cutoff to stop searching at (default is 0)
  319. var $_retrycount; ///< distributed retries count
  320. var $_retrydelay; ///< distributed retries delay
  321. var $_anchor; ///< geographical anchor point
  322. var $_indexweights; ///< per-index weights
  323. var $_ranker; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25)
  324. var $_maxquerytime; ///< max query time, milliseconds (default is 0, do not limit)
  325. var $_fieldweights; ///< per-field-name weights
  326. var $_overrides; ///< per-query attribute values overrides
  327. var $_select; ///< select-list (attributes or expressions, with optional aliases)
  328. var $_error; ///< last error message
  329. var $_warning; ///< last warning message
  330. var $_connerror; ///< connection error vs remote error flag
  331. var $_reqs; ///< requests array for multi-query
  332. var $_mbenc; ///< stored mbstring encoding
  333. var $_arrayresult; ///< whether $result["matches"] should be a hash or an array
  334. var $_timeout; ///< connect timeout
  335. /////////////////////////////////////////////////////////////////////////////
  336. // common stuff
  337. /////////////////////////////////////////////////////////////////////////////
  338. /// create a new client object and fill defaults
  339. function SphinxClient ()
  340. {
  341. // per-client-object settings
  342. $this->_host = "localhost";
  343. $this->_port = 3312;
  344. $this->_path = false;
  345. $this->_socket = false;
  346. // per-query settings
  347. $this->_offset = 0;
  348. $this->_limit = 20;
  349. $this->_mode = SPH_MATCH_ALL;
  350. $this->_weights = array ();
  351. $this->_sort = SPH_SORT_RELEVANCE;
  352. $this->_sortby = "";
  353. $this->_min_id = 0;
  354. $this->_max_id = 0;
  355. $this->_filters = array ();
  356. $this->_groupby = "";
  357. $this->_groupfunc = SPH_GROUPBY_DAY;
  358. $this->_groupsort = "@group desc";
  359. $this->_groupdistinct= "";
  360. $this->_maxmatches = 1000;
  361. $this->_cutoff = 0;
  362. $this->_retrycount = 0;
  363. $this->_retrydelay = 0;
  364. $this->_anchor = array ();
  365. $this->_indexweights= array ();
  366. $this->_ranker = SPH_RANK_PROXIMITY_BM25;
  367. $this->_maxquerytime= 0;
  368. $this->_fieldweights= array();
  369. $this->_overrides = array();
  370. $this->_select = "*";
  371. $this->_error = ""; // per-reply fields (for single-query case)
  372. $this->_warning = "";
  373. $this->_connerror = false;
  374. $this->_reqs = array (); // requests storage (for multi-query case)
  375. $this->_mbenc = "";
  376. $this->_arrayresult = false;
  377. $this->_timeout = 0;
  378. }
  379. function __destruct()
  380. {
  381. if ( $this->_socket !== false )
  382. fclose ( $this->_socket );
  383. }
  384. /// get last error message (string)
  385. function GetLastError ()
  386. {
  387. return $this->_error;
  388. }
  389. /// get last warning message (string)
  390. function GetLastWarning ()
  391. {
  392. return $this->_warning;
  393. }
  394. /// get last error flag (to tell network connection errors from searchd errors or broken responses)
  395. function IsConnectError()
  396. {
  397. return $this->_connerror;
  398. }
  399. /// set searchd host name (string) and port (integer)
  400. function SetServer ( $host, $port = 0 )
  401. {
  402. assert ( is_string($host) );
  403. if ( $host[0] == '/')
  404. {
  405. $this->_path = 'unix://' . $host;
  406. return;
  407. }
  408. if ( substr ( $host, 0, 7 )=="unix://" )
  409. {
  410. $this->_path = $host;
  411. return;
  412. }
  413. assert ( is_int($port) );
  414. $this->_host = $host;
  415. $this->_port = $port;
  416. $this->_path = '';
  417. }
  418. /// set server connection timeout (0 to remove)
  419. function SetConnectTimeout ( $timeout )
  420. {
  421. assert ( is_numeric($timeout) );
  422. $this->_timeout = $timeout;
  423. }
  424. function _Send ( $handle, $data, $length )
  425. {
  426. if ( feof($handle) || fwrite ( $handle, $data, $length ) !== $length )
  427. {
  428. $this->_error = 'connection unexpectedly closed (timed out?)';
  429. $this->_connerror = true;
  430. return false;
  431. }
  432. return true;
  433. }
  434. /////////////////////////////////////////////////////////////////////////////
  435. /// enter mbstring workaround mode
  436. function _MBPush ()
  437. {
  438. $this->_mbenc = "";
  439. if ( ini_get ( "mbstring.func_overload" ) & 2 )
  440. {
  441. $this->_mbenc = mb_internal_encoding();
  442. mb_internal_encoding ( "latin1" );
  443. }
  444. }
  445. /// leave mbstring workaround mode
  446. function _MBPop ()
  447. {
  448. if ( $this->_mbenc )
  449. mb_internal_encoding ( $this->_mbenc );
  450. }
  451. /// connect to searchd server
  452. function _Connect ()
  453. {
  454. if ( $this->_socket !== false )
  455. return $this->_socket;
  456. $errno = 0;
  457. $errstr = "";
  458. $this->_connerror = false;
  459. if ( $this->_path )
  460. {
  461. $host = $this->_path;
  462. $port = 0;
  463. }
  464. else
  465. {
  466. $host = $this->_host;
  467. $port = $this->_port;
  468. }
  469. if ( $this->_timeout<=0 )
  470. $fp = @fsockopen ( $host, $port, $errno, $errstr );
  471. else
  472. $fp = @fsockopen ( $host, $port, $errno, $errstr, $this->_timeout );
  473. if ( !$fp )
  474. {
  475. if ( $this->_path )
  476. $location = $this->_path;
  477. else
  478. $location = "{$this->_host}:{$this->_port}";
  479. $errstr = trim ( $errstr );
  480. $this->_error = "connection to $location failed (errno=$errno, msg=$errstr)";
  481. $this->_connerror = true;
  482. return false;
  483. }
  484. // check version
  485. list(,$v) = unpack ( "N*", fread ( $fp, 4 ) );
  486. $v = (int)$v;
  487. if ( $v<1 )
  488. {
  489. fclose ( $fp );
  490. $this->_error = "expected searchd protocol version 1+, got version '$v'";
  491. return false;
  492. }
  493. // all ok, send my version
  494. if ( !$this->_Send ( $fp, pack ( "N", 1 ), 4 ) )
  495. return false;
  496. return $fp;
  497. }
  498. /// get and check response packet from searchd server
  499. function _GetResponse ( $fp, $client_ver )
  500. {
  501. $response = "";
  502. $len = 0;
  503. $header = fread ( $fp, 8 );
  504. if ( strlen($header)==8 )
  505. {
  506. list ( $status, $ver, $len ) = array_values ( unpack ( "n2a/Nb", $header ) );
  507. $left = $len;
  508. while ( $left>0 && !feof($fp) )
  509. {
  510. $chunk = fread ( $fp, $left );
  511. if ( $chunk )
  512. {
  513. $response .= $chunk;
  514. $left -= strlen($chunk);
  515. }
  516. }
  517. }
  518. if ( $this->_socket === false )
  519. fclose ( $fp );
  520. // check response
  521. $read = strlen ( $response );
  522. if ( !$response || $read!=$len )
  523. {
  524. $this->_error = $len
  525. ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)"
  526. : "received zero-sized searchd response";
  527. return false;
  528. }
  529. // check status
  530. if ( $status==SEARCHD_WARNING )
  531. {
  532. list(,$wlen) = unpack ( "N*", substr ( $response, 0, 4 ) );
  533. $this->_warning = substr ( $response, 4, $wlen );
  534. return substr ( $response, 4+$wlen );
  535. }
  536. if ( $status==SEARCHD_ERROR )
  537. {
  538. $this->_error = "searchd error: " . substr ( $response, 4 );
  539. return false;
  540. }
  541. if ( $status==SEARCHD_RETRY )
  542. {
  543. $this->_error = "temporary searchd error: " . substr ( $response, 4 );
  544. return false;
  545. }
  546. if ( $status!=SEARCHD_OK )
  547. {
  548. $this->_error = "unknown status code '$status'";
  549. return false;
  550. }
  551. // check version
  552. if ( $ver<$client_ver )
  553. {
  554. $this->_warning = sprintf ( "searchd command v.%d.%d older than client's v.%d.%d, some options might not work",
  555. $ver>>8, $ver&0xff, $client_ver>>8, $client_ver&0xff );
  556. }
  557. return $response;
  558. }
  559. /////////////////////////////////////////////////////////////////////////////
  560. // searching
  561. /////////////////////////////////////////////////////////////////////////////
  562. /// set offset and count into result set,
  563. /// and optionally set max-matches and cutoff limits
  564. function SetLimits ( $offset, $limit, $max=0, $cutoff=0 )
  565. {
  566. assert ( is_int($offset) );
  567. assert ( is_int($limit) );
  568. assert ( $offset>=0 );
  569. assert ( $limit>0 );
  570. assert ( $max>=0 );
  571. $this->_offset = $offset;
  572. $this->_limit = $limit;
  573. if ( $max>0 )
  574. $this->_maxmatches = $max;
  575. if ( $cutoff>0 )
  576. $this->_cutoff = $cutoff;
  577. }
  578. /// set maximum query time, in milliseconds, per-index
  579. /// integer, 0 means "do not limit"
  580. function SetMaxQueryTime ( $max )
  581. {
  582. assert ( is_int($max) );
  583. assert ( $max>=0 );
  584. $this->_maxquerytime = $max;
  585. }
  586. /// set matching mode
  587. function SetMatchMode ( $mode )
  588. {
  589. assert ( $mode==SPH_MATCH_ALL
  590. || $mode==SPH_MATCH_ANY
  591. || $mode==SPH_MATCH_PHRASE
  592. || $mode==SPH_MATCH_BOOLEAN
  593. || $mode==SPH_MATCH_EXTENDED
  594. || $mode==SPH_MATCH_FULLSCAN
  595. || $mode==SPH_MATCH_EXTENDED2 );
  596. $this->_mode = $mode;
  597. }
  598. /// set ranking mode
  599. function SetRankingMode ( $ranker )
  600. {
  601. assert ( $ranker==SPH_RANK_PROXIMITY_BM25
  602. || $ranker==SPH_RANK_BM25
  603. || $ranker==SPH_RANK_NONE
  604. || $ranker==SPH_RANK_WORDCOUNT
  605. || $ranker==SPH_RANK_PROXIMITY );
  606. $this->_ranker = $ranker;
  607. }
  608. /// set matches sorting mode
  609. function SetSortMode ( $mode, $sortby="" )
  610. {
  611. assert (
  612. $mode==SPH_SORT_RELEVANCE ||
  613. $mode==SPH_SORT_ATTR_DESC ||
  614. $mode==SPH_SORT_ATTR_ASC ||
  615. $mode==SPH_SORT_TIME_SEGMENTS ||
  616. $mode==SPH_SORT_EXTENDED ||
  617. $mode==SPH_SORT_EXPR );
  618. assert ( is_string($sortby) );
  619. assert ( $mode==SPH_SORT_RELEVANCE || strlen($sortby)>0 );
  620. $this->_sort = $mode;
  621. $this->_sortby = $sortby;
  622. }
  623. /// bind per-field weights by order
  624. /// DEPRECATED; use SetFieldWeights() instead
  625. function SetWeights ( $weights )
  626. {
  627. assert ( is_array($weights) );
  628. foreach ( $weights as $weight )
  629. assert ( is_int($weight) );
  630. $this->_weights = $weights;
  631. }
  632. /// bind per-field weights by name
  633. function SetFieldWeights ( $weights )
  634. {
  635. assert ( is_array($weights) );
  636. foreach ( $weights as $name=>$weight )
  637. {
  638. assert ( is_string($name) );
  639. assert ( is_int($weight) );
  640. }
  641. $this->_fieldweights = $weights;
  642. }
  643. /// bind per-index weights by name
  644. function SetIndexWeights ( $weights )
  645. {
  646. assert ( is_array($weights) );
  647. foreach ( $weights as $index=>$weight )
  648. {
  649. assert ( is_string($index) );
  650. assert ( is_int($weight) );
  651. }
  652. $this->_indexweights = $weights;
  653. }
  654. /// set IDs range to match
  655. /// only match records if document ID is beetwen $min and $max (inclusive)
  656. function SetIDRange ( $min, $max )
  657. {
  658. assert ( is_numeric($min) );
  659. assert ( is_numeric($max) );
  660. assert ( $min<=$max );
  661. $this->_min_id = $min;
  662. $this->_max_id = $max;
  663. }
  664. /// set values set filter
  665. /// only match records where $attribute value is in given set
  666. function SetFilter ( $attribute, $values, $exclude=false )
  667. {
  668. assert ( is_string($attribute) );
  669. assert ( is_array($values) );
  670. assert ( count($values) );
  671. if ( is_array($values) && count($values) )
  672. {
  673. foreach ( $values as $value )
  674. assert ( is_numeric($value) );
  675. $this->_filters[] = array ( "type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values );
  676. }
  677. }
  678. /// set range filter
  679. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  680. function SetFilterRange ( $attribute, $min, $max, $exclude=false )
  681. {
  682. assert ( is_string($attribute) );
  683. assert ( is_numeric($min) );
  684. assert ( is_numeric($max) );
  685. assert ( $min<=$max );
  686. $this->_filters[] = array ( "type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  687. }
  688. /// set float range filter
  689. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  690. function SetFilterFloatRange ( $attribute, $min, $max, $exclude=false )
  691. {
  692. assert ( is_string($attribute) );
  693. assert ( is_float($min) );
  694. assert ( is_float($max) );
  695. assert ( $min<=$max );
  696. $this->_filters[] = array ( "type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  697. }
  698. /// setup anchor point for geosphere distance calculations
  699. /// required to use @geodist in filters and sorting
  700. /// latitude and longitude must be in radians
  701. function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )
  702. {
  703. assert ( is_string($attrlat) );
  704. assert ( is_string($attrlong) );
  705. assert ( is_float($lat) );
  706. assert ( is_float($long) );
  707. $this->_anchor = array ( "attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long );
  708. }
  709. /// set grouping attribute and function
  710. function SetGroupBy ( $attribute, $func, $groupsort="@group desc" )
  711. {
  712. assert ( is_string($attribute) );
  713. assert ( is_string($groupsort) );
  714. assert ( $func==SPH_GROUPBY_DAY
  715. || $func==SPH_GROUPBY_WEEK
  716. || $func==SPH_GROUPBY_MONTH
  717. || $func==SPH_GROUPBY_YEAR
  718. || $func==SPH_GROUPBY_ATTR
  719. || $func==SPH_GROUPBY_ATTRPAIR );
  720. $this->_groupby = $attribute;
  721. $this->_groupfunc = $func;
  722. $this->_groupsort = $groupsort;
  723. }
  724. /// set count-distinct attribute for group-by queries
  725. function SetGroupDistinct ( $attribute )
  726. {
  727. assert ( is_string($attribute) );
  728. $this->_groupdistinct = $attribute;
  729. }
  730. /// set distributed retries count and delay
  731. function SetRetries ( $count, $delay=0 )
  732. {
  733. assert ( is_int($count) && $count>=0 );
  734. assert ( is_int($delay) && $delay>=0 );
  735. $this->_retrycount = $count;
  736. $this->_retrydelay = $delay;
  737. }
  738. /// set result set format (hash or array; hash by default)
  739. /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs
  740. function SetArrayResult ( $arrayresult )
  741. {
  742. assert ( is_bool($arrayresult) );
  743. $this->_arrayresult = $arrayresult;
  744. }
  745. /// set attribute values override
  746. /// there can be only one override per attribute
  747. /// $values must be a hash that maps document IDs to attribute values
  748. function SetOverride ( $attrname, $attrtype, $values )
  749. {
  750. assert ( is_string ( $attrname ) );
  751. assert ( in_array ( $attrtype, array ( SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT ) ) );
  752. assert ( is_array ( $values ) );
  753. $this->_overrides[$attrname] = array ( "attr"=>$attrname, "type"=>$attrtype, "values"=>$values );
  754. }
  755. /// set select-list (attributes or expressions), SQL-like syntax
  756. function SetSelect ( $select )
  757. {
  758. assert ( is_string ( $select ) );
  759. $this->_select = $select;
  760. }
  761. //////////////////////////////////////////////////////////////////////////////
  762. /// clear all filters (for multi-queries)
  763. function ResetFilters ()
  764. {
  765. $this->_filters = array();
  766. $this->_anchor = array();
  767. }
  768. /// clear groupby settings (for multi-queries)
  769. function ResetGroupBy ()
  770. {
  771. $this->_groupby = "";
  772. $this->_groupfunc = SPH_GROUPBY_DAY;
  773. $this->_groupsort = "@group desc";
  774. $this->_groupdistinct= "";
  775. }
  776. /// clear all attribute value overrides (for multi-queries)
  777. function ResetOverrides ()
  778. {
  779. $this->_overrides = array ();
  780. }
  781. //////////////////////////////////////////////////////////////////////////////
  782. /// connect to searchd server, run given search query through given indexes,
  783. /// and return the search results
  784. function Query ( $query, $index="*", $comment="" )
  785. {
  786. assert ( empty($this->_reqs) );
  787. $this->AddQuery ( $query, $index, $comment );
  788. $results = $this->RunQueries ();
  789. $this->_reqs = array (); // just in case it failed too early
  790. if ( !is_array($results) )
  791. return false; // probably network error; error message should be already filled
  792. $this->_error = $results[0]["error"];
  793. $this->_warning = $results[0]["warning"];
  794. if ( $results[0]["status"]==SEARCHD_ERROR )
  795. return false;
  796. else
  797. return $results[0];
  798. }
  799. /// helper to pack floats in network byte order
  800. function _PackFloat ( $f )
  801. {
  802. $t1 = pack ( "f", $f ); // machine order
  803. list(,$t2) = unpack ( "L*", $t1 ); // int in machine order
  804. return pack ( "N", $t2 );
  805. }
  806. /// add query to multi-query batch
  807. /// returns index into results array from RunQueries() call
  808. function AddQuery ( $query, $index="*", $comment="" )
  809. {
  810. // mbstring workaround
  811. $this->_MBPush ();
  812. // build request
  813. $req = pack ( "NNNNN", $this->_offset, $this->_limit, $this->_mode, $this->_ranker, $this->_sort ); // mode and limits
  814. $req .= pack ( "N", strlen($this->_sortby) ) . $this->_sortby;
  815. $req .= pack ( "N", strlen($query) ) . $query; // query itself
  816. $req .= pack ( "N", count($this->_weights) ); // weights
  817. foreach ( $this->_weights as $weight )
  818. $req .= pack ( "N", (int)$weight );
  819. $req .= pack ( "N", strlen($index) ) . $index; // indexes
  820. $req .= pack ( "N", 1 ); // id64 range marker
  821. $req .= sphPackU64 ( $this->_min_id ) . sphPackU64 ( $this->_max_id ); // id64 range
  822. // filters
  823. $req .= pack ( "N", count($this->_filters) );
  824. foreach ( $this->_filters as $filter )
  825. {
  826. $req .= pack ( "N", strlen($filter["attr"]) ) . $filter["attr"];
  827. $req .= pack ( "N", $filter["type"] );
  828. switch ( $filter["type"] )
  829. {
  830. case SPH_FILTER_VALUES:
  831. $req .= pack ( "N", count($filter["values"]) );
  832. foreach ( $filter["values"] as $value )
  833. $req .= sphPackI64 ( $value );
  834. break;
  835. case SPH_FILTER_RANGE:
  836. $req .= sphPackI64 ( $filter["min"] ) . sphPackI64 ( $filter["max"] );
  837. break;
  838. case SPH_FILTER_FLOATRANGE:
  839. $req .= $this->_PackFloat ( $filter["min"] ) . $this->_PackFloat ( $filter["max"] );
  840. break;
  841. default:
  842. assert ( 0 && "internal error: unhandled filter type" );
  843. }
  844. $req .= pack ( "N", $filter["exclude"] );
  845. }
  846. // group-by clause, max-matches count, group-sort clause, cutoff count
  847. $req .= pack ( "NN", $this->_groupfunc, strlen($this->_groupby) ) . $this->_groupby;
  848. $req .= pack ( "N", $this->_maxmatches );
  849. $req .= pack ( "N", strlen($this->_groupsort) ) . $this->_groupsort;
  850. $req .= pack ( "NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay );
  851. $req .= pack ( "N", strlen($this->_groupdistinct) ) . $this->_groupdistinct;
  852. // anchor point
  853. if ( empty($this->_anchor) )
  854. {
  855. $req .= pack ( "N", 0 );
  856. } else
  857. {
  858. $a =& $this->_anchor;
  859. $req .= pack ( "N", 1 );
  860. $req .= pack ( "N", strlen($a["attrlat"]) ) . $a["attrlat"];
  861. $req .= pack ( "N", strlen($a["attrlong"]) ) . $a["attrlong"];
  862. $req .= $this->_PackFloat ( $a["lat"] ) . $this->_PackFloat ( $a["long"] );
  863. }
  864. // per-index weights
  865. $req .= pack ( "N", count($this->_indexweights) );
  866. foreach ( $this->_indexweights as $idx=>$weight )
  867. $req .= pack ( "N", strlen($idx) ) . $idx . pack ( "N", $weight );
  868. // max query time
  869. $req .= pack ( "N", $this->_maxquerytime );
  870. // per-field weights
  871. $req .= pack ( "N", count($this->_fieldweights) );
  872. foreach ( $this->_fieldweights as $field=>$weight )
  873. $req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight );
  874. // comment
  875. $req .= pack ( "N", strlen($comment) ) . $comment;
  876. // attribute overrides
  877. $req .= pack ( "N", count($this->_overrides) );
  878. foreach ( $this->_overrides as $key => $entry )
  879. {
  880. $req .= pack ( "N", strlen($entry["attr"]) ) . $entry["attr"];
  881. $req .= pack ( "NN", $entry["type"], count($entry["values"]) );
  882. foreach ( $entry["values"] as $id=>$val )
  883. {
  884. assert ( is_numeric($id) );
  885. assert ( is_numeric($val) );
  886. $req .= sphPackU64 ( $id );
  887. switch ( $entry["type"] )
  888. {
  889. case SPH_ATTR_FLOAT: $req .= $this->_PackFloat ( $val ); break;
  890. case SPH_ATTR_BIGINT: $req .= sphPackI64 ( $val ); break;
  891. default: $req .= pack ( "N", $val ); break;
  892. }
  893. }
  894. }
  895. // select-list
  896. $req .= pack ( "N", strlen($this->_select) ) . $this->_select;
  897. // mbstring workaround
  898. $this->_MBPop ();
  899. // store request to requests array
  900. $this->_reqs[] = $req;
  901. return count($this->_reqs)-1;
  902. }
  903. /// connect to searchd, run queries batch, and return an array of result sets
  904. function RunQueries ()
  905. {
  906. if ( empty($this->_reqs) )
  907. {
  908. $this->_error = "no queries defined, issue AddQuery() first";
  909. return false;
  910. }
  911. // mbstring workaround
  912. $this->_MBPush ();
  913. if (!( $fp = $this->_Connect() ))
  914. {
  915. $this->_MBPop ();
  916. return false;
  917. }
  918. // send query, get response
  919. $nreqs = count($this->_reqs);
  920. $req = join ( "", $this->_reqs );
  921. $len = 4+strlen($req);
  922. $req = pack ( "nnNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, $nreqs ) . $req; // add header
  923. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  924. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) )
  925. {
  926. $this->_MBPop ();
  927. return false;
  928. }
  929. // query sent ok; we can reset reqs now
  930. $this->_reqs = array ();
  931. // parse and return response
  932. return $this->_ParseSearchResponse ( $response, $nreqs );
  933. }
  934. /// parse and return search query (or queries) response
  935. function _ParseSearchResponse ( $response, $nreqs )
  936. {
  937. $p = 0; // current position
  938. $max = strlen($response); // max position for checks, to protect against broken responses
  939. $results = array ();
  940. for ( $ires=0; $ires<$nreqs && $p<$max; $ires++ )
  941. {
  942. $results[] = array();
  943. $result =& $results[$ires];
  944. $result["error"] = "";
  945. $result["warning"] = "";
  946. // extract status
  947. list(,$status) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  948. $result["status"] = $status;
  949. if ( $status!=SEARCHD_OK )
  950. {
  951. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  952. $message = substr ( $response, $p, $len ); $p += $len;
  953. if ( $status==SEARCHD_WARNING )
  954. {
  955. $result["warning"] = $message;
  956. } else
  957. {
  958. $result["error"] = $message;
  959. continue;
  960. }
  961. }
  962. // read schema
  963. $fields = array ();
  964. $attrs = array ();
  965. list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  966. while ( $nfields-->0 && $p<$max )
  967. {
  968. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  969. $fields[] = substr ( $response, $p, $len ); $p += $len;
  970. }
  971. $result["fields"] = $fields;
  972. list(,$nattrs) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  973. while ( $nattrs-->0 && $p<$max )
  974. {
  975. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  976. $attr = substr ( $response, $p, $len ); $p += $len;
  977. list(,$type) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  978. $attrs[$attr] = $type;
  979. }
  980. $result["attrs"] = $attrs;
  981. // read match count
  982. list(,$count) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  983. list(,$id64) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  984. // read matches
  985. $idx = -1;
  986. while ( $count-->0 && $p<$max )
  987. {
  988. // index into result array
  989. $idx++;
  990. // parse document id and weight
  991. if ( $id64 )
  992. {
  993. $doc = sphUnpackU64 ( substr ( $response, $p, 8 ) ); $p += 8;
  994. list(,$weight) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  995. }
  996. else
  997. {
  998. list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
  999. substr ( $response, $p, 8 ) ) );
  1000. $p += 8;
  1001. if ( PHP_INT_SIZE>=8 )
  1002. {
  1003. // x64 route, workaround broken unpack() in 5.2.2+
  1004. if ( $doc<0 ) $doc += (1<<32);
  1005. } else
  1006. {
  1007. // x32 route, workaround php signed/unsigned braindamage
  1008. $doc = sprintf ( "%u", $doc );
  1009. }
  1010. }
  1011. $weight = sprintf ( "%u", $weight );
  1012. // create match entry
  1013. if ( $this->_arrayresult )
  1014. $result["matches"][$idx] = array ( "id"=>$doc, "weight"=>$weight );
  1015. else
  1016. $result["matches"][$doc]["weight"] = $weight;
  1017. // parse and create attributes
  1018. $attrvals = array ();
  1019. foreach ( $attrs as $attr=>$type )
  1020. {
  1021. // handle 64bit ints
  1022. if ( $type==SPH_ATTR_BIGINT )
  1023. {
  1024. $attrvals[$attr] = sphUnpackI64 ( substr ( $response, $p, 8 ) ); $p += 8;
  1025. continue;
  1026. }
  1027. // handle floats
  1028. if ( $type==SPH_ATTR_FLOAT )
  1029. {
  1030. list(,$uval) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1031. list(,$fval) = unpack ( "f*", pack ( "L", $uval ) );
  1032. $attrvals[$attr] = $fval;
  1033. continue;
  1034. }
  1035. // handle everything else as unsigned ints
  1036. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1037. if ( $type & SPH_ATTR_MULTI )
  1038. {
  1039. $attrvals[$attr] = array ();
  1040. $nvalues = $val;
  1041. while ( $nvalues-->0 && $p<$max )
  1042. {
  1043. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1044. $attrvals[$attr][] = sprintf ( "%u", $val );
  1045. }
  1046. } else
  1047. {
  1048. $attrvals[$attr] = sprintf ( "%u", $val );
  1049. }
  1050. }
  1051. if ( $this->_arrayresult )
  1052. $result["matches"][$idx]["attrs"] = $attrvals;
  1053. else
  1054. $result["matches"][$doc]["attrs"] = $attrvals;
  1055. }
  1056. list ( $total, $total_found, $msecs, $words ) =
  1057. array_values ( unpack ( "N*N*N*N*", substr ( $response, $p, 16 ) ) );
  1058. $result["total"] = sprintf ( "%u", $total );
  1059. $result["total_found"] = sprintf ( "%u", $total_found );
  1060. $result["time"] = sprintf ( "%.3f", $msecs/1000 );
  1061. $p += 16;
  1062. while ( $words-->0 && $p<$max )
  1063. {
  1064. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1065. $word = substr ( $response, $p, $len ); $p += $len;
  1066. list ( $docs, $hits ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
  1067. $result["words"][$word] = array (
  1068. "docs"=>sprintf ( "%u", $docs ),
  1069. "hits"=>sprintf ( "%u", $hits ) );
  1070. }
  1071. }
  1072. $this->_MBPop ();
  1073. return $results;
  1074. }
  1075. /////////////////////////////////////////////////////////////////////////////
  1076. // excerpts generation
  1077. /////////////////////////////////////////////////////////////////////////////
  1078. /// connect to searchd server, and generate exceprts (snippets)
  1079. /// of given documents for given query. returns false on failure,
  1080. /// an array of snippets on success
  1081. function BuildExcerpts ( $docs, $index, $words, $opts=array() )
  1082. {
  1083. assert ( is_array($docs) );
  1084. assert ( is_string($index) );
  1085. assert ( is_string($words) );
  1086. assert ( is_array($opts) );
  1087. $this->_MBPush ();
  1088. if (!( $fp = $this->_Connect() ))
  1089. {
  1090. $this->_MBPop();
  1091. return false;
  1092. }
  1093. /////////////////
  1094. // fixup options
  1095. /////////////////
  1096. if ( !isset($opts["before_match"]) ) $opts["before_match"] = "<b>";
  1097. if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>";
  1098. if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... ";
  1099. if ( !isset($opts["limit"]) ) $opts["limit"] = 256;
  1100. if ( !isset($opts["around"]) ) $opts["around"] = 5;
  1101. if ( !isset($opts["exact_phrase"]) ) $opts["exact_phrase"] = false;
  1102. if ( !isset($opts["single_passage"]) ) $opts["single_passage"] = false;
  1103. if ( !isset($opts["use_boundaries"]) ) $opts["use_boundaries"] = false;
  1104. if ( !isset($opts["weight_order"]) ) $opts["weight_order"] = false;
  1105. /////////////////
  1106. // build request
  1107. /////////////////
  1108. // v.1.0 req
  1109. $flags = 1; // remove spaces
  1110. if ( $opts["exact_phrase"] ) $flags |= 2;
  1111. if ( $opts["single_passage"] ) $flags |= 4;
  1112. if ( $opts["use_boundaries"] ) $flags |= 8;
  1113. if ( $opts["weight_order"] ) $flags |= 16;
  1114. $req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags
  1115. $req .= pack ( "N", strlen($index) ) . $index; // req index
  1116. $req .= pack ( "N", strlen($words) ) . $words; // req words
  1117. // options
  1118. $req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
  1119. $req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
  1120. $req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
  1121. $req .= pack ( "N", (int)$opts["limit"] );
  1122. $req .= pack ( "N", (int)$opts["around"] );
  1123. // documents
  1124. $req .= pack ( "N", count($docs) );
  1125. foreach ( $docs as $doc )
  1126. {
  1127. assert ( is_string($doc) );
  1128. $req .= pack ( "N", strlen($doc) ) . $doc;
  1129. }
  1130. ////////////////////////////
  1131. // send query, get response
  1132. ////////////////////////////
  1133. $len = strlen($req);
  1134. $req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
  1135. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  1136. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ) )
  1137. {
  1138. $this->_MBPop ();
  1139. return false;
  1140. }
  1141. //////////////////
  1142. // parse response
  1143. //////////////////
  1144. $pos = 0;
  1145. $res = array ();
  1146. $rlen = strlen($response);
  1147. for ( $i=0; $i<count($docs); $i++ )
  1148. {
  1149. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  1150. $pos += 4;
  1151. if ( $pos+$len > $rlen )
  1152. {
  1153. $this->_error = "incomplete reply";
  1154. $this->_MBPop ();
  1155. return false;
  1156. }
  1157. $res[] = $len ? substr ( $response, $pos, $len ) : "";
  1158. $pos += $len;
  1159. }
  1160. $this->_MBPop ();
  1161. return $res;
  1162. }
  1163. /////////////////////////////////////////////////////////////////////////////
  1164. // keyword generation
  1165. /////////////////////////////////////////////////////////////////////////////
  1166. /// connect to searchd server, and generate keyword list for a given query
  1167. /// returns false on failure,
  1168. /// an array of words on success
  1169. function BuildKeywords ( $query, $index, $hits )
  1170. {
  1171. assert ( is_string($query) );
  1172. assert ( is_string($index) );
  1173. assert ( is_bool($hits) );
  1174. $this->_MBPush ();
  1175. if (!( $fp = $this->_Connect() ))
  1176. {
  1177. $this->_MBPop();
  1178. return false;
  1179. }
  1180. /////////////////
  1181. // build request
  1182. /////////////////
  1183. // v.1.0 req
  1184. $req = pack ( "N", strlen($query) ) . $query; // req query
  1185. $req .= pack ( "N", strlen($index) ) . $index; // req index
  1186. $req .= pack ( "N", (int)$hits );
  1187. ////////////////////////////
  1188. // send query, get response
  1189. ////////////////////////////
  1190. $len = strlen($req);
  1191. $req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
  1192. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  1193. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ) )
  1194. {
  1195. $this->_MBPop ();
  1196. return false;
  1197. }
  1198. //////////////////
  1199. // parse response
  1200. //////////////////
  1201. $pos = 0;
  1202. $res = array ();
  1203. $rlen = strlen($response);
  1204. list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  1205. $pos += 4;
  1206. for ( $i=0; $i<$nwords; $i++ )
  1207. {
  1208. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  1209. $tokenized = $len ? substr ( $response, $pos, $len ) : "";
  1210. $pos += $len;
  1211. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  1212. $normalized = $len ? substr ( $response, $pos, $len ) : "";
  1213. $pos += $len;
  1214. $res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized );
  1215. if ( $hits )
  1216. {
  1217. list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) );
  1218. $pos += 8;
  1219. $res [$i]["docs"] = $ndocs;
  1220. $res [$i]["hits"] = $nhits;
  1221. }
  1222. if ( $pos > $rlen )
  1223. {
  1224. $this->_error = "incomplete reply";
  1225. $this->_MBPop ();
  1226. return false;
  1227. }
  1228. }
  1229. $this->_MBPop ();
  1230. return $res;
  1231. }
  1232. function EscapeString ( $string )
  1233. {
  1234. $from = array ( '\\', '(',')','|','-','!','@','~','"','&', '/' );
  1235. $to = array ( '\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/' );
  1236. return str_replace ( $from, $to, $string );
  1237. }
  1238. /////////////////////////////////////////////////////////////////////////////
  1239. // attribute updates
  1240. /////////////////////////////////////////////////////////////////////////////
  1241. /// batch update given attributes in given rows in given indexes
  1242. /// returns amount of updated documents (0 or more) on success, or -1 on failure
  1243. function UpdateAttributes ( $index, $attrs, $values, $mva=false )
  1244. {
  1245. // verify everything
  1246. assert ( is_string($index) );
  1247. assert ( is_bool($mva) );
  1248. assert ( is_array($attrs) );
  1249. foreach ( $attrs as $attr )
  1250. assert ( is_string($attr) );
  1251. assert ( is_array($values) );
  1252. foreach ( $values as $id=>$entry )
  1253. {
  1254. assert ( is_numeric($id) );
  1255. assert ( is_array($entry) );
  1256. assert ( count($entry)==count($attrs) );
  1257. foreach ( $entry as $v )
  1258. {
  1259. if ( $mva )
  1260. {
  1261. assert ( is_array($v) );
  1262. foreach ( $v as $vv )
  1263. assert ( is_int($vv) );
  1264. } else
  1265. assert ( is_int($v) );
  1266. }
  1267. }
  1268. // build request
  1269. $req = pack ( "N", strlen($index) ) . $index;
  1270. $req .= pack ( "N", count($attrs) );
  1271. foreach ( $attrs as $attr )
  1272. {
  1273. $req .= pack ( "N", strlen($attr) ) . $attr;
  1274. $req .= pack ( "N", $mva ? 1 : 0 );
  1275. }
  1276. $req .= pack ( "N", count($values) );
  1277. foreach ( $values as $id=>$entry )
  1278. {
  1279. $req .= sphPackU64 ( $id );
  1280. foreach ( $entry as $v )
  1281. {
  1282. $req .= pack ( "N", $mva ? count($v) : $v );
  1283. if ( $mva )
  1284. foreach ( $v as $vv )
  1285. $req .= pack ( "N", $vv );
  1286. }
  1287. }
  1288. // connect, send query, get response
  1289. if (!( $fp = $this->_Connect() ))
  1290. return -1;
  1291. $len = strlen($req);
  1292. $req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
  1293. if ( !$this->_Send ( $fp, $req, $len+8 ) )
  1294. return -1;
  1295. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
  1296. return -1;
  1297. // parse response
  1298. list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) );
  1299. return $updated;
  1300. }
  1301. /////////////////////////////////////////////////////////////////////////////
  1302. // persistent connections
  1303. /////////////////////////////////////////////////////////////////////////////
  1304. function Open()
  1305. {
  1306. if ( $this->_socket !== false )
  1307. {
  1308. $this->_error = 'already connected';
  1309. return false;
  1310. }
  1311. if ( !$fp = $this->_Connect() )
  1312. return false;
  1313. // command, command version = 0, body length = 4, body = 1
  1314. $req = pack ( "nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1 );
  1315. if ( !$this->_Send ( $fp, $req, 12 ) )
  1316. return false;
  1317. $this->_socket = $fp;
  1318. return true;
  1319. }
  1320. function Close()
  1321. {
  1322. if ( $this->_socket === false )
  1323. {
  1324. $this->_error = 'not connected';
  1325. return false;
  1326. }
  1327. fclose ( $this->_socket );
  1328. $this->_socket = false;
  1329. return true;
  1330. }
  1331. //////////////////////////////////////////////////////////////////////////
  1332. // status
  1333. //////////////////////////////////////////////////////////////////////////
  1334. function Status ()
  1335. {
  1336. $this->_MBPush ();
  1337. if (!( $fp = $this->_Connect() ))
  1338. {
  1339. $this->_MBPop();
  1340. return false;
  1341. }
  1342. $req = pack ( "nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, 1 ); // len=4, body=1
  1343. if ( !( $this->_Send ( $fp, $req, 12 ) ) ||
  1344. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_STATUS ) ) )
  1345. {
  1346. $this->_MBPop ();
  1347. return false;
  1348. }
  1349. $res = substr ( $response, 4 ); // just ignore length, error handling, etc
  1350. $this->_MBPop ();
  1351. return $res;
  1352. }
  1353. //////////////////////////////////////////////////////////////////////////
  1354. // sphinxql query
  1355. //////////////////////////////////////////////////////////////////////////
  1356. function SqlQuery ( $query )
  1357. {
  1358. $this->_MBPush ();
  1359. if (!( $fp = $this->_Connect() ))
  1360. {
  1361. $this->_MBPop();
  1362. return false;
  1363. }
  1364. $len = strlen($query);
  1365. $req = pack ( "nnNNN", SEARCHD_COMMAND_QUERY, VER_COMMAND_QUERY, 8+$len, VER_COMMAND_SEARCH, $len ) . $query;
  1366. if ( !( $this->_Send ( $fp, $req, 16+$len ) ) ||
  1367. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_QUERY ) ) )
  1368. {
  1369. $this->_MBPop ();
  1370. return false;
  1371. }
  1372. // parse and return response
  1373. $results = $this->_ParseSearchResponse ( $response, 1 );
  1374. return $results[0];
  1375. }
  1376. }
  1377. //
  1378. // $Id$
  1379. //