SphinxClient.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * $Id$
  3. *
  4. * Java version of Sphinx searchd client (Java API)
  5. *
  6. * Copyright (c) 2007-2008, Andrew Aksyonoff
  7. * Copyright (c) 2007, Vladimir Fedorkov
  8. * All rights reserved
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License. You should have
  12. * received a copy of the GPL license along with this program; if you
  13. * did not, you can find it at http://www.gnu.org/
  14. */
  15. package org.sphx.api;
  16. import java.io.*;
  17. import java.net.*;
  18. import java.util.*;
  19. /** Sphinx client class */
  20. public class SphinxClient
  21. {
  22. /* matching modes */
  23. public final static int SPH_MATCH_ALL = 0;
  24. public final static int SPH_MATCH_ANY = 1;
  25. public final static int SPH_MATCH_PHRASE = 2;
  26. public final static int SPH_MATCH_BOOLEAN = 3;
  27. public final static int SPH_MATCH_EXTENDED = 4;
  28. public final static int SPH_MATCH_FULLSCAN = 5;
  29. public final static int SPH_MATCH_EXTENDED2 = 6;
  30. /* ranking modes (extended2 only) */
  31. public final static int SPH_RANK_PROXIMITY_BM25 = 0;
  32. public final static int SPH_RANK_BM25 = 1;
  33. public final static int SPH_RANK_NONE = 2;
  34. public final static int SPH_RANK_WORDCOUNT = 3;
  35. /* sorting modes */
  36. public final static int SPH_SORT_RELEVANCE = 0;
  37. public final static int SPH_SORT_ATTR_DESC = 1;
  38. public final static int SPH_SORT_ATTR_ASC = 2;
  39. public final static int SPH_SORT_TIME_SEGMENTS = 3;
  40. public final static int SPH_SORT_EXTENDED = 4;
  41. public final static int SPH_SORT_EXPR = 5;
  42. /* grouping functions */
  43. public final static int SPH_GROUPBY_DAY = 0;
  44. public final static int SPH_GROUPBY_WEEK = 1;
  45. public final static int SPH_GROUPBY_MONTH = 2;
  46. public final static int SPH_GROUPBY_YEAR = 3;
  47. public final static int SPH_GROUPBY_ATTR = 4;
  48. public final static int SPH_GROUPBY_ATTRPAIR = 5;
  49. /* searchd reply status codes */
  50. public final static int SEARCHD_OK = 0;
  51. public final static int SEARCHD_ERROR = 1;
  52. public final static int SEARCHD_RETRY = 2;
  53. public final static int SEARCHD_WARNING = 3;
  54. /* attribute types */
  55. public final static int SPH_ATTR_INTEGER = 1;
  56. public final static int SPH_ATTR_TIMESTAMP = 2;
  57. public final static int SPH_ATTR_ORDINAL = 3;
  58. public final static int SPH_ATTR_BOOL = 4;
  59. public final static int SPH_ATTR_FLOAT = 5;
  60. public final static int SPH_ATTR_MULTI = 0x40000000;
  61. /* searchd commands */
  62. private final static int SEARCHD_COMMAND_SEARCH = 0;
  63. private final static int SEARCHD_COMMAND_EXCERPT = 1;
  64. private final static int SEARCHD_COMMAND_UPDATE = 2;
  65. private final static int SEARCHD_COMMAND_KEYWORDS = 3;
  66. /* searchd command versions */
  67. private final static int VER_MAJOR_PROTO = 0x1;
  68. private final static int VER_COMMAND_SEARCH = 0x113;
  69. private final static int VER_COMMAND_EXCERPT = 0x100;
  70. private final static int VER_COMMAND_UPDATE = 0x101;
  71. private final static int VER_COMMAND_KEYWORDS = 0x100;
  72. /* filter types */
  73. private final static int SPH_FILTER_VALUES = 0;
  74. private final static int SPH_FILTER_RANGE = 1;
  75. private final static int SPH_FILTER_FLOATRANGE = 2;
  76. private String _host;
  77. private int _port;
  78. private int _offset;
  79. private int _limit;
  80. private int _mode;
  81. private int[] _weights;
  82. private int _sort;
  83. private String _sortby;
  84. private int _minId;
  85. private int _maxId;
  86. private ByteArrayOutputStream _rawFilters;
  87. private DataOutputStream _filters;
  88. private int _filterCount;
  89. private String _groupBy;
  90. private int _groupFunc;
  91. private String _groupSort;
  92. private String _groupDistinct;
  93. private int _maxMatches;
  94. private int _cutoff;
  95. private int _retrycount;
  96. private int _retrydelay;
  97. private String _latitudeAttr;
  98. private String _longitudeAttr;
  99. private float _latitude;
  100. private float _longitude;
  101. private String _error;
  102. private String _warning;
  103. private ArrayList _reqs;
  104. private Map _indexWeights;
  105. private int _ranker;
  106. private int _maxQueryTime;
  107. private Map _fieldWeights;
  108. private static final int SPH_CLIENT_TIMEOUT_MILLISEC = 30000;
  109. /** Creates a new SphinxClient instance. */
  110. public SphinxClient()
  111. {
  112. this("localhost", 3312);
  113. }
  114. /** Creates a new SphinxClient instance, with host:port specification. */
  115. public SphinxClient(String host, int port)
  116. {
  117. _host = host;
  118. _port = port;
  119. _offset = 0;
  120. _limit = 20;
  121. _mode = SPH_MATCH_ALL;
  122. _sort = SPH_SORT_RELEVANCE;
  123. _sortby = "";
  124. _minId = 0;
  125. _maxId = 0xFFFFFFFF;
  126. _filterCount = 0;
  127. _rawFilters = new ByteArrayOutputStream();
  128. _filters = new DataOutputStream(_rawFilters);
  129. _groupBy = "";
  130. _groupFunc = SPH_GROUPBY_DAY;
  131. _groupSort = "@group desc";
  132. _groupDistinct = "";
  133. _maxMatches = 1000;
  134. _cutoff = 0;
  135. _retrycount = 0;
  136. _retrydelay = 0;
  137. _latitudeAttr = null;
  138. _longitudeAttr = null;
  139. _latitude = 0;
  140. _longitude = 0;
  141. _error = "";
  142. _warning = "";
  143. _reqs = new ArrayList();
  144. _weights = null;
  145. _indexWeights = new LinkedHashMap();
  146. _fieldWeights = new LinkedHashMap();
  147. _ranker = SPH_RANK_PROXIMITY_BM25;
  148. }
  149. /** Get last error message, if any. */
  150. public String GetLastError()
  151. {
  152. return _error;
  153. }
  154. /** Get last warning message, if any. */
  155. public String GetLastWarning()
  156. {
  157. return _warning;
  158. }
  159. /** Set searchd host and port to connect to. */
  160. public void SetServer(String host, int port) throws SphinxException
  161. {
  162. myAssert ( host!=null && host.length()>0, "host name must not be empty" );
  163. myAssert ( port>0 && port<65536, "port must be in 1..65535 range" );
  164. _host = host;
  165. _port = port;
  166. }
  167. /** Internal method. Sanity check. */
  168. private void myAssert ( boolean condition, String err ) throws SphinxException
  169. {
  170. if ( !condition )
  171. {
  172. _error = err;
  173. throw new SphinxException ( err );
  174. }
  175. }
  176. /** Internal method. String IO helper. */
  177. private static void writeNetUTF8 ( DataOutputStream ostream, String str ) throws IOException
  178. {
  179. if ( str==null )
  180. {
  181. ostream.writeInt ( 0 );
  182. } else
  183. {
  184. ostream.writeShort ( 0 );
  185. ostream.writeUTF ( str );
  186. }
  187. }
  188. /** Internal method. String IO helper. */
  189. private static String readNetUTF8(DataInputStream istream) throws IOException
  190. {
  191. istream.readUnsignedShort (); /* searchd emits dword lengths, but Java expects words; lets just skip first 2 bytes */
  192. return istream.readUTF ();
  193. }
  194. /** Internal method. Unsigned int IO helper. */
  195. private static long readDword ( DataInputStream istream ) throws IOException
  196. {
  197. long v = (long) istream.readInt ();
  198. if ( v<0 )
  199. v += 4294967296L;
  200. return v;
  201. }
  202. /** Internal method. Connect to searchd and exchange versions. */
  203. private Socket _Connect()
  204. {
  205. Socket sock = null;
  206. try
  207. {
  208. sock = new Socket ( _host, _port );
  209. sock.setSoTimeout ( SPH_CLIENT_TIMEOUT_MILLISEC );
  210. DataInputStream sIn = new DataInputStream ( sock.getInputStream() );
  211. int version = sIn.readInt();
  212. if ( version<1 )
  213. {
  214. sock.close ();
  215. _error = "expected searchd protocol version 1+, got version " + version;
  216. return null;
  217. }
  218. DataOutputStream sOut = new DataOutputStream ( sock.getOutputStream() );
  219. sOut.writeInt ( VER_MAJOR_PROTO );
  220. } catch ( IOException e )
  221. {
  222. _error = "connection to " + _host + ":" + _port + " failed: " + e;
  223. try
  224. {
  225. if ( sock!=null )
  226. sock.close ();
  227. } catch ( IOException e1 ) {}
  228. return null;
  229. }
  230. return sock;
  231. }
  232. /** Internal method. Get and check response packet from searchd. */
  233. private byte[] _GetResponse ( Socket sock )
  234. {
  235. /* connect */
  236. DataInputStream sIn = null;
  237. InputStream SockInput = null;
  238. try
  239. {
  240. SockInput = sock.getInputStream();
  241. sIn = new DataInputStream ( SockInput );
  242. } catch ( IOException e )
  243. {
  244. _error = "getInputStream() failed: " + e;
  245. return null;
  246. }
  247. /* read response */
  248. byte[] response = null;
  249. short status = 0, ver = 0;
  250. int len = 0;
  251. try
  252. {
  253. /* read status fields */
  254. status = sIn.readShort();
  255. ver = sIn.readShort();
  256. len = sIn.readInt();
  257. /* read response if non-empty */
  258. if ( len<=0 )
  259. {
  260. _error = "invalid response packet size (len=" + len + ")";
  261. return null;
  262. }
  263. response = new byte[len];
  264. sIn.readFully ( response, 0, len );
  265. /* check status */
  266. if ( status==SEARCHD_WARNING )
  267. {
  268. DataInputStream in = new DataInputStream ( new ByteArrayInputStream ( response ) );
  269. int iWarnLen = in.readInt ();
  270. _warning = new String ( response, 4, iWarnLen );
  271. System.arraycopy ( response, 4+iWarnLen, response, 0, response.length-4-iWarnLen );
  272. } else if ( status==SEARCHD_ERROR )
  273. {
  274. _error = "searchd error: " + new String ( response, 4, response.length-4 );
  275. return null;
  276. } else if ( status==SEARCHD_RETRY )
  277. {
  278. _error = "temporary searchd error: " + new String ( response, 4, response.length-4 );
  279. return null;
  280. } else if ( status!=SEARCHD_OK )
  281. {
  282. _error = "searched returned unknown status, code=" + status;
  283. return null;
  284. }
  285. } catch ( IOException e )
  286. {
  287. if ( len!=0 )
  288. {
  289. /* get trace, to provide even more failure details */
  290. PrintWriter ew = new PrintWriter ( new StringWriter() );
  291. e.printStackTrace ( ew );
  292. ew.flush ();
  293. ew.close ();
  294. String sTrace = ew.toString ();
  295. /* build error message */
  296. _error = "failed to read searchd response (status=" + status + ", ver=" + ver + ", len=" + len + ", trace=" + sTrace +")";
  297. } else
  298. {
  299. _error = "received zero-sized searchd response (searchd crashed?): " + e.getMessage();
  300. }
  301. return null;
  302. } finally
  303. {
  304. try
  305. {
  306. if ( sIn!=null ) sIn.close();
  307. if ( sock!=null && !sock.isConnected() ) sock.close();
  308. } catch ( IOException e )
  309. {
  310. /* silently ignore close failures; nothing could be done anyway */
  311. }
  312. }
  313. return response;
  314. }
  315. /** Internal method. Connect to searchd, send request, get response as DataInputStream. */
  316. private DataInputStream _DoRequest ( int command, int version, ByteArrayOutputStream req )
  317. {
  318. /* connect */
  319. Socket sock = _Connect();
  320. if ( sock==null )
  321. return null;
  322. /* send request */
  323. byte[] reqBytes = req.toByteArray();
  324. try
  325. {
  326. DataOutputStream sockDS = new DataOutputStream ( sock.getOutputStream() );
  327. sockDS.writeShort ( command );
  328. sockDS.writeShort ( version );
  329. sockDS.writeInt ( reqBytes.length );
  330. sockDS.write ( reqBytes );
  331. } catch ( Exception e )
  332. {
  333. _error = "network error: " + e;
  334. return null;
  335. }
  336. /* get response */
  337. byte[] response = _GetResponse ( sock );
  338. if ( response==null )
  339. return null;
  340. /* spawn that tampon */
  341. return new DataInputStream ( new ByteArrayInputStream ( response ) );
  342. }
  343. /** Set matches offset and limit to return to client, max matches to retrieve on server, and cutoff. */
  344. public void SetLimits ( int offset, int limit, int max, int cutoff ) throws SphinxException
  345. {
  346. myAssert ( offset>=0, "offset must not be negative" );
  347. myAssert ( limit>0, "limit must be positive" );
  348. myAssert ( max>0, "max must be positive" );
  349. myAssert ( cutoff>=0, "cutoff must not be negative" );
  350. _offset = offset;
  351. _limit = limit;
  352. _maxMatches = max;
  353. _cutoff = cutoff;
  354. }
  355. /** Set matches offset and limit to return to client, and max matches to retrieve on server. */
  356. public void SetLimits ( int offset, int limit, int max ) throws SphinxException
  357. {
  358. SetLimits ( offset, limit, max, _cutoff );
  359. }
  360. /** Set matches offset and limit to return to client. */
  361. public void SetLimits ( int offset, int limit) throws SphinxException
  362. {
  363. SetLimits ( offset, limit, _maxMatches, _cutoff );
  364. }
  365. /** Set maximum query time, in milliseconds, per-index, 0 means "do not limit". */
  366. public void SetMaxQueryTime ( int maxTime ) throws SphinxException
  367. {
  368. myAssert ( maxTime>=0, "max_query_time must not be negative" );
  369. _maxQueryTime = maxTime;
  370. }
  371. /** Set matching mode. */
  372. public void SetMatchMode(int mode) throws SphinxException
  373. {
  374. myAssert (
  375. mode==SPH_MATCH_ALL ||
  376. mode==SPH_MATCH_ANY ||
  377. mode==SPH_MATCH_PHRASE ||
  378. mode==SPH_MATCH_BOOLEAN ||
  379. mode==SPH_MATCH_EXTENDED ||
  380. mode==SPH_MATCH_EXTENDED2, "unknown mode value; use one of the SPH_MATCH_xxx constants" );
  381. _mode = mode;
  382. }
  383. /** Set ranking mode. */
  384. public void SetRankingMode ( int ranker ) throws SphinxException
  385. {
  386. myAssert ( ranker==SPH_RANK_PROXIMITY_BM25
  387. || ranker==SPH_RANK_BM25
  388. || ranker==SPH_RANK_NONE
  389. || ranker==SPH_RANK_WORDCOUNT, "unknown ranker value; use one of the SPH_RANK_xxx constants" );
  390. _ranker = ranker;
  391. }
  392. /** Set sorting mode. */
  393. public void SetSortMode ( int mode, String sortby ) throws SphinxException
  394. {
  395. myAssert (
  396. mode==SPH_SORT_RELEVANCE ||
  397. mode==SPH_SORT_ATTR_DESC ||
  398. mode==SPH_SORT_ATTR_ASC ||
  399. mode==SPH_SORT_TIME_SEGMENTS ||
  400. mode==SPH_SORT_EXTENDED, "unknown mode value; use one of the available SPH_SORT_xxx constants" );
  401. myAssert ( mode==SPH_SORT_RELEVANCE || ( sortby!=null && sortby.length()>0 ), "sortby string must not be empty in selected mode" );
  402. _sort = mode;
  403. _sortby = ( sortby==null ) ? "" : sortby;
  404. }
  405. /** Set per-field weights (all values must be positive). WARNING: DEPRECATED, use SetFieldWeights() instead. */
  406. public void SetWeights(int[] weights) throws SphinxException
  407. {
  408. myAssert ( weights!=null, "weights must not be null" );
  409. for (int i = 0; i < weights.length; i++) {
  410. int weight = weights[i];
  411. myAssert ( weight>0, "all weights must be greater than 0" );
  412. }
  413. _weights = weights;
  414. }
  415. /**
  416. * Bind per-field weights by field name.
  417. * @param fieldWeights hash which maps String index names to Integer weights
  418. */
  419. public void SetFieldeights ( Map fieldWeights ) throws SphinxException
  420. {
  421. /* FIXME! implement checks here */
  422. _fieldWeights = ( fieldWeights==null ) ? new LinkedHashMap () : fieldWeights;
  423. }
  424. /**
  425. * Bind per-index weights by index name (and enable summing the weights on duplicate matches, instead of replacing them).
  426. * @param indexWeights hash which maps String index names to Integer weights
  427. */
  428. public void SetIndexWeights ( Map indexWeights ) throws SphinxException
  429. {
  430. /* FIXME! implement checks here */
  431. _indexWeights = ( indexWeights==null ) ? new LinkedHashMap () : indexWeights;
  432. }
  433. /** Set document IDs range to match. */
  434. public void SetIDRange ( int min, int max ) throws SphinxException
  435. {
  436. myAssert ( min<=max, "min must be less or equal to max" );
  437. _minId = min;
  438. _maxId = max;
  439. }
  440. /** Set values filter. Only match records where attribute value is in given set. */
  441. public void SetFilter ( String attribute, int[] values, boolean exclude ) throws SphinxException
  442. {
  443. myAssert ( values!=null && values.length>0, "values array must not be null or empty" );
  444. myAssert ( attribute!=null && attribute.length()>0, "attribute name must not be null or empty" );
  445. try
  446. {
  447. writeNetUTF8 ( _filters, attribute );
  448. _filters.writeInt ( SPH_FILTER_VALUES );
  449. _filters.writeInt ( values.length );
  450. for ( int i=0; i<values.length; i++ )
  451. _filters.writeInt ( values[i] );
  452. _filters.writeInt ( exclude ? 1 : 0 );
  453. } catch ( Exception e )
  454. {
  455. myAssert ( false, "IOException: " + e.getMessage() );
  456. }
  457. _filterCount++;
  458. }
  459. /** Set values filter with a single value (syntax sugar; see {@link #SetFilter(String,int[],boolean)}). */
  460. public void SetFilter ( String attribute, int value, boolean exclude ) throws SphinxException
  461. {
  462. int[] values = new int[] { value };
  463. SetFilter ( attribute, values, exclude );
  464. }
  465. /** Set integer range filter. Only match records if attribute value is beetwen min and max (inclusive). */
  466. public void SetFilterRange ( String attribute, int min, int max, boolean exclude ) throws SphinxException
  467. {
  468. myAssert ( min<=max, "min must be less or equal to max" );
  469. try
  470. {
  471. writeNetUTF8 ( _filters, attribute );
  472. _filters.writeInt ( SPH_FILTER_RANGE );
  473. _filters.writeInt ( min );
  474. _filters.writeInt ( max );
  475. _filters.writeInt ( exclude ? 1 : 0 );
  476. } catch ( Exception e )
  477. {
  478. myAssert ( false, "IOException: " + e.getMessage() );
  479. }
  480. _filterCount++;
  481. }
  482. /** Set float range filter. Only match records if attribute value is beetwen min and max (inclusive). */
  483. public void SetFilterFloatRange ( String attribute, float min, float max, boolean exclude ) throws SphinxException
  484. {
  485. myAssert ( min<=max, "min must be less or equal to max" );
  486. try
  487. {
  488. writeNetUTF8 ( _filters, attribute );
  489. _filters.writeInt ( SPH_FILTER_RANGE );
  490. _filters.writeFloat ( min );
  491. _filters.writeFloat ( max );
  492. _filters.writeInt ( exclude ? 1 : 0 );
  493. } catch ( Exception e )
  494. {
  495. myAssert ( false, "IOException: " + e.getMessage() );
  496. }
  497. _filterCount++;
  498. }
  499. /** Setup geographical anchor point. Required to use @geodist in filters and sorting; distance will be computed to this point. */
  500. public void SetGeoAnchor ( String latitudeAttr, String longitudeAttr, float latitude, float longitude ) throws SphinxException
  501. {
  502. myAssert ( latitudeAttr!=null && latitudeAttr.length()>0, "longitudeAttr string must not be null or empty" );
  503. myAssert ( longitudeAttr!=null && longitudeAttr.length()>0, "longitudeAttr string must not be null or empty" );
  504. _latitudeAttr = latitudeAttr;
  505. _longitudeAttr = longitudeAttr;
  506. _latitude = latitude;
  507. _longitude = longitude;
  508. }
  509. /** Set grouping attribute and function. */
  510. public void SetGroupBy ( String attribute, int func, String groupsort ) throws SphinxException
  511. {
  512. myAssert (
  513. func==SPH_GROUPBY_DAY ||
  514. func==SPH_GROUPBY_WEEK ||
  515. func==SPH_GROUPBY_MONTH ||
  516. func==SPH_GROUPBY_YEAR ||
  517. func==SPH_GROUPBY_ATTR ||
  518. func==SPH_GROUPBY_ATTRPAIR, "unknown func value; use one of the available SPH_GROUPBY_xxx constants" );
  519. _groupBy = attribute;
  520. _groupFunc = func;
  521. _groupSort = groupsort;
  522. }
  523. /** Set grouping attribute and function with default ("@group desc") groupsort (syntax sugar). */
  524. public void SetGroupBy(String attribute, int func) throws SphinxException
  525. {
  526. SetGroupBy(attribute, func, "@group desc");
  527. }
  528. /** Set count-distinct attribute for group-by queries. */
  529. public void SetGroupDistinct(String attribute)
  530. {
  531. _groupDistinct = attribute;
  532. }
  533. /** Set distributed retries count and delay. */
  534. public void SetRetries ( int count, int delay ) throws SphinxException
  535. {
  536. myAssert ( count>=0, "count must not be negative" );
  537. myAssert ( delay>=0, "delay must not be negative" );
  538. _retrycount = count;
  539. _retrydelay = delay;
  540. }
  541. /** Set distributed retries count with default (zero) delay (syntax sugar). */
  542. public void SetRetries ( int count ) throws SphinxException
  543. {
  544. SetRetries ( count, 0 );
  545. }
  546. /** Reset all currently set filters (for multi-queries). */
  547. public void ResetFilters()
  548. {
  549. /* should we close them first? */
  550. _rawFilters = new ByteArrayOutputStream();
  551. _filters = new DataOutputStream(_rawFilters);
  552. _filterCount = 0;
  553. /* reset GEO anchor */
  554. _latitudeAttr = null;
  555. _longitudeAttr = null;
  556. _latitude = 0;
  557. _longitude = 0;
  558. }
  559. /** Connect to searchd server and run current search query against all indexes (syntax sugar). */
  560. public SphinxResult Query ( String query ) throws SphinxException
  561. {
  562. return Query ( query, "*", "" );
  563. }
  564. /** Connect to searchd server and run current search query against all indexes (syntax sugar). */
  565. public SphinxResult Query ( String query, String index ) throws SphinxException
  566. {
  567. return Query ( query, index, "" );
  568. }
  569. /** Connect to searchd server and run current search query. */
  570. public SphinxResult Query ( String query, String index, String comment ) throws SphinxException
  571. {
  572. myAssert ( _reqs==null || _reqs.size()==0, "AddQuery() and Query() can not be combined; use RunQueries() instead" );
  573. AddQuery ( query, index, comment );
  574. SphinxResult[] results = RunQueries();
  575. if (results == null || results.length < 1) {
  576. return null; /* probably network error; error message should be already filled */
  577. }
  578. SphinxResult res = results[0];
  579. _warning = res.warning;
  580. _error = res.error;
  581. if (res == null || res.getStatus() == SEARCHD_ERROR) {
  582. return null;
  583. } else {
  584. return res;
  585. }
  586. }
  587. /** Add new query with current settings to current search request. */
  588. public int AddQuery ( String query, String index, String comment ) throws SphinxException
  589. {
  590. ByteArrayOutputStream req = new ByteArrayOutputStream();
  591. /* build request */
  592. try {
  593. DataOutputStream out = new DataOutputStream(req);
  594. out.writeInt(_offset);
  595. out.writeInt(_limit);
  596. out.writeInt(_mode);
  597. out.writeInt(_ranker);
  598. out.writeInt(_sort);
  599. writeNetUTF8(out, _sortby);
  600. writeNetUTF8(out, query);
  601. int weightLen = _weights != null ? _weights.length : 0;
  602. out.writeInt(weightLen);
  603. if (_weights != null) {
  604. for (int i = 0; i < _weights.length; i++)
  605. out.writeInt(_weights[i]);
  606. }
  607. writeNetUTF8(out, index);
  608. out.writeInt(0);
  609. out.writeInt(_minId);
  610. out.writeInt(_maxId);
  611. /* filters */
  612. out.writeInt(_filterCount);
  613. out.write(_rawFilters.toByteArray());
  614. /* group-by, max matches, sort-by-group flag */
  615. out.writeInt(_groupFunc);
  616. writeNetUTF8(out, _groupBy);
  617. out.writeInt(_maxMatches);
  618. writeNetUTF8(out, _groupSort);
  619. out.writeInt(_cutoff);
  620. out.writeInt(_retrycount);
  621. out.writeInt(_retrydelay);
  622. writeNetUTF8(out, _groupDistinct);
  623. /* anchor point */
  624. if (_latitudeAttr == null || _latitudeAttr.length() == 0 || _longitudeAttr == null || _longitudeAttr.length() == 0) {
  625. out.writeInt(0);
  626. } else {
  627. out.writeInt(1);
  628. writeNetUTF8(out, _latitudeAttr);
  629. writeNetUTF8(out, _longitudeAttr);
  630. out.writeFloat(_latitude);
  631. out.writeFloat(_longitude);
  632. }
  633. /* per-index weights */
  634. out.writeInt(_indexWeights.size());
  635. for (Iterator e = _indexWeights.keySet().iterator(); e.hasNext();) {
  636. String indexName = (String) e.next();
  637. Integer weight = (Integer) _indexWeights.get(indexName);
  638. writeNetUTF8(out, indexName);
  639. out.writeInt(weight.intValue());
  640. }
  641. /* max query time */
  642. out.writeInt ( _maxQueryTime );
  643. /* per-field weights */
  644. out.writeInt ( _fieldWeights.size() );
  645. for ( Iterator e=_fieldWeights.keySet().iterator(); e.hasNext(); )
  646. {
  647. String field = (String) e.next();
  648. Integer weight = (Integer) _fieldWeights.get ( field );
  649. writeNetUTF8 ( out, field );
  650. out.writeInt ( weight.intValue() );
  651. }
  652. /* comment */
  653. writeNetUTF8 ( out, comment );
  654. /* done! */
  655. out.flush ();
  656. int qIndex = _reqs.size();
  657. _reqs.add ( qIndex, req.toByteArray() );
  658. return qIndex;
  659. } catch ( Exception e )
  660. {
  661. myAssert ( false, "error in AddQuery(): " + e + ": " + e.getMessage() );
  662. } finally
  663. {
  664. try
  665. {
  666. _filters.close ();
  667. _rawFilters.close ();
  668. } catch ( IOException e )
  669. {
  670. myAssert ( false, "error in AddQuery(): " + e + ": " + e.getMessage() );
  671. }
  672. }
  673. return -1;
  674. }
  675. /** Run all previously added search queries. */
  676. public SphinxResult[] RunQueries() throws SphinxException
  677. {
  678. if ( _reqs==null || _reqs.size()<1 )
  679. {
  680. _error = "no queries defined, issue AddQuery() first";
  681. return null;
  682. }
  683. /* build the mega-request */
  684. int nreqs = _reqs.size();
  685. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  686. try
  687. {
  688. DataOutputStream req = new DataOutputStream ( reqBuf );
  689. req.writeInt ( nreqs );
  690. for ( int i=0; i<nreqs; i++ )
  691. req.write ( (byte[]) _reqs.get(i) );
  692. req.flush ();
  693. } catch ( Exception e )
  694. {
  695. _error = "internal error: failed to build request: " + e;
  696. return null;
  697. }
  698. DataInputStream in =_DoRequest ( SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, reqBuf );
  699. if ( in==null )
  700. return null;
  701. SphinxResult[] results = new SphinxResult [ nreqs ];
  702. _reqs = new ArrayList();
  703. try
  704. {
  705. for ( int ires=0; ires<nreqs; ires++ )
  706. {
  707. SphinxResult res = new SphinxResult();
  708. results[ires] = res;
  709. int status = in.readInt();
  710. res.setStatus ( status );
  711. if (status != SEARCHD_OK) {
  712. String message = readNetUTF8(in);
  713. if (status == SEARCHD_WARNING) {
  714. res.warning = message;
  715. } else {
  716. res.error = message;
  717. continue;
  718. }
  719. }
  720. /* read fields */
  721. int nfields = in.readInt();
  722. res.fields = new String[nfields];
  723. int pos = 0;
  724. for (int i = 0; i < nfields; i++)
  725. res.fields[i] = readNetUTF8(in);
  726. /* read arrts */
  727. int nattrs = in.readInt();
  728. res.attrTypes = new int[nattrs];
  729. res.attrNames = new String[nattrs];
  730. for (int i = 0; i < nattrs; i++) {
  731. String AttrName = readNetUTF8(in);
  732. int AttrType = in.readInt();
  733. res.attrNames[i] = AttrName;
  734. res.attrTypes[i] = AttrType;
  735. }
  736. /* read match count */
  737. int count = in.readInt();
  738. int id64 = in.readInt();
  739. res.matches = new SphinxMatch[count];
  740. for ( int matchesNo=0; matchesNo<count; matchesNo++ )
  741. {
  742. SphinxMatch docInfo;
  743. docInfo = new SphinxMatch (
  744. ( id64==0 ) ? readDword(in) : in.readLong(),
  745. in.readInt() );
  746. /* read matches */
  747. for (int attrNumber = 0; attrNumber < res.attrTypes.length; attrNumber++)
  748. {
  749. String attrName = res.attrNames[attrNumber];
  750. int type = res.attrTypes[attrNumber];
  751. /* handle floats */
  752. if ( type==SPH_ATTR_FLOAT )
  753. {
  754. docInfo.attrValues.add ( attrNumber, new Float ( in.readFloat() ) );
  755. continue;
  756. }
  757. /* handle everything else as unsigned ints */
  758. long val = readDword ( in );
  759. if ( ( type & SPH_ATTR_MULTI )!=0 )
  760. {
  761. long[] vals = new long [ (int)val ];
  762. for ( int k=0; k<val; k++ )
  763. vals[k] = readDword ( in );
  764. docInfo.attrValues.add ( attrNumber, vals );
  765. } else
  766. {
  767. docInfo.attrValues.add ( attrNumber, new Long ( val ) );
  768. }
  769. }
  770. res.matches[matchesNo] = docInfo;
  771. }
  772. res.total = in.readInt();
  773. res.totalFound = in.readInt();
  774. res.time = in.readInt() / 1000.0f;
  775. res.words = new SphinxWordInfo [ in.readInt() ];
  776. for ( int i=0; i<res.words.length; i++ )
  777. res.words[i] = new SphinxWordInfo ( readNetUTF8(in), readDword(in), readDword(in) );
  778. }
  779. return results;
  780. } catch ( IOException e )
  781. {
  782. _error = "incomplete reply";
  783. return null;
  784. }
  785. }
  786. /**
  787. * Connect to searchd server and generate excerpts (snippets) from given documents.
  788. * @param opts maps String keys to String or Integer values (see the documentation for complete keys list).
  789. * @return null on failure, array of snippets on success.
  790. */
  791. public String[] BuildExcerpts ( String[] docs, String index, String words, Map opts ) throws SphinxException
  792. {
  793. myAssert(docs != null && docs.length > 0, "BuildExcerpts: Have no documents to process");
  794. myAssert(index != null && index.length() > 0, "BuildExcerpts: Have no index to process documents");
  795. myAssert(words != null && words.length() > 0, "BuildExcerpts: Have no words to highlight");
  796. if (opts == null) opts = new LinkedHashMap();
  797. /* fixup options */
  798. if (!opts.containsKey("before_match")) opts.put("before_match", "<b>");
  799. if (!opts.containsKey("after_match")) opts.put("after_match", "</b>");
  800. if (!opts.containsKey("chunk_separator")) opts.put("chunk_separator", "...");
  801. if (!opts.containsKey("limit")) opts.put("limit", new Integer(256));
  802. if (!opts.containsKey("around")) opts.put("around", new Integer(5));
  803. if (!opts.containsKey("exact_phrase")) opts.put("exact_phrase", new Integer(0));
  804. if (!opts.containsKey("single_passage")) opts.put("single_passage", new Integer(0));
  805. if (!opts.containsKey("use_boundaries")) opts.put("use_boundaries", new Integer(0));
  806. if (!opts.containsKey("weight_order")) opts.put("weight_order", new Integer(0));
  807. /* build request */
  808. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  809. DataOutputStream req = new DataOutputStream ( reqBuf );
  810. try
  811. {
  812. req.writeInt(0);
  813. int iFlags = 1; /* remove_spaces */
  814. if ( ((Integer)opts.get("exact_phrase"))!=0 ) iFlags |= 2;
  815. if ( ((Integer)opts.get("single_passage"))!=0 ) iFlags |= 4;
  816. if ( ((Integer)opts.get("use_boundaries"))!=0 ) iFlags |= 8;
  817. if ( ((Integer)opts.get("weight_order"))!=0 ) iFlags |= 16;
  818. req.writeInt ( iFlags );
  819. writeNetUTF8 ( req, index );
  820. writeNetUTF8 ( req, words );
  821. /* send options */
  822. writeNetUTF8 ( req, (String) opts.get("before_match") );
  823. writeNetUTF8 ( req, (String) opts.get("after_match") );
  824. writeNetUTF8 ( req, (String) opts.get("chunk_separator") );
  825. req.writeInt ( ((Integer) opts.get("limit")).intValue() );
  826. req.writeInt ( ((Integer) opts.get("around")).intValue() );
  827. /* send documents */
  828. for ( int i=0; i<docs.length; i++ )
  829. writeNetUTF8 ( req, docs[i] );
  830. req.flush();
  831. } catch ( Exception e )
  832. {
  833. _error = "internal error: failed to build request: " + e;
  834. return null;
  835. }
  836. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, reqBuf );
  837. if ( in==null )
  838. return null;
  839. try
  840. {
  841. String[] res = new String [ docs.length ];
  842. for ( int i=0; i<docs.length; i++ )
  843. res[i] = readNetUTF8 ( in );
  844. return res;
  845. } catch ( Exception e )
  846. {
  847. _error = "incomplete reply";
  848. return null;
  849. }
  850. }
  851. /**
  852. * Connect to searchd server and update given attributes on given documents in given indexes.
  853. * Sample code that will set group_id=123 where id=1 and group_id=456 where id=3:
  854. *
  855. * <pre>
  856. * String[] attrs = new String[1];
  857. *
  858. * attrs[0] = "group_id";
  859. * long[][] values = new long[2][2];
  860. *
  861. * values[0] = new long[2]; values[0][0] = 1; values[0][1] = 123;
  862. * values[1] = new long[2]; values[1][0] = 3; values[1][1] = 456;
  863. *
  864. * int res = cl.UpdateAttributes ( "test1", attrs, values );
  865. * </pre>
  866. *
  867. * @param index index name(s) to update; might be distributed
  868. * @param attrs array with the names of the attributes to update
  869. * @param values array of updates; each long[] entry must contains document ID
  870. * in the first element, and all new attribute values in the following ones
  871. * @return -1 on failure, amount of actually found and updated documents (might be 0) on success
  872. *
  873. * @throws SphinxException on invalid parameters
  874. */
  875. public int UpdateAttributes ( String index, String[] attrs, long[][] values ) throws SphinxException
  876. {
  877. /* check args */
  878. myAssert ( index!=null && index.length()>0, "no index name provided" );
  879. myAssert ( attrs!=null && attrs.length>0, "no attribute names provided" );
  880. myAssert ( values!=null && values.length>0, "no update entries provided" );
  881. for ( int i=0; i<values.length; i++ )
  882. {
  883. myAssert ( values[i]!=null, "update entry #" + i + " is null" );
  884. myAssert ( values[i].length==1+attrs.length, "update entry #" + i + " has wrong length" );
  885. }
  886. /* build and send request */
  887. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  888. DataOutputStream req = new DataOutputStream ( reqBuf );
  889. try
  890. {
  891. writeNetUTF8 ( req, index );
  892. req.writeInt ( attrs.length );
  893. for ( int i=0; i<attrs.length; i++ )
  894. writeNetUTF8 ( req, attrs[i] );
  895. req.writeInt ( values.length );
  896. for ( int i=0; i<values.length; i++ )
  897. {
  898. req.writeLong ( values[i][0] ); /* send docid as 64bit value */
  899. for ( int j=1; j<values[i].length; j++ )
  900. req.writeInt ( (int)values[i][j] ); /* send values as 32bit values; FIXME! what happens when they are over 2^31? */
  901. }
  902. req.flush();
  903. } catch ( Exception e )
  904. {
  905. _error = "internal error: failed to build request: " + e;
  906. return -1;
  907. }
  908. /* get and parse response */
  909. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, reqBuf );
  910. if ( in==null )
  911. return -1;
  912. try
  913. {
  914. return in.readInt ();
  915. } catch ( Exception e )
  916. {
  917. _error = "incomplete reply";
  918. return -1;
  919. }
  920. }
  921. /**
  922. * Connect to searchd server, and generate keyword list for a given query.
  923. * Returns null on failure, an array of Maps with misc per-keyword info on success.
  924. */
  925. public Map[] BuildKeywords ( String query, String index, boolean hits ) throws SphinxException
  926. {
  927. /* build request */
  928. ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
  929. DataOutputStream req = new DataOutputStream ( reqBuf );
  930. try
  931. {
  932. writeNetUTF8 ( req, query );
  933. writeNetUTF8 ( req, index );
  934. req.writeInt ( hits ? 1 : 0 );
  935. } catch ( Exception e )
  936. {
  937. _error = "internal error: failed to build request: " + e;
  938. return null;
  939. }
  940. /* run request */
  941. DataInputStream in = _DoRequest ( SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, reqBuf );
  942. if ( in==null )
  943. return null;
  944. /* parse reply */
  945. try
  946. {
  947. int iNumWords = in.readInt ();
  948. Map[] res = new Map[iNumWords];
  949. for ( int i=0; i<iNumWords; i++ )
  950. {
  951. res[i] = new LinkedHashMap ();
  952. res[i].put ( "tokenized", readNetUTF8 ( in ) );
  953. res[i].put ( "normalized", readNetUTF8 ( in ) );
  954. if ( hits )
  955. {
  956. res[i].put ( "docs", readDword ( in ) );
  957. res[i].put ( "hits", readDword ( in ) );
  958. }
  959. }
  960. return res;
  961. } catch ( Exception e )
  962. {
  963. _error = "incomplete reply";
  964. return null;
  965. }
  966. }
  967. }
  968. /*
  969. * $Id$
  970. */