sphinxapi.php 46 KB

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