sphinxapi.php 20 KB

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