sphinxapi.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. //
  3. // $Id$
  4. //
  5. //
  6. // Copyright (c) 2001-2007, 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. /// current client-side command implementation versions
  21. define ( "VER_COMMAND_SEARCH", 0x109 );
  22. define ( "VER_COMMAND_EXCERPT", 0x100 );
  23. define ( "VER_COMMAND_UPDATE", 0x100 );
  24. /// known searchd status codes
  25. define ( "SEARCHD_OK", 0 );
  26. define ( "SEARCHD_ERROR", 1 );
  27. define ( "SEARCHD_RETRY", 2 );
  28. define ( "SEARCHD_WARNING", 3 );
  29. /// known match modes
  30. define ( "SPH_MATCH_ALL", 0 );
  31. define ( "SPH_MATCH_ANY", 1 );
  32. define ( "SPH_MATCH_PHRASE", 2 );
  33. define ( "SPH_MATCH_BOOLEAN", 3 );
  34. define ( "SPH_MATCH_EXTENDED", 4 );
  35. /// known sort modes
  36. define ( "SPH_SORT_RELEVANCE", 0 );
  37. define ( "SPH_SORT_ATTR_DESC", 1 );
  38. define ( "SPH_SORT_ATTR_ASC", 2 );
  39. define ( "SPH_SORT_TIME_SEGMENTS", 3 );
  40. define ( "SPH_SORT_EXTENDED", 4 );
  41. /// known attribute types
  42. define ( "SPH_ATTR_INTEGER", 1 );
  43. define ( "SPH_ATTR_TIMESTAMP", 2 );
  44. /// known grouping functions
  45. define ( "SPH_GROUPBY_DAY", 0 );
  46. define ( "SPH_GROUPBY_WEEK", 1 );
  47. define ( "SPH_GROUPBY_MONTH", 2 );
  48. define ( "SPH_GROUPBY_YEAR", 3 );
  49. define ( "SPH_GROUPBY_ATTR", 4 );
  50. define ( "SPH_GROUPBY_ATTRPAIR", 5 );
  51. /// sphinx searchd client class
  52. class SphinxClient
  53. {
  54. var $_host; ///< searchd host (default is "localhost")
  55. var $_port; ///< searchd port (default is 3312)
  56. var $_offset; ///< how many records to seek from result-set start (default is 0)
  57. var $_limit; ///< how many records to return from result-set starting at offset (default is 20)
  58. var $_mode; ///< query matching mode (default is SPH_MATCH_ALL)
  59. var $_weights; ///< per-field weights (default is 1 for all fields)
  60. var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE)
  61. var $_sortby; ///< attribute to sort by (defualt is "")
  62. var $_min_id; ///< min ID to match (default is 0)
  63. var $_max_id; ///< max ID to match (default is UINT_MAX)
  64. var $_filters; ///< search filters
  65. var $_groupby; ///< group-by attribute name
  66. var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with)
  67. var $_groupsort; ///< group-by sorting clause (to sort groups in result set with)
  68. var $_maxmatches; ///< max matches to retrieve
  69. var $_cutoff; ///< cutoff to stop searching at (default is 0)
  70. var $_error; ///< last error message
  71. var $_warning; ///< last warning message
  72. /////////////////////////////////////////////////////////////////////////////
  73. // common stuff
  74. /////////////////////////////////////////////////////////////////////////////
  75. /// create a new client object and fill defaults
  76. function SphinxClient ()
  77. {
  78. $this->_host = "localhost";
  79. $this->_port = 3312;
  80. $this->_offset = 0;
  81. $this->_limit = 20;
  82. $this->_mode = SPH_MATCH_ALL;
  83. $this->_weights = array ();
  84. $this->_sort = SPH_SORT_RELEVANCE;
  85. $this->_sortby = "";
  86. $this->_min_id = 0;
  87. $this->_max_id = 0xFFFFFFFF;
  88. $this->_filters = array ();
  89. $this->_groupby = "";
  90. $this->_groupfunc = SPH_GROUPBY_DAY;
  91. $this->_groupsort = "@group desc";
  92. $this->_maxmatches = 1000;
  93. $this->_cutoff = 0;
  94. $this->_error = "";
  95. $this->_warning = "";
  96. }
  97. /// get last error message (string)
  98. function GetLastError ()
  99. {
  100. return $this->_error;
  101. }
  102. /// get last warning message (string)
  103. function GetLastWarning ()
  104. {
  105. return $this->_warning;
  106. }
  107. /// set searchd server
  108. function SetServer ( $host, $port )
  109. {
  110. assert ( is_string($host) );
  111. assert ( is_int($port) );
  112. $this->_host = $host;
  113. $this->_port = $port;
  114. }
  115. /////////////////////////////////////////////////////////////////////////////
  116. /// connect to searchd server
  117. function _Connect ()
  118. {
  119. if (!( $fp = @fsockopen ( $this->_host, $this->_port ) ) )
  120. {
  121. $this->_error = "connection to {$this->_host}:{$this->_port} failed";
  122. return false;
  123. }
  124. // check version
  125. list(,$v) = unpack ( "N*", fread ( $fp, 4 ) );
  126. $v = (int)$v;
  127. if ( $v<1 )
  128. {
  129. fclose ( $fp );
  130. $this->_error = "expected searchd protocol version 1+, got version '$v'";
  131. return false;
  132. }
  133. // all ok, send my version
  134. fwrite ( $fp, pack ( "N", 1 ) );
  135. return $fp;
  136. }
  137. /// get and check response packet from searchd server
  138. function _GetResponse ( $fp, $client_ver )
  139. {
  140. $header = fread ( $fp, 8 );
  141. list ( $status, $ver, $len ) = array_values ( unpack ( "n2a/Nb", $header ) );
  142. $response = "";
  143. $left = $len;
  144. while ( $left>0 && !feof($fp) )
  145. {
  146. $chunk = fread ( $fp, $left );
  147. if ( $chunk )
  148. {
  149. $response .= $chunk;
  150. $left -= strlen($chunk);
  151. }
  152. }
  153. fclose ( $fp );
  154. // check response
  155. $read = strlen ( $response );
  156. if ( !$response || $read!=$len )
  157. {
  158. $this->_error = $len
  159. ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)"
  160. : "received zero-sized searchd response";
  161. return false;
  162. }
  163. // check status
  164. if ( $status==SEARCHD_WARNING )
  165. {
  166. list(,$wlen) = unpack ( "N*", substr ( $response, 0, 4 ) );
  167. $this->_warning = substr ( $response, 4, $wlen );
  168. return substr ( $response, 4+$wlen );
  169. }
  170. if ( $status==SEARCHD_ERROR )
  171. {
  172. $this->_error = "searchd error: " . substr ( $response, 4 );
  173. return false;
  174. }
  175. if ( $status==SEARCHD_RETRY )
  176. {
  177. $this->_error = "temporary searchd error: " . substr ( $response, 4 );
  178. return false;
  179. }
  180. if ( $status!=SEARCHD_OK )
  181. {
  182. $this->_error = "unknown status code '$status'";
  183. return false;
  184. }
  185. // check version
  186. if ( $ver<$client_ver )
  187. {
  188. $this->_warning = sprintf ( "searchd command v.%d.%d older than client's v.%d.%d, some options might not work",
  189. $ver>>8, $ver&0xff, $client_ver>>8, $client_ver&0xff );
  190. }
  191. return $response;
  192. }
  193. /////////////////////////////////////////////////////////////////////////////
  194. // searching
  195. /////////////////////////////////////////////////////////////////////////////
  196. /// set offset and count into result set,
  197. /// and max-matches and cutoff to use while searching
  198. function SetLimits ( $offset, $limit, $max=0, $cutoff=0 )
  199. {
  200. assert ( is_int($offset) );
  201. assert ( is_int($limit) );
  202. assert ( $offset>=0 );
  203. assert ( $limit>0 );
  204. assert ( $max>=0 );
  205. $this->_offset = $offset;
  206. $this->_limit = $limit;
  207. if ( $max>0 )
  208. $this->_maxmatches = $max;
  209. if ( $cutoff>0 )
  210. $this->_cutoff = $cutoff;
  211. }
  212. /// set match mode
  213. function SetMatchMode ( $mode )
  214. {
  215. assert ( $mode==SPH_MATCH_ALL
  216. || $mode==SPH_MATCH_ANY
  217. || $mode==SPH_MATCH_PHRASE
  218. || $mode==SPH_MATCH_BOOLEAN
  219. || $mode==SPH_MATCH_EXTENDED );
  220. $this->_mode = $mode;
  221. }
  222. /// set matches sorting mode
  223. function SetSortMode ( $mode, $sortby="" )
  224. {
  225. assert (
  226. $mode==SPH_SORT_RELEVANCE ||
  227. $mode==SPH_SORT_ATTR_DESC ||
  228. $mode==SPH_SORT_ATTR_ASC ||
  229. $mode==SPH_SORT_TIME_SEGMENTS ||
  230. $mode==SPH_SORT_EXTENDED );
  231. assert ( is_string($sortby) );
  232. assert ( $mode==SPH_SORT_RELEVANCE || strlen($sortby)>0 );
  233. $this->_sort = $mode;
  234. $this->_sortby = $sortby;
  235. }
  236. /// set per-field weights
  237. function SetWeights ( $weights )
  238. {
  239. assert ( is_array($weights) );
  240. foreach ( $weights as $weight )
  241. assert ( is_int($weight) );
  242. $this->_weights = $weights;
  243. }
  244. /// set IDs range to match
  245. /// only match those records where document ID
  246. /// is beetwen $min and $max (including $min and $max)
  247. function SetIDRange ( $min, $max )
  248. {
  249. assert ( is_int($min) );
  250. assert ( is_int($max) );
  251. assert ( $min<=$max );
  252. $this->_min_id = $min;
  253. $this->_max_id = $max;
  254. }
  255. /// set values filter
  256. /// only match those records where $attribute column values
  257. /// are in specified set
  258. function SetFilter ( $attribute, $values, $exclude=false )
  259. {
  260. assert ( is_string($attribute) );
  261. assert ( is_array($values) );
  262. assert ( count($values) );
  263. if ( is_array($values) && count($values) )
  264. {
  265. foreach ( $values as $value )
  266. assert ( is_int($value) );
  267. $this->_filters[] = array ( "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values );
  268. }
  269. }
  270. /// set range filter
  271. /// only match those records where $attribute column value
  272. /// is beetwen $min and $max (including $min and $max)
  273. function SetFilterRange ( $attribute, $min, $max, $exclude=false )
  274. {
  275. assert ( is_string($attribute) );
  276. assert ( is_int($min) );
  277. assert ( is_int($max) );
  278. assert ( $min<=$max );
  279. $this->_filters[] = array ( "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  280. }
  281. /// set grouping attribute and function
  282. ///
  283. /// in grouping mode, all matches are assigned to different groups
  284. /// based on grouping function value.
  285. ///
  286. /// each group keeps track of the total match count, and the best match
  287. /// (in this group) according to current sorting function.
  288. ///
  289. /// the final result set contains one best match per group, with
  290. /// grouping function value and matches count attached.
  291. ///
  292. /// groups in result set could be sorted by any sorting clause,
  293. /// including both document attributes and the following special
  294. /// internal Sphinx attributes:
  295. ///
  296. /// - @id - match document ID;
  297. /// - @weight, @rank, @relevance - match weight;
  298. /// - @group - groupby function value;
  299. /// - @count - amount of matches in group.
  300. ///
  301. /// the default mode is to sort by groupby value in descending order,
  302. /// ie. by "@group desc".
  303. ///
  304. /// "total_found" would contain total amount of matching groups over
  305. /// the whole index.
  306. ///
  307. /// WARNING: grouping is done in fixed memory and thus its results
  308. /// are only approximate; so there might be more groups reported
  309. /// in total_found than actually present. @count might also
  310. /// be underestimated.
  311. ///
  312. /// for example, if sorting by relevance and grouping by "published"
  313. /// attribute with SPH_GROUPBY_DAY function, then the result set will
  314. /// contain one most relevant match per each day when there were any
  315. /// matches published, with day number and per-day match count attached,
  316. /// and sorted by day number in descending order (ie. recent days first).
  317. function SetGroupBy ( $attribute, $func, $groupsort="@group desc" )
  318. {
  319. assert ( is_string($attribute) );
  320. assert ( is_string($groupsort) );
  321. assert ( $func==SPH_GROUPBY_DAY
  322. || $func==SPH_GROUPBY_WEEK
  323. || $func==SPH_GROUPBY_MONTH
  324. || $func==SPH_GROUPBY_YEAR
  325. || $func==SPH_GROUPBY_ATTR
  326. || $func==SPH_GROUPBY_ATTRPAIR );
  327. $this->_groupby = $attribute;
  328. $this->_groupfunc = $func;
  329. $this->_groupsort = $groupsort;
  330. }
  331. /// connect to searchd server and run given search query
  332. ///
  333. /// $query is query string
  334. /// $index is index name to query, default is "*" which means to query all indexes
  335. ///
  336. /// returns false on failure
  337. /// returns hash which has the following keys on success:
  338. /// "matches"
  339. /// hash which maps found document_id to ( "weight", "group" ) hash
  340. /// "total"
  341. /// total amount of matches retrieved (upto SPH_MAX_MATCHES, see sphinx.h)
  342. /// "total_found"
  343. /// total amount of matching documents in index
  344. /// "time"
  345. /// search time
  346. /// "words"
  347. /// hash which maps query terms (stemmed!) to ( "docs", "hits" ) hash
  348. function Query ( $query, $index="*" )
  349. {
  350. if (!( $fp = $this->_Connect() ))
  351. return false;
  352. /////////////////
  353. // build request
  354. /////////////////
  355. $req = pack ( "NNNN", $this->_offset, $this->_limit, $this->_mode, $this->_sort ); // mode and limits
  356. $req .= pack ( "N", strlen($this->_sortby) ) . $this->_sortby;
  357. $req .= pack ( "N", strlen($query) ) . $query; // query itself
  358. $req .= pack ( "N", count($this->_weights) ); // weights
  359. foreach ( $this->_weights as $weight )
  360. $req .= pack ( "N", (int)$weight );
  361. $req .= pack ( "N", strlen($index) ) . $index; // indexes
  362. $req .= pack ( "NNN", 0, (int)$this->_min_id, (int)$this->_max_id ); // id32 range
  363. // filters
  364. $req .= pack ( "N", count($this->_filters) );
  365. foreach ( $this->_filters as $filter )
  366. {
  367. $req .= pack ( "N", strlen($filter["attr"]) ) . $filter["attr"];
  368. if ( isset($filter["values"]) )
  369. {
  370. $req .= pack ( "N", count($filter["values"]) );
  371. foreach ( $filter["values"] as $value )
  372. $req .= pack ( "N", $value );
  373. } else
  374. {
  375. $req .= pack ( "NNN", 0, $filter["min"], $filter["max"] );
  376. }
  377. $req .= pack ( "N", $filter["exclude"] );
  378. }
  379. // group-by clause, max-matches count, group-sort clause, cutoff count
  380. $req .= pack ( "NN", $this->_groupfunc, strlen($this->_groupby) ) . $this->_groupby;
  381. $req .= pack ( "N", $this->_maxmatches );
  382. $req .= pack ( "N", strlen($this->_groupsort) ) . $this->_groupsort;
  383. $req .= pack ( "N", $this->_cutoff );
  384. ////////////////////////////
  385. // send query, get response
  386. ////////////////////////////
  387. $len = strlen($req);
  388. $req = pack ( "nnN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len ) . $req; // add header
  389. fwrite ( $fp, $req, $len+8 );
  390. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ))
  391. return false;
  392. //////////////////
  393. // parse response
  394. //////////////////
  395. $result = array();
  396. $max = strlen($response); // protection from broken response
  397. // read schema
  398. $p = 0;
  399. $fields = array ();
  400. $attrs = array ();
  401. list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  402. while ( $nfields-->0 && $p<$max )
  403. {
  404. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  405. $fields[] = substr ( $response, $p, $len ); $p += $len;
  406. }
  407. $result["fields"] = $fields;
  408. list(,$nattrs) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  409. while ( $nattrs-->0 && $p<$max )
  410. {
  411. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  412. $attr = substr ( $response, $p, $len ); $p += $len;
  413. list(,$type) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  414. $attrs[$attr] = $type;
  415. }
  416. $result["attrs"] = $attrs;
  417. // read match count
  418. list(,$count) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  419. list(,$id64) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  420. // read matches
  421. while ( $count-->0 && $p<$max )
  422. {
  423. if ( $id64 )
  424. {
  425. list ( $dochi, $doclo, $weight ) = array_values ( unpack ( "N*N*N*",
  426. substr ( $response, $p, 12 ) ) );
  427. $p += 12;
  428. $doc = (((int)$dochi)<<32) + ((int)$doclo);
  429. } else
  430. {
  431. list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
  432. substr ( $response, $p, 8 ) ) );
  433. $p += 8;
  434. }
  435. $doc = sprintf ( "%u", $doc ); // workaround for php signed/unsigned braindamage
  436. $weight = sprintf ( "%u", $weight );
  437. $result["matches"][$doc]["weight"] = $weight;
  438. foreach ( $attrs as $attr=>$type )
  439. {
  440. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  441. $result["matches"][$doc]["attrs"][$attr] = sprintf ( "%u", $val );
  442. }
  443. }
  444. list ( $total, $total_found, $msecs, $words ) =
  445. array_values ( unpack ( "N*N*N*N*", substr ( $response, $p, 16 ) ) );
  446. $result["total"] = sprintf ( "%u", $total );
  447. $result["total_found"] = sprintf ( "%u", $total_found );
  448. $result["time"] = sprintf ( "%.3f", $msecs/1000 );
  449. $p += 16;
  450. while ( $words-->0 )
  451. {
  452. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  453. $word = substr ( $response, $p, $len ); $p += $len;
  454. list ( $docs, $hits ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
  455. $result["words"][$word] = array (
  456. "docs"=>sprintf ( "%u", $docs ),
  457. "hits"=>sprintf ( "%u", $hits ) );
  458. }
  459. return $result;
  460. }
  461. /////////////////////////////////////////////////////////////////////////////
  462. // excerpts generation
  463. /////////////////////////////////////////////////////////////////////////////
  464. /// connect to searchd server and generate exceprts from given documents
  465. ///
  466. /// $docs is an array of strings which represent the documents' contents
  467. /// $index is a string specifiying the index which settings will be used
  468. /// for stemming, lexing and case folding
  469. /// $words is a string which contains the words to highlight
  470. /// $opts is a hash which contains additional optional highlighting parameters:
  471. /// "before_match"
  472. /// a string to insert before a set of matching words, default is "<b>"
  473. /// "after_match"
  474. /// a string to insert after a set of matching words, default is "<b>"
  475. /// "chunk_separator"
  476. /// a string to insert between excerpts chunks, default is " ... "
  477. /// "limit"
  478. /// max excerpt size in symbols (codepoints), default is 256
  479. /// "around"
  480. /// how much words to highlight around each match, default is 5
  481. ///
  482. /// returns false on failure
  483. /// returns an array of string excerpts on success
  484. function BuildExcerpts ( $docs, $index, $words, $opts=array() )
  485. {
  486. assert ( is_array($docs) );
  487. assert ( is_string($index) );
  488. assert ( is_string($words) );
  489. assert ( is_array($opts) );
  490. if (!( $fp = $this->_Connect() ))
  491. return false;
  492. /////////////////
  493. // fixup options
  494. /////////////////
  495. if ( !isset($opts["before_match"]) ) $opts["before_match"] = "<b>";
  496. if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>";
  497. if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... ";
  498. if ( !isset($opts["limit"]) ) $opts["limit"] = 256;
  499. if ( !isset($opts["around"]) ) $opts["around"] = 5;
  500. /////////////////
  501. // build request
  502. /////////////////
  503. // v.1.0 req
  504. $req = pack ( "NN", 0, 1 ); // mode=0, flags=1 (remove spaces)
  505. $req .= pack ( "N", strlen($index) ) . $index; // req index
  506. $req .= pack ( "N", strlen($words) ) . $words; // req words
  507. // options
  508. $req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
  509. $req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
  510. $req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
  511. $req .= pack ( "N", (int)$opts["limit"] );
  512. $req .= pack ( "N", (int)$opts["around"] );
  513. // documents
  514. $req .= pack ( "N", count($docs) );
  515. foreach ( $docs as $doc )
  516. {
  517. assert ( is_string($doc) );
  518. $req .= pack ( "N", strlen($doc) ) . $doc;
  519. }
  520. ////////////////////////////
  521. // send query, get response
  522. ////////////////////////////
  523. $len = strlen($req);
  524. $req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
  525. $wrote = fwrite ( $fp, $req, $len+8 );
  526. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ))
  527. return false;
  528. //////////////////
  529. // parse response
  530. //////////////////
  531. $pos = 0;
  532. $res = array ();
  533. $rlen = strlen($response);
  534. for ( $i=0; $i<count($docs); $i++ )
  535. {
  536. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  537. $pos += 4;
  538. if ( $pos+$len > $rlen )
  539. {
  540. $this->_error = "incomplete reply";
  541. return false;
  542. }
  543. $res[] = substr ( $response, $pos, $len );
  544. $pos += $len;
  545. }
  546. return $res;
  547. }
  548. /////////////////////////////////////////////////////////////////////////////
  549. // attribute updates
  550. /////////////////////////////////////////////////////////////////////////////
  551. /// update specified attributes on specified documents
  552. ///
  553. /// $index is a name of the index to be updated
  554. /// $attrs is an array of attribute name strings
  555. /// $values is a hash where key is document id, and value is an array of
  556. /// new attribute values
  557. ///
  558. /// returns number of actually updated documents (0 or more) on success
  559. /// returns -1 on failure
  560. ///
  561. /// usage example:
  562. /// $cl->UpdateAttributes ( array("group"), array(123=>array(456)) );
  563. function UpdateAttributes ( $index, $attrs, $values )
  564. {
  565. // verify everything
  566. assert ( is_string($index) );
  567. assert ( is_array($attrs) );
  568. foreach ( $attrs as $attr )
  569. assert ( is_string($attr) );
  570. assert ( is_array($values) );
  571. foreach ( $values as $id=>$entry )
  572. {
  573. assert ( is_int($id) );
  574. assert ( is_array($entry) );
  575. assert ( count($entry)==count($attrs) );
  576. foreach ( $entry as $v )
  577. assert ( is_int($v) );
  578. }
  579. // build request
  580. $req = pack ( "N", strlen($index) ) . $index;
  581. $req .= pack ( "N", count($attrs) );
  582. foreach ( $attrs as $attr )
  583. $req .= pack ( "N", strlen($attr) ) . $attr;
  584. $req .= pack ( "N", count($values) );
  585. foreach ( $values as $id=>$entry )
  586. {
  587. $req .= pack ( "N", $id );
  588. foreach ( $entry as $v )
  589. $req .= pack ( "N", $v );
  590. }
  591. // connect, send query, get response
  592. if (!( $fp = $this->_Connect() ))
  593. return -1;
  594. $len = strlen($req);
  595. $req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
  596. fwrite ( $fp, $req, $len+8 );
  597. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
  598. return -1;
  599. // parse response
  600. list(,$updated) = unpack ( "N*", substr ( $response, $p, 4 ) );
  601. return $updated;
  602. }
  603. }
  604. //
  605. // $Id$
  606. //
  607. ?>