SphinxClient.java 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*
  2. * $Id$
  3. *
  4. * Java version of Sphinx searchd client (Java API)
  5. *
  6. * Copyright (c) 2007, Vladimir Fedorkov
  7. * Copyright (c) 2007-2014, Andrew Aksyonoff
  8. * Copyright (c) 2008-2014, Sphinx Technologies Inc
  9. * All rights reserved
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License. You should have
  13. * received a copy of the GPL license along with this program; if you
  14. * did not, you can find it at http://www.gnu.org/
  15. */
  16. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  17. // WARNING
  18. // We strongly recommend you to use SphinxQL instead of the API
  19. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  20. package org.sphx.api;
  21. import java.io.*;
  22. import java.net.*;
  23. import java.util.*;
  24. import java.net.SocketAddress.*;
  25. /** Sphinx client class */
  26. public class SphinxClient
  27. {
  28. /* matching modes */
  29. public final static int SPH_MATCH_ALL = 0;
  30. public final static int SPH_MATCH_ANY = 1;
  31. public final static int SPH_MATCH_PHRASE = 2;
  32. public final static int SPH_MATCH_BOOLEAN = 3;
  33. public final static int SPH_MATCH_EXTENDED = 4;
  34. public final static int SPH_MATCH_FULLSCAN = 5;
  35. public final static int SPH_MATCH_EXTENDED2 = 6;
  36. /* ranking modes (extended2 only) */
  37. public final static int SPH_RANK_PROXIMITY_BM25 = 0;
  38. public final static int SPH_RANK_BM25 = 1;
  39. public final static int SPH_RANK_NONE = 2;
  40. public final static int SPH_RANK_WORDCOUNT = 3;
  41. public final static int SPH_RANK_PROXIMITY = 4;
  42. public final static int SPH_RANK_MATCHANY = 5;
  43. public final static int SPH_RANK_FIELDMASK = 6;
  44. public final static int SPH_RANK_SPH04 = 7;
  45. public final static int SPH_RANK_EXPR = 8;
  46. public final static int SPH_RANK_TOTAL = 9;
  47. /* sorting modes */
  48. public final static int SPH_SORT_RELEVANCE = 0;
  49. public final static int SPH_SORT_ATTR_DESC = 1;
  50. public final static int SPH_SORT_ATTR_ASC = 2;
  51. public final static int SPH_SORT_TIME_SEGMENTS = 3;
  52. public final static int SPH_SORT_EXTENDED = 4;
  53. public final static int SPH_SORT_EXPR = 5;
  54. /* grouping functions */
  55. public final static int SPH_GROUPBY_DAY = 0;
  56. public final static int SPH_GROUPBY_WEEK = 1;
  57. public final static int SPH_GROUPBY_MONTH = 2;
  58. public final static int SPH_GROUPBY_YEAR = 3;
  59. public final static int SPH_GROUPBY_ATTR = 4;
  60. public final static int SPH_GROUPBY_ATTRPAIR = 5;
  61. /* searchd reply status codes */
  62. public final static int SEARCHD_OK = 0;
  63. public final static int SEARCHD_ERROR = 1;
  64. public final static int SEARCHD_RETRY = 2;
  65. public final static int SEARCHD_WARNING = 3;
  66. /* attribute types */
  67. public final static int SPH_ATTR_INTEGER = 1;
  68. public final static int SPH_ATTR_TIMESTAMP = 2;
  69. public final static int SPH_ATTR_ORDINAL = 3;
  70. public final static int SPH_ATTR_BOOL = 4;
  71. public final static int SPH_ATTR_FLOAT = 5;
  72. public final static int SPH_ATTR_BIGINT = 6;
  73. public final static int SPH_ATTR_STRING = 7;
  74. public final static int SPH_ATTR_MULTI = 0x40000001;
  75. public final static int SPH_ATTR_MULTI64 = 0x40000002;
  76. /* searchd commands */
  77. private final static int SEARCHD_COMMAND_SEARCH = 0;
  78. private final static int SEARCHD_COMMAND_EXCERPT = 1;
  79. private final static int SEARCHD_COMMAND_UPDATE = 2;
  80. private final static int SEARCHD_COMMAND_KEYWORDS = 3;
  81. private final static int SEARCHD_COMMAND_PERSIST = 4;
  82. private final static int SEARCHD_COMMAND_FLUSHATTRS = 7;
  83. /* searchd command versions */
  84. private final static int VER_MAJOR_PROTO = 0x1;
  85. private final static int VER_COMMAND_SEARCH = 0x119;
  86. private final static int VER_COMMAND_EXCERPT = 0x102;
  87. private final static int VER_COMMAND_UPDATE = 0x103;
  88. private final static int VER_COMMAND_KEYWORDS = 0x100;
  89. private final static int VER_COMMAND_FLUSHATTRS = 0x100;
  90. /* filter types */
  91. private final static int SPH_FILTER_VALUES = 0;
  92. private final static int SPH_FILTER_RANGE = 1;
  93. private final static int SPH_FILTER_FLOATRANGE = 2;
  94. private String _host;
  95. private int _port;
  96. private String _path;
  97. private Socket _socket;
  98. private int _offset;
  99. private int _limit;
  100. private int _mode;
  101. private int[] _weights;
  102. private int _sort;
  103. private String _sortby;
  104. private int _minId;
  105. private int _maxId;
  106. private ByteArrayOutputStream _rawFilters;
  107. private DataOutputStream _filters;
  108. private int _filterCount;
  109. private String _groupBy;
  110. private int _groupFunc;
  111. private String _groupSort;
  112. private String _groupDistinct;
  113. private int _maxMatches;
  114. private int _cutoff;
  115. private int _retrycount;
  116. private int _retrydelay;
  117. private String _latitudeAttr;
  118. private String _longitudeAttr;
  119. private float _latitude;
  120. private float _longitude;
  121. private String _error;
  122. private String _warning;
  123. private boolean _connerror;
  124. private int _timeout;
  125. private ArrayList _reqs;
  126. private Map _indexWeights;
  127. private int _ranker;
  128. private String _rankexpr;
  129. private int _maxQueryTime;
  130. private Map _fieldWeights;
  131. private Map _overrideTypes;
  132. private Map _overrideValues;
  133. private String _select;
  134. /** Creates a new SphinxClient instance. */
  135. public SphinxClient()
  136. {
  137. this("localhost", 9312);
  138. }
  139. /** Creates a new SphinxClient instance, with host:port specification. */
  140. public SphinxClient(String host, int port)
  141. {
  142. _host = host;
  143. _port = port;
  144. _path = null;
  145. _socket = null;
  146. _offset = 0;
  147. _limit = 20;
  148. _mode = SPH_MATCH_EXTENDED2;
  149. _sort = SPH_SORT_RELEVANCE;
  150. _sortby = "";
  151. _minId = 0;
  152. _maxId = 0;
  153. _filterCount = 0;
  154. _rawFilters = new ByteArrayOutputStream();
  155. _filters = new DataOutputStream(_rawFilters);
  156. _groupBy = "";
  157. _groupFunc = SPH_GROUPBY_DAY;
  158. _groupSort = "@group desc";
  159. _groupDistinct = "";
  160. _maxMatches = 1000;
  161. _cutoff = 0;
  162. _retrycount = 0;
  163. _retrydelay = 0;
  164. _latitudeAttr = null;
  165. _longitudeAttr = null;
  166. _latitude = 0;
  167. _longitude = 0;
  168. _error = "";
  169. _warning = "";
  170. _connerror = false;
  171. _timeout = 1000;
  172. _reqs = new ArrayList();
  173. _weights = null;
  174. _indexWeights = new LinkedHashMap();
  175. _fieldWeights = new LinkedHashMap();
  176. _ranker = SPH_RANK_PROXIMITY_BM25;
  177. _rankexpr = "";
  178. _overrideTypes = new LinkedHashMap();
  179. _overrideValues = new LinkedHashMap();
  180. _select = "*";
  181. }
  182. /** Get last error message, if any. */
  183. public String GetLastError()
  184. {
  185. return _error;
  186. }
  187. /** Get last warning message, if any. */
  188. public String GetLastWarning()
  189. {
  190. return _warning;
  191. }
  192. /** Get last error flag (to tell network connection errors from searchd errors or broken responses). */
  193. public boolean IsConnectError()
  194. {
  195. return _connerror;
  196. }
  197. /** Set searchd host and port to connect to. */
  198. public void SetServer(String host, int port) throws SphinxException
  199. {
  200. myAssert ( host!=null && host.length()>0, "host name must not be empty" );
  201. myAssert ( port>0 && port<65536, "port must be in 1..65535 range" );
  202. _host = host;
  203. _port = port;
  204. }
  205. /** Set server connection timeout (0 to remove), in milliseconds. */
  206. public void SetConnectTimeout ( int timeout )
  207. {
  208. _timeout = Math.max ( timeout, 0 );
  209. }
  210. /** Internal method. Sanity check. */
  211. private void myAssert ( boolean condition, String err ) throws SphinxException
  212. {
  213. if ( !condition )
  214. {
  215. _error = err;
  216. throw new SphinxException ( err );
  217. }
  218. }
  219. /** Internal method. String IO helper. */
  220. private static void writeNetUTF8 ( DataOutputStream ostream, String str ) throws IOException
  221. {
  222. if ( str==null )
  223. {
  224. ostream.writeInt ( 0 );
  225. return;
  226. }
  227. byte[] sBytes = str.getBytes ( "UTF-8" );
  228. int iLen = sBytes.length;
  229. ostream.writeInt ( iLen );
  230. ostream.write ( sBytes );
  231. }
  232. /** Internal method. String IO helper. */
  233. private static String readNetUTF8(DataInputStream istream) throws IOException
  234. {
  235. int iLen = istream.readInt();
  236. byte[] sBytes = new byte [ iLen ];
  237. istream.readFully ( sBytes );
  238. return new String ( sBytes, "UTF-8");
  239. }
  240. /** Internal method. Unsigned int IO helper. */
  241. private static long readDword ( DataInputStream istream ) throws IOException
  242. {
  243. long v = (long) istream.readInt ();
  244. if ( v<0 )
  245. v += 4294967296L;
  246. return v;
  247. }
  248. /** Internal method. Connect to searchd and exchange versions. */
  249. private Socket _Connect()
  250. {
  251. if ( _socket!=null )
  252. return _socket;
  253. _connerror = false;
  254. Socket sock = null;
  255. try
  256. {
  257. sock = new Socket ();
  258. sock.setSoTimeout ( _timeout );
  259. InetSocketAddress addr = new InetSocketAddress ( _host, _port );
  260. sock.connect ( addr, _timeout );
  261. DataInputStream sIn = new DataInputStream ( sock.getInputStream() );
  262. int version = sIn.readInt();
  263. if ( version<1 )
  264. {
  265. sock.close ();
  266. _error = "expected searchd protocol version 1+, got version " + version;
  267. return null;
  268. }
  269. DataOutputStream sOut = new DataOutputStream ( sock.getOutputStream() );
  270. sOut.writeInt ( VER_MAJOR_PROTO );
  271. } catch ( IOException e )
  272. {
  273. _error = "connection to " + _host + ":" + _port + " failed: " + e;
  274. _connerror = true;
  275. try
  276. {
  277. if ( sock!=null )
  278. sock.close ();
  279. } catch ( IOException e1 ) {}
  280. return null;
  281. }
  282. return sock;
  283. }
  284. /** Internal method. Get and check response packet from searchd. */
  285. private byte[] _GetResponse ( Socket sock )
  286. {
  287. /* connect */
  288. DataInputStream sIn = null;
  289. InputStream SockInput = null;
  290. try
  291. {
  292. SockInput = sock.getInputStream();
  293. sIn = new DataInputStream ( SockInput );
  294. } catch ( IOException e )
  295. {
  296. _error = "getInputStream() failed: " + e;
  297. return null;
  298. }
  299. /* read response */
  300. byte[] response = null;
  301. short status = 0, ver = 0;
  302. int len = 0;
  303. try
  304. {
  305. /* read status fields */
  306. status = sIn.readShort();
  307. ver = sIn.readShort();
  308. len = sIn.readInt();
  309. /* read response if non-empty */
  310. if ( len<=0 )
  311. {
  312. _error = "invalid response packet size (len=" + len + ")";
  313. return null;
  314. }
  315. response = new byte[len];
  316. sIn.readFully ( response, 0, len );
  317. /* check status */
  318. if ( status==SEARCHD_WARNING )
  319. {
  320. DataInputStream in = new DataInputStream ( new ByteArrayInputStream ( response ) );
  321. int iWarnLen = in.readInt ();
  322. _warning = new String ( response, 4, iWarnLen );
  323. System.arraycopy ( response, 4+iWarnLen, response, 0, response.length-4-iWarnLen );
  324. } else if ( status==SEARCHD_ERROR )
  325. {
  326. _error = "searchd error: " + new String ( response, 4, response.length-4 );
  327. return null;
  328. } else if ( status==SEARCHD_RETRY )
  329. {
  330. _error = "temporary searchd error: " + new String ( response, 4, response.length-4 );
  331. return null;
  332. } else if ( status!=SEARCHD_OK )
  333. {
  334. _error = "searched returned unknown status, code=" + status;
  335. return null;
  336. }
  337. } catch ( IOException e )
  338. {
  339. if ( len!=0 )
  340. {
  341. /* get trace, to provide even more failure details */
  342. PrintWriter ew = new PrintWriter ( new StringWriter() );
  343. e.printStackTrace ( ew );
  344. ew.flush ();
  345. ew.close ();
  346. String sTrace = ew.toString ();
  347. /* build error message */
  348. _error = "failed to read searchd response (status=" + status + ", ver=" + ver + ", len=" + len + ", trace=" + sTrace +")";
  349. } else
  350. {
  351. _error = "received zero-sized searchd response (searchd crashed?): " + e.getMessage();
  352. }
  353. return null;
  354. } finally
  355. {
  356. if ( _socket==null )
  357. {
  358. try
  359. {
  360. if ( sIn!=null )
  361. sIn.close();
  362. if ( sock!=null && !sock.isConnected() )
  363. sock.close();
  364. } catch ( IOException e )
  365. {
  366. /* silently ignore close failures; nothing could be done anyway */
  367. }
  368. }
  369. }
  370. return response;
  371. }
  372. /** Internal method. Connect to searchd, send request, get response as DataInputStream. */
  373. private DataInputStream _DoRequest ( int command, int version, ByteArrayOutputStream req )
  374. {
  375. /* connect */
  376. Socket sock = _Connect();
  377. if ( sock==null )
  378. return null;
  379. /* send request */
  380. byte[] reqBytes = req.toByteArray();
  381. try
  382. {
  383. DataOutputStream sockDS = new DataOutputStream ( sock.getOutputStream() );
  384. sockDS.writeShort ( command );
  385. sockDS.writeShort ( version );
  386. sockDS.writeInt ( reqBytes.length );
  387. sockDS.write ( reqBytes );
  388. } catch ( Exception e )
  389. {
  390. _error = "network error: " + e;
  391. _connerror = true;
  392. return null;
  393. }
  394. /* get response */
  395. byte[] response = _GetResponse ( sock );
  396. if ( response==null )
  397. return null;
  398. /* spawn that tampon */
  399. return new DataInputStream ( new ByteArrayInputStream ( response ) );
  400. }
  401. /** Set matches offset and limit to return to client, max matches to retrieve on server, and cutoff. */
  402. public void SetLimits ( int offset, int limit, int max, int cutoff ) throws SphinxException
  403. {
  404. myAssert ( offset>=0, "offset must not be negative" );
  405. myAssert ( limit>0, "limit must be positive" );
  406. myAssert ( max>0, "max must be positive" );
  407. myAssert ( cutoff>=0, "cutoff must not be negative" );
  408. _offset = offset;
  409. _limit = limit;
  410. _maxMatches = max;
  411. _cutoff = cutoff;
  412. }
  413. /** Set matches offset and limit to return to client, and max matches to retrieve on server. */
  414. public void SetLimits ( int offset, int limit, int max ) throws SphinxException
  415. {
  416. SetLimits ( offset, limit, max, _cutoff );
  417. }
  418. /** Set matches offset and limit to return to client. */
  419. public void SetLimits ( int offset, int limit) throws SphinxException
  420. {
  421. SetLimits ( offset, limit, _maxMatches, _cutoff );
  422. }
  423. /** Set maximum query time, in milliseconds, per-index, 0 means "do not limit". */
  424. public void SetMaxQueryTime ( int maxTime ) throws SphinxException
  425. {
  426. myAssert ( maxTime>=0, "max_query_time must not be negative" );
  427. _maxQueryTime = maxTime;
  428. }
  429. /** Set matching mode. DEPRECATED */
  430. public void SetMatchMode(int mode) throws SphinxException
  431. {
  432. System.out.println ( "DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API\n" );
  433. myAssert (
  434. mode==SPH_MATCH_ALL ||
  435. mode==SPH_MATCH_ANY ||
  436. mode==SPH_MATCH_PHRASE ||
  437. mode==SPH_MATCH_BOOLEAN ||
  438. mode==SPH_MATCH_EXTENDED ||
  439. mode==SPH_MATCH_FULLSCAN ||
  440. mode==SPH_MATCH_EXTENDED2, "unknown mode value; use one of the SPH_MATCH_xxx constants" );
  441. _mode = mode;
  442. }
  443. /** Set ranking mode. */
  444. public void SetRankingMode ( int ranker, String rankexpr ) throws SphinxException
  445. {
  446. myAssert ( ranker>=0 && ranker<SPH_RANK_TOTAL, "unknown ranker value; use one of the SPH_RANK_xxx constants" );
  447. _rankexpr = ( rankexpr==null ) ? "" : rankexpr;
  448. _ranker = ranker;
  449. }
  450. /** Set sorting mode. */
  451. public void SetSortMode ( int mode, String sortby ) throws SphinxException
  452. {
  453. myAssert (
  454. mode==SPH_SORT_RELEVANCE ||
  455. mode==SPH_SORT_ATTR_DESC ||
  456. mode==SPH_SORT_ATTR_ASC ||
  457. mode==SPH_SORT_TIME_SEGMENTS ||
  458. mode==SPH_SORT_EXTENDED ||
  459. mode==SPH_SORT_EXPR, "unknown mode value; use one of the available SPH_SORT_xxx constants" );
  460. myAssert ( mode==SPH_SORT_RELEVANCE || ( sortby!=null && sortby.length()>0 ), "sortby string must not be empty in selected mode" );
  461. _sort = mode;
  462. _sortby = ( sortby==null ) ? "" : sortby;
  463. }
  464. /** Set per-field weights (all values must be positive). WARNING: DEPRECATED, use SetFieldWeights() instead. */
  465. public void SetWeights(int[] weights) throws SphinxException
  466. {
  467. myAssert ( weights!=null, "weights must not be null" );
  468. for (int i = 0; i < weights.length; i++) {
  469. int weight = weights[i];
  470. myAssert ( weight>0, "all weights must be greater than 0" );
  471. }
  472. _weights = weights;
  473. }
  474. /**
  475. * Bind per-field weights by field name.
  476. * @param fieldWeights hash which maps String index names to Integer weights
  477. */
  478. public void SetFieldWeights ( Map fieldWeights ) throws SphinxException
  479. {
  480. /* FIXME! implement checks here */
  481. _fieldWeights = ( fieldWeights==null ) ? new LinkedHashMap () : fieldWeights;
  482. }
  483. /**
  484. * Bind per-index weights by index name (and enable summing the weights on duplicate matches, instead of replacing them).
  485. * @param indexWeights hash which maps String index names to Integer weights
  486. */
  487. public void SetIndexWeights ( Map indexWeights ) throws SphinxException
  488. {
  489. /* FIXME! implement checks here */
  490. _indexWeights = ( indexWeights==null ) ? new LinkedHashMap () : indexWeights;
  491. }
  492. /** Set document IDs range to match. */
  493. public void SetIDRange ( int min, int max ) throws SphinxException
  494. {
  495. myAssert ( min<=max, "min must be less or equal to max" );
  496. _minId = min;
  497. _maxId = max;
  498. }
  499. /** Set values filter. Only match records where attribute value is in given set. */
  500. public void SetFilter ( String attribute, int[] values, boolean exclude ) throws SphinxException
  501. {
  502. myAssert ( values!=null && values.length>0, "values array must not be null or empty" );
  503. myAssert ( attribute!=null && attribute.length()>0, "attribute name must not be null or empty" );
  504. try
  505. {
  506. writeNetUTF8 ( _filters, attribute );
  507. _filters.writeInt ( SPH_FILTER_VALUES );
  508. _filters.writeInt ( values.length );
  509. for ( int i=0; i<values.length; i++ )
  510. _filters.writeLong ( values[i] );
  511. _filters.writeInt ( exclude ? 1 : 0 );
  512. } catch ( Exception e )
  513. {
  514. myAssert ( false, "IOException: " + e.getMessage() );
  515. }
  516. _filterCount++;
  517. }
  518. /** Set values filter. Only match records where attribute value is in given set. */
  519. public void SetFilter ( String attribute, long[] values, boolean exclude ) throws SphinxException
  520. {
  521. myAssert ( values!=null && values.length>0, "values array must not be null or empty" );
  522. myAssert ( attribute!=null && attribute.length()>0, "attribute name must not be null or empty" );
  523. try
  524. {
  525. writeNetUTF8 ( _filters, attribute );
  526. _filters.writeInt ( SPH_FILTER_VALUES );
  527. _filters.writeInt ( values.length );
  528. for ( int i=0; i<values.length; i++ )
  529. _filters.writeLong ( values[i] );
  530. _filters.writeInt ( exclude ? 1 : 0 );
  531. } catch ( Exception e )
  532. {
  533. myAssert ( false, "IOException: " + e.getMessage() );
  534. }
  535. _filterCount++;
  536. }
  537. /** Set values filter with a single value (syntax sugar; see {@link #SetFilter(String,int[],boolean)}). */
  538. public void SetFilter ( String attribute, int value, boolean exclude ) throws SphinxException
  539. {
  540. long[] values = new long[] { value };
  541. SetFilter ( attribute, values, exclude );
  542. }
  543. /** Set values filter with a single value (syntax sugar; see {@link #SetFilter(String,int[],boolean)}). */
  544. public void SetFilter ( String attribute, long value, boolean exclude ) throws SphinxException
  545. {
  546. long[] values = new long[] { value };
  547. SetFilter ( attribute, values, exclude );
  548. }
  549. /** Set integer range filter. Only match records if attribute value is beetwen min and max (inclusive). */
  550. public void SetFilterRange ( String attribute, long min, long max, boolean exclude ) throws SphinxException
  551. {
  552. myAssert ( min<=max, "min must be less or equal to max" );
  553. try
  554. {
  555. writeNetUTF8 ( _filters, attribute );
  556. _filters.writeInt ( SPH_FILTER_RANGE );
  557. _filters.writeLong ( min );
  558. _filters.writeLong ( max );
  559. _filters.writeInt ( exclude ? 1 : 0 );
  560. } catch ( Exception e )
  561. {
  562. myAssert ( false, "IOException: " + e.getMessage() );
  563. }
  564. _filterCount++;
  565. }
  566. /** Set integer range filter. Only match records if attribute value is beetwen min and max (inclusive). */
  567. public void SetFilterRange ( String attribute, int min, int max, boolean exclude ) throws SphinxException
  568. {
  569. SetFilterRange ( attribute, (long)min, (long)max, exclude );
  570. }
  571. /** Set float range filter. Only match records if attribute value is beetwen min and max (inclusive). */
  572. public void SetFilterFloatRange ( String attribute, float min, float max, boolean exclude ) throws SphinxException
  573. {
  574. myAssert ( min<=max, "min must be less or equal to max" );
  575. try
  576. {
  577. writeNetUTF8 ( _filters, attribute );
  578. _filters.writeInt ( SPH_FILTER_FLOATRANGE );
  579. _filters.writeFloat ( min );
  580. _filters.writeFloat ( max );
  581. _filters.writeInt ( exclude ? 1 : 0 );
  582. } catch ( Exception e )
  583. {
  584. myAssert ( false, "IOException: " + e.getMessage() );
  585. }
  586. _filterCount++;
  587. }
  588. /** Setup geographical anchor point. Required to use @geodist in filters and sorting; distance will be computed to this point. */
  589. public void SetGeoAnchor ( String latitudeAttr, String longitudeAttr, float latitude, float longitude ) throws SphinxException
  590. {
  591. myAssert ( latitudeAttr!=null && latitudeAttr.length()>0, "longitudeAttr string must not be null or empty" );
  592. myAssert ( longitudeAttr!=null && longitudeAttr.length()>0, "longitudeAttr string must not be null or empty" );
  593. _latitudeAttr = latitudeAttr;
  594. _longitudeAttr = longitudeAttr;
  595. _latitude = latitude;
  596. _longitude = longitude;
  597. }
  598. /** Set grouping attribute and function. */
  599. public void SetGroupBy ( String attribute, int func, String groupsort ) throws SphinxException
  600. {
  601. myAssert (
  602. func==SPH_GROUPBY_DAY ||
  603. func==SPH_GROUPBY_WEEK ||
  604. func==SPH_GROUPBY_MONTH ||
  605. func==SPH_GROUPBY_YEAR ||
  606. func==SPH_GROUPBY_ATTR ||
  607. func==SPH_GROUPBY_ATTRPAIR, "unknown func value; use one of the available SPH_GROUPBY_xxx constants" );
  608. _groupBy = attribute;
  609. _groupFunc = func;
  610. _groupSort = groupsort;
  611. }
  612. /** Set grouping attribute and function with default ("@group desc") groupsort (syntax sugar). */
  613. public void SetGroupBy(String attribute, int func) throws SphinxException
  614. {
  615. SetGroupBy(attribute, func, "@group desc");
  616. }
  617. /** Set count-distinct attribute for group-by queries. */
  618. public void SetGroupDistinct(String attribute)
  619. {
  620. _groupDistinct = attribute;
  621. }
  622. /** Set distributed retries count and delay. */
  623. public void SetRetries ( int count, int delay ) throws SphinxException
  624. {
  625. myAssert ( count>=0, "count must not be negative" );
  626. myAssert ( delay>=0, "delay must not be negative" );
  627. _retrycount = count;
  628. _retrydelay = delay;
  629. }
  630. /** Set distributed retries count with default (zero) delay (syntax sugar). */
  631. public void SetRetries ( int count ) throws SphinxException
  632. {
  633. SetRetries ( count, 0 );
  634. }
  635. /**
  636. * DEPRECATED: Set attribute values override (one override list per attribute).
  637. * @param values maps Long document IDs to Int/Long/Float values (as specified in attrtype).
  638. */
  639. public void SetOverride ( String attrname, int attrtype, Map values ) throws SphinxException
  640. {
  641. System.out.println ( "DEPRECATED: Do not call this method. Use SphinxQL REMAP() function instead.\n" );
  642. myAssert ( attrname!=null && attrname.length()>0, "attrname must not be empty" );
  643. myAssert ( attrtype==SPH_ATTR_INTEGER || attrtype==SPH_ATTR_TIMESTAMP || attrtype==SPH_ATTR_BOOL || attrtype==SPH_ATTR_FLOAT || attrtype==SPH_ATTR_BIGINT,
  644. "unsupported attrtype (must be one of INTEGER, TIMESTAMP, BOOL, FLOAT, or BIGINT)" );
  645. _overrideTypes.put ( attrname, new Integer ( attrtype ) );
  646. _overrideValues.put ( attrname, values );
  647. }
  648. /** Set select-list (attributes or expressions), SQL-like syntax. */
  649. public void SetSelect ( String select ) throws SphinxException
  650. {
  651. myAssert ( select!=null, "select clause string must not be null" );
  652. _select = select;
  653. }
  654. /** Reset all currently set filters (for multi-queries). */
  655. public void ResetFilters()
  656. {
  657. /* should we close them first? */
  658. _rawFilters = new ByteArrayOutputStream();
  659. _filters = new DataOutputStream(_rawFilters);
  660. _filterCount = 0;
  661. /* reset GEO anchor */
  662. _latitudeAttr = null;
  663. _longitudeAttr = null;
  664. _latitude = 0;
  665. _longitude = 0;
  666. }
  667. /** Clear groupby settings (for multi-queries). */
  668. public void ResetGroupBy ()
  669. {
  670. _groupBy = "";
  671. _groupFunc = SPH_GROUPBY_DAY;
  672. _groupSort = "@group desc";
  673. _groupDistinct = "";
  674. }
  675. /** Clear all attribute value overrides (for multi-queries). */
  676. public void ResetOverrides ()
  677. {
  678. _overrideTypes.clear ();
  679. _overrideValues.clear ();
  680. }
  681. /** Connect to searchd server and run current search query against all indexes (syntax sugar). */
  682. public SphinxResult Query ( String query ) throws SphinxException
  683. {
  684. return Query ( query, "*", "" );
  685. }
  686. /** Connect to searchd server and run current search query against all indexes (syntax sugar). */
  687. public SphinxResult Query ( String query, String index ) throws SphinxException
  688. {
  689. return Query ( query, index, "" );
  690. }
  691. /** Connect to searchd server and run current search query. */
  692. public SphinxResult Query ( String query, String index, String comment ) throws SphinxException
  693. {
  694. myAssert ( _reqs==null || _reqs.size()==0, "AddQuery() and Query() can not be combined; use RunQueries() instead" );
  695. AddQuery ( query, index, comment );
  696. SphinxResult[] results = RunQueries();
  697. _reqs = new ArrayList(); /* just in case it failed too early */
  698. if ( results==null || results.length<1 )
  699. return null; /* probably network error; error message should be already filled */
  700. SphinxResult res = results[0];
  701. _warning = res.warning;
  702. _error = res.error;
  703. if ( res==null || res.getStatus()==SEARCHD_ERROR )
  704. return null;
  705. return res;
  706. }
  707. /** Add new query with current settings to current search request. */
  708. public int AddQuery ( String query, String index, String comment ) throws SphinxException
  709. {
  710. ByteArrayOutputStream req = new ByteArrayOutputStream();
  711. /* build request */
  712. try {
  713. DataOutputStream out = new DataOutputStream(req);
  714. out.writeInt(_offset);
  715. out.writeInt(_limit);
  716. out.writeInt(_mode);
  717. out.writeInt(_ranker);
  718. if ( _ranker == SPH_RANK_EXPR ) {
  719. writeNetUTF8(out, _rankexpr);
  720. }
  721. out.writeInt(_sort);
  722. writeNetUTF8(out, _sortby);
  723. writeNetUTF8(out, query);
  724. int weightLen = _weights != null ? _weights.length : 0;
  725. out.writeInt(weightLen);
  726. if (_weights != null) {
  727. for (int i = 0; i < _weights.length; i++)
  728. out.writeInt(_weights[i]);
  729. }
  730. writeNetUTF8(out, index);
  731. out.writeInt(0);
  732. out.writeInt(_minId);
  733. out.writeInt(_maxId);
  734. /* filters */
  735. out.writeInt(_filterCount);
  736. out.write(_rawFilters.toByteArray());
  737. /* group-by, max matches, sort-by-group flag */
  738. out.writeInt(_groupFunc);
  739. writeNetUTF8(out, _groupBy);
  740. out.writeInt(_maxMatches);
  741. writeNetUTF8(out, _groupSort);
  742. out.writeInt(_cutoff);
  743. out.writeInt(_retrycount);
  744. out.writeInt(_retrydelay);
  745. writeNetUTF8(out, _groupDistinct);
  746. /* anchor point */
  747. if (_latitudeAttr == null || _latitudeAttr.length() == 0 || _longitudeAttr == null || _longitudeAttr.length() == 0) {
  748. out.writeInt(0);
  749. } else {
  750. out.writeInt(1);
  751. writeNetUTF8(out, _latitudeAttr);
  752. writeNetUTF8(out, _longitudeAttr);
  753. out.writeFloat(_latitude);
  754. out.writeFloat(_longitude);
  755. }
  756. /* per-index weights */
  757. out.writeInt(_indexWeights.size());
  758. for (Iterator e = _indexWeights.keySet().iterator(); e.hasNext();) {
  759. String indexName = (String) e.next();
  760. Integer weight = (Integer) _indexWeights.get(indexName);
  761. writeNetUTF8(out, indexName);
  762. out.writeInt(weight.intValue());
  763. }
  764. /* max query time */
  765. out.writeInt ( _maxQueryTime );
  766. /* per-field weights */
  767. out.writeInt ( _fieldWeights.size() );
  768. for ( Iterator e=_fieldWeights.keySet().iterator(); e.hasNext(); )
  769. {
  770. String field = (String) e.next();
  771. Integer weight = (Integer) _fieldWeights.get ( field );
  772. writeNetUTF8 ( out, field );
  773. out.writeInt ( weight.intValue() );
  774. }
  775. /* comment */
  776. writeNetUTF8 ( out, comment );
  777. /* overrides */
  778. out.writeInt ( _overrideTypes.size() );
  779. for ( Iterator e=_overrideTypes.keySet().iterator(); e.hasNext(); )
  780. {
  781. String attr = (String) e.next();
  782. Integer type = (Integer) _overrideTypes.get ( attr );
  783. Map values = (Map) _overrideValues.get ( attr );
  784. writeNetUTF8 ( out, attr );
  785. out.writeInt ( type.intValue() );
  786. out.writeInt ( values.size() );
  787. for ( Iterator e2=values.keySet().iterator(); e2.hasNext(); )
  788. {
  789. Long id = (Long) e2.next ();
  790. out.writeLong ( id.longValue() );
  791. switch ( type.intValue() )
  792. {
  793. case SPH_ATTR_FLOAT: out.writeFloat ( ( (Float) values.get ( id ) ).floatValue() ); break;
  794. case SPH_ATTR_BIGINT: out.writeLong ( ( (Long)values.get ( id ) ).longValue() ); break;
  795. default: out.writeInt ( ( (Integer)values.get ( id ) ).intValue() ); break;
  796. }
  797. }
  798. }
  799. /* select-list */
  800. writeNetUTF8 ( out, _select );
  801. /* done! */
  802. out.flush ();
  803. int qIndex = _reqs.size();
  804. _reqs.add ( qIndex, req.toByteArray() );
  805. return qIndex;
  806. } catch ( Exception e )
  807. {
  808. myAssert ( false, "error in AddQuery(): " + e + ": " + e.getMessage() );
  809. } finally
  810. {
  811. try
  812. {
  813. _filters.close ();
  814. _rawFilters.close ();
  815. } catch ( IOException e )
  816. {
  817. myAssert ( false, "error in AddQuery(): " + e + ": " + e.getMessage() );
  818. }
  819. }
  820. return -1;
  821. }
  822. /** Run all previously added search queries. */
  823. public SphinxResult[] RunQueries() throws SphinxException
  824. {
  825. if ( _reqs==null || _reqs.size()<1 )
  826. {
  827. _error = "no queries defined, issue AddQuery() first";
  828. return null;
  829. }
  830. /* build the mega-request */
  831. int nreqs = _reqs.size();
  832. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  833. try
  834. {
  835. DataOutputStream req = new DataOutputStream ( reqBuf );
  836. /* its a client */
  837. req.writeInt(0);
  838. req.writeInt ( nreqs );
  839. for ( int i=0; i<nreqs; i++ )
  840. req.write ( (byte[]) _reqs.get(i) );
  841. req.flush ();
  842. } catch ( Exception e )
  843. {
  844. _error = "internal error: failed to build request: " + e;
  845. return null;
  846. }
  847. DataInputStream in =_DoRequest ( SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, reqBuf );
  848. if ( in==null )
  849. return null;
  850. SphinxResult[] results = new SphinxResult [ nreqs ];
  851. _reqs = new ArrayList();
  852. try
  853. {
  854. for ( int ires=0; ires<nreqs; ires++ )
  855. {
  856. SphinxResult res = new SphinxResult();
  857. results[ires] = res;
  858. int status = in.readInt();
  859. res.setStatus ( status );
  860. if (status != SEARCHD_OK) {
  861. String message = readNetUTF8(in);
  862. if (status == SEARCHD_WARNING) {
  863. res.warning = message;
  864. } else {
  865. res.error = message;
  866. continue;
  867. }
  868. }
  869. /* read fields */
  870. int nfields = in.readInt();
  871. res.fields = new String[nfields];
  872. int pos = 0;
  873. for (int i = 0; i < nfields; i++)
  874. res.fields[i] = readNetUTF8(in);
  875. /* read arrts */
  876. int nattrs = in.readInt();
  877. res.attrTypes = new int[nattrs];
  878. res.attrNames = new String[nattrs];
  879. for (int i = 0; i < nattrs; i++) {
  880. String AttrName = readNetUTF8(in);
  881. int AttrType = in.readInt();
  882. res.attrNames[i] = AttrName;
  883. res.attrTypes[i] = AttrType;
  884. }
  885. /* read match count */
  886. int count = in.readInt();
  887. int id64 = in.readInt();
  888. res.matches = new SphinxMatch[count];
  889. for ( int matchesNo=0; matchesNo<count; matchesNo++ )
  890. {
  891. SphinxMatch docInfo;
  892. docInfo = new SphinxMatch (
  893. ( id64==0 ) ? readDword(in) : in.readLong(),
  894. in.readInt() );
  895. /* read matches */
  896. for (int attrNumber = 0; attrNumber < res.attrTypes.length; attrNumber++)
  897. {
  898. String attrName = res.attrNames[attrNumber];
  899. int type = res.attrTypes[attrNumber];
  900. /* handle bigints */
  901. if ( type==SPH_ATTR_BIGINT )
  902. {
  903. docInfo.attrValues.add ( attrNumber, new Long ( in.readLong() ) );
  904. continue;
  905. }
  906. /* handle floats */
  907. if ( type==SPH_ATTR_FLOAT )
  908. {
  909. docInfo.attrValues.add ( attrNumber, new Float ( in.readFloat() ) );
  910. continue;
  911. }
  912. /* handle strings */
  913. if ( type==SPH_ATTR_STRING )
  914. {
  915. String s = readNetUTF8(in);
  916. docInfo.attrValues.add ( attrNumber, s );
  917. continue;
  918. }
  919. /* handle everything else as unsigned ints */
  920. long val = readDword ( in );
  921. if ( type==SPH_ATTR_MULTI )
  922. {
  923. long[] vals = new long [ (int)val ];
  924. for ( int k=0; k<val; k++ )
  925. vals[k] = readDword ( in );
  926. docInfo.attrValues.add ( attrNumber, vals );
  927. } else if ( type==SPH_ATTR_MULTI64 )
  928. {
  929. val = val / 2;
  930. long[] vals = new long [ (int)val ];
  931. for ( int k=0; k<val; k++ )
  932. vals[k] = in.readLong ();
  933. docInfo.attrValues.add ( attrNumber, vals );
  934. } else
  935. {
  936. docInfo.attrValues.add ( attrNumber, new Long ( val ) );
  937. }
  938. }
  939. res.matches[matchesNo] = docInfo;
  940. }
  941. res.total = in.readInt();
  942. res.totalFound = in.readInt();
  943. res.time = in.readInt() / 1000.0f;
  944. res.words = new SphinxWordInfo [ in.readInt() ];
  945. for ( int i=0; i<res.words.length; i++ )
  946. res.words[i] = new SphinxWordInfo ( readNetUTF8(in), readDword(in), readDword(in) );
  947. }
  948. return results;
  949. } catch ( IOException e )
  950. {
  951. _error = "incomplete reply";
  952. return null;
  953. }
  954. }
  955. /**
  956. * Connect to searchd server and generate excerpts (snippets) from given documents.
  957. * @param opts maps String keys to String or Integer values (see the documentation for complete keys list).
  958. * @return null on failure, array of snippets on success.
  959. */
  960. public String[] BuildExcerpts ( String[] docs, String index, String words, Map opts ) throws SphinxException
  961. {
  962. myAssert(docs != null && docs.length > 0, "BuildExcerpts: Have no documents to process");
  963. myAssert(index != null && index.length() > 0, "BuildExcerpts: Have no index to process documents");
  964. myAssert(words != null && words.length() > 0, "BuildExcerpts: Have no words to highlight");
  965. if (opts == null) opts = new LinkedHashMap();
  966. /* fixup options */
  967. if (!opts.containsKey("before_match")) opts.put("before_match", "<b>");
  968. if (!opts.containsKey("after_match")) opts.put("after_match", "</b>");
  969. if (!opts.containsKey("chunk_separator")) opts.put("chunk_separator", "...");
  970. if (!opts.containsKey("html_strip_mode")) opts.put("html_strip_mode", "index");
  971. if (!opts.containsKey("limit")) opts.put("limit", new Integer(256));
  972. if (!opts.containsKey("limit_passages")) opts.put("limit_passages", new Integer(0));
  973. if (!opts.containsKey("limit_words")) opts.put("limit_words", new Integer(0));
  974. if (!opts.containsKey("around")) opts.put("around", new Integer(5));
  975. if (!opts.containsKey("start_passage_id")) opts.put("start_passage_id", new Integer(1));
  976. if (!opts.containsKey("exact_phrase")) opts.put("exact_phrase", new Integer(0));
  977. if (!opts.containsKey("single_passage")) opts.put("single_passage", new Integer(0));
  978. if (!opts.containsKey("use_boundaries")) opts.put("use_boundaries", new Integer(0));
  979. if (!opts.containsKey("weight_order")) opts.put("weight_order", new Integer(0));
  980. if (!opts.containsKey("load_files")) opts.put("load_files", new Integer(0));
  981. if (!opts.containsKey("allow_empty")) opts.put("allow_empty", new Integer(0));
  982. if (!opts.containsKey("query_mode")) opts.put("query_mode", new Integer(0));
  983. if (!opts.containsKey("force_all_words")) opts.put("force_all_words", new Integer(0));
  984. /* build request */
  985. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  986. DataOutputStream req = new DataOutputStream ( reqBuf );
  987. try
  988. {
  989. req.writeInt(0);
  990. int iFlags = 1; /* remove_spaces */
  991. if ( ((Integer)opts.get("exact_phrase")).intValue()!=0 ) iFlags |= 2;
  992. if ( ((Integer)opts.get("single_passage")).intValue()!=0 ) iFlags |= 4;
  993. if ( ((Integer)opts.get("use_boundaries")).intValue()!=0 ) iFlags |= 8;
  994. if ( ((Integer)opts.get("weight_order")).intValue()!=0 ) iFlags |= 16;
  995. if ( ((Integer)opts.get("query_mode")).intValue()!=0 ) iFlags |= 32;
  996. if ( ((Integer)opts.get("force_all_words")).intValue()!=0 ) iFlags |= 64;
  997. if ( ((Integer)opts.get("load_files")).intValue()!=0 ) iFlags |= 128;
  998. if ( ((Integer)opts.get("allow_empty")).intValue()!=0 ) iFlags |= 256;
  999. req.writeInt ( iFlags );
  1000. writeNetUTF8 ( req, index );
  1001. writeNetUTF8 ( req, words );
  1002. /* send options */
  1003. writeNetUTF8 ( req, (String) opts.get("before_match") );
  1004. writeNetUTF8 ( req, (String) opts.get("after_match") );
  1005. writeNetUTF8 ( req, (String) opts.get("chunk_separator") );
  1006. req.writeInt ( ((Integer) opts.get("limit")).intValue() );
  1007. req.writeInt ( ((Integer) opts.get("around")).intValue() );
  1008. req.writeInt ( ((Integer) opts.get("limit_passages")).intValue() );
  1009. req.writeInt ( ((Integer) opts.get("limit_words")).intValue() );
  1010. req.writeInt ( ((Integer) opts.get("start_passage_id")).intValue() );
  1011. writeNetUTF8 ( req, (String) opts.get("html_strip_mode") );
  1012. /* send documents */
  1013. req.writeInt ( docs.length );
  1014. for ( int i=0; i<docs.length; i++ )
  1015. writeNetUTF8 ( req, docs[i] );
  1016. req.flush();
  1017. } catch ( Exception e )
  1018. {
  1019. _error = "internal error: failed to build request: " + e;
  1020. return null;
  1021. }
  1022. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, reqBuf );
  1023. if ( in==null )
  1024. return null;
  1025. try
  1026. {
  1027. String[] res = new String [ docs.length ];
  1028. for ( int i=0; i<docs.length; i++ )
  1029. res[i] = readNetUTF8 ( in );
  1030. return res;
  1031. } catch ( Exception e )
  1032. {
  1033. _error = "incomplete reply";
  1034. return null;
  1035. }
  1036. }
  1037. /**
  1038. * Connect to searchd server and update given attributes on given documents in given indexes.
  1039. * Sample code that will set group_id=123 where id=1 and group_id=456 where id=3:
  1040. *
  1041. * <pre>
  1042. * String[] attrs = new String[1];
  1043. *
  1044. * attrs[0] = "group_id";
  1045. * long[][] values = new long[2][2];
  1046. *
  1047. * values[0] = new long[2]; values[0][0] = 1; values[0][1] = 123;
  1048. * values[1] = new long[2]; values[1][0] = 3; values[1][1] = 456;
  1049. *
  1050. * int res = cl.UpdateAttributes ( "test1", attrs, values );
  1051. * </pre>
  1052. *
  1053. * @param index index name(s) to update; might be distributed
  1054. * @param attrs array with the names of the attributes to update
  1055. * @param values array of updates; each long[] entry must contains document ID
  1056. * in the first element, and all new attribute values in the following ones
  1057. * @param ignorenonexistent the flag whether to silently ignore non existent columns up update request
  1058. * @return -1 on failure, amount of actually found and updated documents (might be 0) on success
  1059. *
  1060. * @throws SphinxException on invalid parameters
  1061. */
  1062. public int UpdateAttributes ( String index, String[] attrs, long[][] values, boolean ignorenonexistent ) throws SphinxException
  1063. {
  1064. /* check args */
  1065. myAssert ( index!=null && index.length()>0, "no index name provided" );
  1066. myAssert ( attrs!=null && attrs.length>0, "no attribute names provided" );
  1067. myAssert ( values!=null && values.length>0, "no update entries provided" );
  1068. for ( int i=0; i<values.length; i++ )
  1069. {
  1070. myAssert ( values[i]!=null, "update entry #" + i + " is null" );
  1071. myAssert ( values[i].length==1+attrs.length, "update entry #" + i + " has wrong length" );
  1072. }
  1073. /* build and send request */
  1074. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  1075. DataOutputStream req = new DataOutputStream ( reqBuf );
  1076. try
  1077. {
  1078. writeNetUTF8 ( req, index );
  1079. req.writeInt ( attrs.length );
  1080. req.writeInt ( ignorenonexistent ? 1 : 0 );
  1081. for ( int i=0; i<attrs.length; i++ )
  1082. {
  1083. writeNetUTF8 ( req, attrs[i] );
  1084. req.writeInt ( 0 ); // not MVA attr
  1085. }
  1086. req.writeInt ( values.length );
  1087. for ( int i=0; i<values.length; i++ )
  1088. {
  1089. req.writeLong ( values[i][0] ); /* send docid as 64bit value */
  1090. for ( int j=1; j<values[i].length; j++ )
  1091. req.writeInt ( (int)values[i][j] ); /* send values as 32bit values; FIXME! what happens when they are over 2^31? */
  1092. }
  1093. req.flush();
  1094. } catch ( Exception e )
  1095. {
  1096. _error = "internal error: failed to build request: " + e;
  1097. return -1;
  1098. }
  1099. /* get and parse response */
  1100. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, reqBuf );
  1101. if ( in==null )
  1102. return -1;
  1103. try
  1104. {
  1105. return in.readInt ();
  1106. } catch ( Exception e )
  1107. {
  1108. _error = "incomplete reply";
  1109. return -1;
  1110. }
  1111. }
  1112. /**
  1113. * Connect to searchd server and update given MVA attributes on given document in given indexes.
  1114. * Sample code that will set group_id=(123, 456, 789) where id=10
  1115. *
  1116. * <pre>
  1117. * String[] attrs = new String[1];
  1118. *
  1119. * attrs[0] = "group_id";
  1120. * int[][] values = new int[1][3];
  1121. *
  1122. * values[0] = new int[3]; values[0][0] = 123; values[0][1] = 456; values[0][2] = 789
  1123. *
  1124. * int res = cl.UpdateAttributesMVA ( "test1", 10, attrs, values );
  1125. * </pre>
  1126. *
  1127. * @param index index name(s) to update; might be distributed
  1128. * @param docid id of document to update
  1129. * @param attrs array with the names of the attributes to update
  1130. * @param values array of updates; each int[] entry must contains all new attribute values
  1131. * @param ignorenonexistent the flag whether to silently ignore non existent columns up update request
  1132. * @return -1 on failure, amount of actually found and updated documents (might be 0) on success
  1133. *
  1134. * @throws SphinxException on invalid parameters
  1135. */
  1136. public int UpdateAttributesMVA ( String index, long docid, String[] attrs, int[][] values, boolean ignorenonexistent ) throws SphinxException
  1137. {
  1138. /* check args */
  1139. myAssert ( index!=null && index.length()>0, "no index name provided" );
  1140. myAssert ( docid>0, "invalid document id" );
  1141. myAssert ( attrs!=null && attrs.length>0, "no attribute names provided" );
  1142. myAssert ( values!=null && values.length>0, "no update entries provided" );
  1143. myAssert ( values.length==attrs.length, "update entry has wrong length" );
  1144. for ( int i=0; i<values.length; i++ )
  1145. {
  1146. myAssert ( values[i]!=null, "update entry #" + i + " is null" );
  1147. }
  1148. /* build and send request */
  1149. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  1150. DataOutputStream req = new DataOutputStream ( reqBuf );
  1151. try
  1152. {
  1153. writeNetUTF8 ( req, index );
  1154. req.writeInt ( attrs.length );
  1155. req.writeInt ( ignorenonexistent ? 1 : 0 );
  1156. for ( int i=0; i<attrs.length; i++ )
  1157. {
  1158. writeNetUTF8 ( req, attrs[i] );
  1159. req.writeInt ( 1 ); // MVA attr
  1160. }
  1161. req.writeInt ( 1 );
  1162. req.writeLong ( docid ); /* send docid as 64bit value */
  1163. for ( int i=0; i<values.length; i++ )
  1164. {
  1165. req.writeInt ( values[i].length ); /* send MVA's count */
  1166. for ( int j=0; j<values[i].length; j++ ) /* send MVAs itself*/
  1167. req.writeInt ( values[i][j] );
  1168. }
  1169. req.flush();
  1170. } catch ( Exception e )
  1171. {
  1172. _error = "internal error: failed to build request: " + e;
  1173. return -1;
  1174. }
  1175. /* get and parse response */
  1176. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, reqBuf );
  1177. if ( in==null )
  1178. return -1;
  1179. try
  1180. {
  1181. return in.readInt ();
  1182. } catch ( Exception e )
  1183. {
  1184. _error = "incomplete reply";
  1185. return -1;
  1186. }
  1187. }
  1188. public int UpdateAttributes ( String index, String[] attrs, long[][] values ) throws SphinxException
  1189. {
  1190. return UpdateAttributes ( index, attrs, values, false );
  1191. }
  1192. public int UpdateAttributesMVA ( String index, long docid, String[] attrs, int[][] values ) throws SphinxException
  1193. {
  1194. return UpdateAttributesMVA ( index, docid, attrs, values, false );
  1195. }
  1196. /**
  1197. * Connect to searchd server, and generate keyword list for a given query.
  1198. * Returns null on failure, an array of Maps with misc per-keyword info on success.
  1199. */
  1200. public Map[] BuildKeywords ( String query, String index, boolean hits ) throws SphinxException
  1201. {
  1202. /* build request */
  1203. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  1204. DataOutputStream req = new DataOutputStream ( reqBuf );
  1205. try
  1206. {
  1207. writeNetUTF8 ( req, query );
  1208. writeNetUTF8 ( req, index );
  1209. req.writeInt ( hits ? 1 : 0 );
  1210. } catch ( Exception e )
  1211. {
  1212. _error = "internal error: failed to build request: " + e;
  1213. return null;
  1214. }
  1215. /* run request */
  1216. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, reqBuf );
  1217. if ( in==null )
  1218. return null;
  1219. /* parse reply */
  1220. try
  1221. {
  1222. int iNumWords = in.readInt ();
  1223. Map[] res = new Map[iNumWords];
  1224. for ( int i=0; i<iNumWords; i++ )
  1225. {
  1226. res[i] = new LinkedHashMap ();
  1227. res[i].put ( "tokenized", readNetUTF8 ( in ) );
  1228. res[i].put ( "normalized", readNetUTF8 ( in ) );
  1229. if ( hits )
  1230. {
  1231. res[i].put ( "docs", new Long ( readDword ( in ) ) );
  1232. res[i].put ( "hits", new Long ( readDword ( in ) ) );
  1233. }
  1234. }
  1235. return res;
  1236. } catch ( Exception e )
  1237. {
  1238. _error = "incomplete reply";
  1239. return null;
  1240. }
  1241. }
  1242. /**
  1243. * Force attribute flush, and block until it completes.
  1244. * Returns current internal flush tag on success, -1 on failure.
  1245. */
  1246. public int FlushAttributes() throws SphinxException
  1247. {
  1248. /* build request */
  1249. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  1250. /* run request */
  1251. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, reqBuf );
  1252. if ( in==null )
  1253. return -1;
  1254. /* parse reply */
  1255. try
  1256. {
  1257. int iFlushTag = in.readInt ();
  1258. return iFlushTag;
  1259. } catch ( Exception e )
  1260. {
  1261. _error = "incomplete reply";
  1262. return -1;
  1263. }
  1264. }
  1265. /** Escape the characters with special meaning in query syntax. */
  1266. static public String EscapeString ( String s )
  1267. {
  1268. return s.replaceAll ( "([\\(\\)\\|\\-\\!\\@\\~\\\\\\\"\\&\\/\\^\\$\\=])", "\\\\$1" );
  1269. }
  1270. /** Open persistent connection to searchd. */
  1271. public boolean Open()
  1272. {
  1273. if ( _socket!=null )
  1274. {
  1275. _error = "already connected";
  1276. return false;
  1277. }
  1278. Socket sock = _Connect();
  1279. if ( sock==null )
  1280. return false;
  1281. // command, command version = 0, body length = 4, body = 1
  1282. try
  1283. {
  1284. DataOutputStream sOut = new DataOutputStream ( sock.getOutputStream() );
  1285. sOut.writeShort ( SEARCHD_COMMAND_PERSIST );
  1286. sOut.writeShort ( 0 );
  1287. sOut.writeInt ( 4 );
  1288. sOut.writeInt ( 1 );
  1289. } catch ( IOException e )
  1290. {
  1291. _error = "network error: " + e;
  1292. _connerror = true;
  1293. }
  1294. _socket = sock;
  1295. return true;
  1296. }
  1297. /** Close existing persistent connection. */
  1298. public boolean Close()
  1299. {
  1300. if ( _socket==null )
  1301. {
  1302. _error = "not connected";
  1303. return false;
  1304. }
  1305. try
  1306. {
  1307. _socket.close();
  1308. } catch ( IOException e )
  1309. {}
  1310. _socket = null;
  1311. return true;
  1312. }
  1313. }
  1314. /*
  1315. * $Id$
  1316. */