test.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.exit ( 0 );
  35. }
  36. StringBuffer q = new StringBuffer();
  37. String host = "localhost";
  38. int port = 3312;
  39. int mode = SphinxClient.SPH_MATCH_ALL;
  40. String index = "*";
  41. int offset = 0;
  42. int limit = 20;
  43. int sortMode = SphinxClient.SPH_SORT_RELEVANCE;
  44. String sortClause = "";
  45. String groupBy = "";
  46. String groupSort = "";
  47. SphinxClient cl = new SphinxClient();
  48. /* parse arguments */
  49. if ( argv!=null)
  50. for ( int i=0; i<argv.length; i++ )
  51. {
  52. String arg = argv[i];
  53. if ( "-h".equals(arg) || "--host".equals(arg) ) host = argv[++i];
  54. else if ( "-p".equals(arg) || "--port".equals(arg) ) port = Integer.parseInt ( argv[++i] );
  55. else if ( "-i".equals(arg) || "--index".equals(arg) ) index = argv[++i];
  56. else if ( "-s".equals(arg) || "--sortby".equals(arg) ) { sortMode = SphinxClient.SPH_SORT_EXTENDED; sortClause = argv[++i]; }
  57. else if ( "-S".equals(arg) || "--sortexpr".equals(arg) ) { sortMode = SphinxClient.SPH_SORT_EXPR; sortClause = argv[++i]; }
  58. else if ( "-a".equals(arg) || "--any".equals(arg) ) mode = SphinxClient.SPH_MATCH_ANY;
  59. else if ( "-b".equals(arg) || "--boolean".equals(arg) ) mode = SphinxClient.SPH_MATCH_BOOLEAN;
  60. else if ( "-e".equals(arg) || "--extended".equals(arg) ) mode = SphinxClient.SPH_MATCH_EXTENDED;
  61. else if ( "-ph".equals(arg)|| "--phrase".equals(arg) ) mode = SphinxClient.SPH_MATCH_PHRASE;
  62. else if ( "-e2".equals(arg) ) mode = SphinxClient.SPH_MATCH_EXTENDED2;
  63. else if ( "-g".equals(arg) || "--group".equals(arg) ) groupBy = argv[++i];
  64. else if ( "-gs".equals(arg)|| "--groupsort".equals(arg) ) groupSort = argv[++i];
  65. else if ( "-o".equals(arg) || "--offset".equals(arg) ) offset = Integer.parseInt(argv[++i]);
  66. else if ( "-l".equals(arg) || "--limit".equals(arg) ) limit = Integer.parseInt(argv[++i]);
  67. else if ( "-ga".equals(arg)|| "--geoanchor".equals(arg) ) cl.SetGeoAnchor ( argv[++i], argv[++i], Float.parseFloat(argv[++i]), Float.parseFloat(argv[++i]) );
  68. else q.append ( argv[i] ).append ( " " );
  69. }
  70. cl.SetServer ( host, port );
  71. cl.SetWeights ( new int[] { 100, 1 } );
  72. cl.SetMatchMode ( mode );
  73. cl.SetLimits ( offset, limit );
  74. cl.SetSortMode ( sortMode, sortClause );
  75. if ( groupBy.length()>0 )
  76. cl.SetGroupBy ( groupBy, SphinxClient.SPH_GROUPBY_ATTR, groupSort );
  77. SphinxResult res = cl.Query(q.toString(), index);
  78. if ( res==null )
  79. {
  80. System.err.println ( "Error: " + cl.GetLastError() );
  81. System.exit ( 1 );
  82. }
  83. if ( cl.GetLastWarning()!=null && cl.GetLastWarning().length()>0 )
  84. System.out.println ( "WARNING: " + cl.GetLastWarning() + "\n" );
  85. /* print me out */
  86. System.out.println ( "Query '" + q + "' retrieved " + res.total + " of " + res.totalFound + " matches in " + res.time + " sec." );
  87. System.out.println ( "Query stats:" );
  88. for ( int i=0; i<res.words.length; i++ )
  89. {
  90. SphinxWordInfo wordInfo = res.words[i];
  91. System.out.println ( "\t'" + wordInfo.word + "' found " + wordInfo.hits + " times in " + wordInfo.docs + " documents" );
  92. }
  93. System.out.println ( "\nMatches:" );
  94. for ( int i=0; i<res.matches.length; i++ )
  95. {
  96. SphinxMatch info = res.matches[i];
  97. System.out.print ( (i+1) + ". id=" + info.docId + ", weight=" + info.weight );
  98. if ( res.attrNames==null || res.attrTypes==null )
  99. continue;
  100. for ( int a=0; a<res.attrNames.length; a++ )
  101. {
  102. System.out.print ( ", " + res.attrNames[a] + "=" );
  103. if ( ( res.attrTypes[a] & SphinxClient.SPH_ATTR_MULTI )!=0 )
  104. {
  105. System.out.print ( "(" );
  106. long[] attrM = (long[]) info.attrValues.get(a);
  107. if ( attrM!=null )
  108. for ( int j=0; j<attrM.length; j++ )
  109. {
  110. if ( j!=0 )
  111. System.out.print ( "," );
  112. System.out.print ( attrM[j] );
  113. }
  114. System.out.print ( ")" );
  115. } else
  116. {
  117. switch ( res.attrTypes[a] )
  118. {
  119. case SphinxClient.SPH_ATTR_INTEGER:
  120. case SphinxClient.SPH_ATTR_ORDINAL:
  121. case SphinxClient.SPH_ATTR_FLOAT:
  122. /* longs or floats; print as is */
  123. System.out.print ( info.attrValues.get(a) );
  124. break;
  125. case SphinxClient.SPH_ATTR_TIMESTAMP:
  126. Long iStamp = (Long) info.attrValues.get(a);
  127. Date date = new Date ( iStamp.longValue()*1000 );
  128. System.out.print ( date.toString() );
  129. break;
  130. default:
  131. System.out.print ( "(unknown-attr-type=" + res.attrTypes[a] + ")" );
  132. }
  133. }
  134. }
  135. System.out.println();
  136. }
  137. }
  138. }
  139. /*
  140. * $Id$
  141. */