test.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. private static final int RESULT_LIMIT = 20;
  12. private static final int MAX_RESULTS = 1000;
  13. public static void main ( String[] argv ) throws SphinxException
  14. {
  15. if ( argv==null || argv.length<2 )
  16. {
  17. System.out.println ( "usage: test [-a] <word [word [word [...]]]> [-g <group>] [-p <port>] [-h <host>] [-i <index>] [-e] [-b]" );
  18. System.out.println ( "or: test [-any] <word [word [word [...]]]> [--group <group>] [--port <port>] [--host <host>] [--index <index>] [--extended] [--boolean]\n" );
  19. }
  20. StringBuffer q = new StringBuffer();
  21. String host = "localhost";
  22. int port = 3312;
  23. int mode = SphinxClient.SPH_MATCH_ALL;
  24. Set groups = new LinkedHashSet();
  25. String index = "*";
  26. int offset = 0;
  27. /* parse arguments */
  28. if ( argv!=null)
  29. for ( int i=0; i<argv.length; i++ )
  30. {
  31. String arg = argv[i];
  32. if ( "-a".equals(arg) || "--any".equals(arg) ) mode = SphinxClient.SPH_MATCH_ANY;
  33. else if ( "-b".equals(arg) || "--boolean".equals(arg) ) mode = SphinxClient.SPH_MATCH_BOOLEAN;
  34. else if ( "-e".equals(arg) || "--extended".equals(arg) ) mode = SphinxClient.SPH_MATCH_EXTENDED;
  35. else if ( "-g".equals(arg) || "--group".equals(arg) ) groups.add ( argv[++i] );
  36. else if ( "-p".equals(arg) || "--port".equals(arg) ) port = Integer.parseInt ( argv[++i] );
  37. else if ( "-h".equals(arg) || "--host".equals(arg) ) host = argv[++i];
  38. else if ( "-o".equals(arg) || "--offsset".equals(arg) ) offset = Integer.parseInt(argv[++i]);
  39. else if ( "-i".equals(arg) || "--index".equals(arg) ) index = argv[++i];
  40. else q.append ( argv[i] ).append ( " " );
  41. }
  42. SphinxClient cl = new SphinxClient();
  43. cl.SetServer ( host, port );
  44. cl.SetWeights ( new int[] { 100, 1 } );
  45. cl.SetMatchMode ( mode );
  46. cl.SetLimits ( offset, RESULT_LIMIT, MAX_RESULTS );
  47. /* convert groups to int[] */
  48. if ( groups.size()>0 )
  49. {
  50. int[] groupList = new int[groups.size()];
  51. String group = null;
  52. int i = 0;
  53. try
  54. {
  55. for (Iterator e = groups.iterator(); e.hasNext(); i++)
  56. {
  57. group = (String) e.next();
  58. groupList[i] = Integer.parseInt(group);
  59. }
  60. } catch (Exception ex)
  61. {
  62. System.out.println("Groups must be integer. Failed to convert group " + group);
  63. return;
  64. }
  65. cl.SetFilter("group_id", groupList, false);
  66. }
  67. SphinxResult res = cl.Query(q.toString(), index);
  68. if (res == null || (cl.GetLastError() != null && cl.GetLastError().length() > 0))
  69. {
  70. System.err.println("Error: " + cl.GetLastError());
  71. System.exit(1);
  72. }
  73. /* print me out */
  74. System.out.println ( "Query '" + q + "' retrieved " + res.total + " of " + res.totalFound + " matches in " + res.time + " sec." );
  75. System.out.println ( "Query stats:" );
  76. for ( int i=0; i<res.words.length; i++ )
  77. {
  78. SphinxWordInfo wordInfo = res.words[i];
  79. System.out.println ( "\t'" + wordInfo.word + "' found " + wordInfo.hits + " times in " + wordInfo.docs + " documents" );
  80. }
  81. System.out.println ( "\nMatches:" );
  82. for ( int i=0; i<res.matches.length; i++ )
  83. {
  84. SphinxMatch info = res.matches[i];
  85. System.out.print ( (i+1) + ". id=" + info.docId + ", weight=" + info.weight );
  86. if ( res.attrNames==null || res.attrTypes==null )
  87. continue;
  88. for ( int a=0; a<res.attrNames.length; a++ )
  89. {
  90. System.out.print ( ", " + res.attrNames[a] + "=" );
  91. if ( ( res.attrTypes[a] & SphinxClient.SPH_ATTR_MULTI )!=0 )
  92. {
  93. System.out.print ( "(" );
  94. int[] attrM = (int[]) info.attrValues.get(a);
  95. if ( attrM!=null )
  96. for ( int j=0; j<attrM.length; j++ )
  97. {
  98. if ( j!=0 )
  99. System.out.print ( "," );
  100. System.out.print ( attrM[j] );
  101. }
  102. System.out.print ( ")" );
  103. } else
  104. {
  105. switch ( res.attrTypes[a] )
  106. {
  107. case SphinxClient.SPH_ATTR_INTEGER:
  108. case SphinxClient.SPH_ATTR_ORDINAL:
  109. Integer attrI = (Integer) info.attrValues.get(a);
  110. System.out.print ( attrI.intValue() );
  111. break;
  112. case SphinxClient.SPH_ATTR_FLOAT:
  113. Float attrF = (Float) info.attrValues.get(a);
  114. System.out.print ( attrF.floatValue() );
  115. break;
  116. case SphinxClient.SPH_ATTR_TIMESTAMP:
  117. Integer attrT = (Integer) info.attrValues.get(a);
  118. Date date = new Date ( attrT.longValue()*1000 );
  119. System.out.print ( date.toString() );
  120. break;
  121. default:
  122. System.out.print ( "(unknown-attr-type=" + res.attrTypes[a] + ")" );
  123. }
  124. }
  125. }
  126. System.out.println();
  127. }
  128. }
  129. }
  130. /*
  131. * $Id$
  132. */