test.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * $Id$
  3. */
  4. package org.sphx.api;
  5. import java.util.*;
  6. /**
  7. * Test class for sphinx API
  8. */
  9. public class test
  10. {
  11. public static void main ( String[] argv ) throws SphinxException
  12. {
  13. if ( argv==null || argv.length<1 )
  14. {
  15. System.out.print ( "Usage: java -jar sphinxapi.jar [OPTIONS] query words\n\n" );
  16. System.out.print ( "Options are:\n" );
  17. System.out.print ( "-h, --host <HOST>\tconnect to searchd at host HOST\n" );
  18. System.out.print ( "-p, --port\t\tconnect to searchd at port PORT\n" );
  19. System.out.print ( "-i, --index <IDX>\tsearch through index(es) specified by IDX\n" );
  20. System.out.print ( "-s, --sortby <CLAUSE>\tsort matches by 'CLAUSE' in sort_extended mode\n" );
  21. System.out.print ( "-S, --sortexpr <EXPR>\tsort matches by 'EXPR' DESC in sort_expr mode\n" );
  22. System.out.print ( "-a, --any\t\tuse 'match any word' matching mode\n" );
  23. System.out.print ( "-b, --boolean\t\tuse 'boolean query' matching mode\n" );
  24. System.out.print ( "-e, --extended\t\tuse 'extended query' matching mode\n" );
  25. System.out.print ( "-ph,--phrase\t\tuse 'exact phrase' matching mode\n" );
  26. // System.out.print ( "-f, --filter <ATTR>\tfilter by attribute 'ATTR' (default is 'group_id')\n" );
  27. // System.out.print ( "-v, --value <VAL>\tadd VAL to allowed 'group_id' values list\n" );
  28. System.out.print ( "-g, --groupby <EXPR>\tgroup matches by 'EXPR'\n" );
  29. System.out.print ( "-gs,--groupsort <EXPR>\tsort groups by 'EXPR'\n" );
  30. // System.out.print ( "-d, --distinct <ATTR>\tcount distinct values of 'ATTR''\n" );
  31. System.out.print ( "-l, --limit <COUNT>\tretrieve COUNT matches (default: 20)\n" );
  32. System.out.print ( "-ga, --geoanchor <LATATTR> <LONGATTR> <LAT> <LONG>\n" );
  33. System.out.print ( "\t\t\tset anchor for geodistance\n" );
  34. System.out.print ( "--select <EXPRS>\tselect the listed expressions only\n" );
  35. System.exit ( 0 );
  36. }
  37. StringBuffer q = new StringBuffer();
  38. String host = "localhost";
  39. int port = 3312;
  40. int mode = SphinxClient.SPH_MATCH_ALL;
  41. String index = "*";
  42. int offset = 0;
  43. int limit = 20;
  44. int sortMode = SphinxClient.SPH_SORT_RELEVANCE;
  45. String sortClause = "";
  46. String groupBy = "";
  47. String groupSort = "";
  48. SphinxClient cl = new SphinxClient();
  49. /* parse arguments */
  50. if ( argv!=null)
  51. for ( int i=0; i<argv.length; i++ )
  52. {
  53. String arg = argv[i];
  54. if ( "-h".equals(arg) || "--host".equals(arg) ) host = argv[++i];
  55. else if ( "-p".equals(arg) || "--port".equals(arg) ) port = Integer.parseInt ( argv[++i] );
  56. else if ( "-i".equals(arg) || "--index".equals(arg) ) index = argv[++i];
  57. else if ( "-s".equals(arg) || "--sortby".equals(arg) ) { sortMode = SphinxClient.SPH_SORT_EXTENDED; sortClause = argv[++i]; }
  58. else if ( "-S".equals(arg) || "--sortexpr".equals(arg) ) { sortMode = SphinxClient.SPH_SORT_EXPR; sortClause = argv[++i]; }
  59. else if ( "-a".equals(arg) || "--any".equals(arg) ) mode = SphinxClient.SPH_MATCH_ANY;
  60. else if ( "-b".equals(arg) || "--boolean".equals(arg) ) mode = SphinxClient.SPH_MATCH_BOOLEAN;
  61. else if ( "-e".equals(arg) || "--extended".equals(arg) ) mode = SphinxClient.SPH_MATCH_EXTENDED;
  62. else if ( "-ph".equals(arg)|| "--phrase".equals(arg) ) mode = SphinxClient.SPH_MATCH_PHRASE;
  63. else if ( "-e2".equals(arg) ) mode = SphinxClient.SPH_MATCH_EXTENDED2;
  64. else if ( "-g".equals(arg) || "--group".equals(arg) ) groupBy = argv[++i];
  65. else if ( "-gs".equals(arg)|| "--groupsort".equals(arg) ) groupSort = argv[++i];
  66. else if ( "-o".equals(arg) || "--offset".equals(arg) ) offset = Integer.parseInt(argv[++i]);
  67. else if ( "-l".equals(arg) || "--limit".equals(arg) ) limit = Integer.parseInt(argv[++i]);
  68. else if ( "-ga".equals(arg)|| "--geoanchor".equals(arg) ) cl.SetGeoAnchor ( argv[++i], argv[++i], Float.parseFloat(argv[++i]), Float.parseFloat(argv[++i]) );
  69. else if ( "--select".equals(arg) ) cl.SetSelect ( argv[++i] );
  70. else q.append ( argv[i] ).append ( " " );
  71. }
  72. cl.SetServer ( host, port );
  73. cl.SetWeights ( new int[] { 100, 1 } );
  74. cl.SetMatchMode ( mode );
  75. cl.SetLimits ( offset, limit );
  76. cl.SetSortMode ( sortMode, sortClause );
  77. if ( groupBy.length()>0 )
  78. cl.SetGroupBy ( groupBy, SphinxClient.SPH_GROUPBY_ATTR, groupSort );
  79. SphinxResult res = cl.Query(q.toString(), index);
  80. if ( res==null )
  81. {
  82. System.err.println ( "Error: " + cl.GetLastError() );
  83. System.exit ( 1 );
  84. }
  85. if ( cl.GetLastWarning()!=null && cl.GetLastWarning().length()>0 )
  86. System.out.println ( "WARNING: " + cl.GetLastWarning() + "\n" );
  87. /* print me out */
  88. System.out.println ( "Query '" + q + "' retrieved " + res.total + " of " + res.totalFound + " matches in " + res.time + " sec." );
  89. System.out.println ( "Query stats:" );
  90. for ( int i=0; i<res.words.length; i++ )
  91. {
  92. SphinxWordInfo wordInfo = res.words[i];
  93. System.out.println ( "\t'" + wordInfo.word + "' found " + wordInfo.hits + " times in " + wordInfo.docs + " documents" );
  94. }
  95. System.out.println ( "\nMatches:" );
  96. for ( int i=0; i<res.matches.length; i++ )
  97. {
  98. SphinxMatch info = res.matches[i];
  99. System.out.print ( (i+1) + ". id=" + info.docId + ", weight=" + info.weight );
  100. if ( res.attrNames==null || res.attrTypes==null )
  101. continue;
  102. for ( int a=0; a<res.attrNames.length; a++ )
  103. {
  104. System.out.print ( ", " + res.attrNames[a] + "=" );
  105. if ( ( res.attrTypes[a] & SphinxClient.SPH_ATTR_MULTI )!=0 )
  106. {
  107. System.out.print ( "(" );
  108. long[] attrM = (long[]) info.attrValues.get(a);
  109. if ( attrM!=null )
  110. for ( int j=0; j<attrM.length; j++ )
  111. {
  112. if ( j!=0 )
  113. System.out.print ( "," );
  114. System.out.print ( attrM[j] );
  115. }
  116. System.out.print ( ")" );
  117. } else
  118. {
  119. switch ( res.attrTypes[a] )
  120. {
  121. case SphinxClient.SPH_ATTR_INTEGER:
  122. case SphinxClient.SPH_ATTR_ORDINAL:
  123. case SphinxClient.SPH_ATTR_FLOAT:
  124. case SphinxClient.SPH_ATTR_BIGINT:
  125. case SphinxClient.SPH_ATTR_STRING:
  126. /* ints, longs, floats, strings.. print as is */
  127. System.out.print ( info.attrValues.get(a) );
  128. break;
  129. case SphinxClient.SPH_ATTR_TIMESTAMP:
  130. Long iStamp = (Long) info.attrValues.get(a);
  131. Date date = new Date ( iStamp.longValue()*1000 );
  132. System.out.print ( date.toString() );
  133. break;
  134. default:
  135. System.out.print ( "(unknown-attr-type=" + res.attrTypes[a] + ")" );
  136. }
  137. }
  138. }
  139. System.out.println();
  140. }
  141. }
  142. }
  143. /*
  144. * $Id$
  145. */