test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. //
  3. // $Id$
  4. //
  5. require ( "sphinxapi.php" );
  6. //////////////////////
  7. // parse command line
  8. //////////////////////
  9. // for very old PHP versions, like at my home test server
  10. if ( is_array($argv) && !isset($_SERVER["argv"]) )
  11. $_SERVER["argv"] = $argv;
  12. unset ( $_SERVER["argv"][0] );
  13. // build query
  14. if ( !is_array($_SERVER["argv"]) || empty($_SERVER["argv"]) )
  15. {
  16. print ( "Usage: php -f test.php [OPTIONS] query words\n\n" );
  17. print ( "Options are:\n" );
  18. print ( "-h, --host <HOST>\tconnect to searchd at host HOST\n" );
  19. print ( "-p, --port\t\tconnect to searchd at port PORT\n" );
  20. print ( "-i, --index <IDX>\tsearch through index(es) specified by IDX\n" );
  21. print ( "-s, --sortby <CLAUSE>\tsort matches by 'CLAUSE' in sort_extended mode\n" );
  22. print ( "-S, --sortexpr <EXPR>\tsort matches by 'EXPR' DESC in sort_expr mode\n" );
  23. print ( "-a, --any\t\tuse 'match any word' matching mode\n" );
  24. print ( "-b, --boolean\t\tuse 'boolean query' matching mode\n" );
  25. print ( "-e, --extended\t\tuse 'extended query' matching mode\n" );
  26. print ( "-ph,--phrase\t\tuse 'exact phrase' matching mode\n" );
  27. print ( "-f, --filter <ATTR>\tfilter by attribute 'ATTR' (default is 'group_id')\n" );
  28. print ( "-v, --value <VAL>\tadd VAL to allowed 'group_id' values list\n" );
  29. print ( "-g, --groupby <EXPR>\tgroup matches by 'EXPR'\n" );
  30. print ( "-gs,--groupsort <EXPR>\tsort groups by 'EXPR'\n" );
  31. print ( "-d, --distinct <ATTR>\tcount distinct values of 'ATTR''\n" );
  32. print ( "-l, --limit <COUNT>\tretrieve COUNT matches (default: 20)\n" );
  33. exit;
  34. }
  35. $args = array();
  36. foreach ( $_SERVER["argv"] as $arg )
  37. $args[] = $arg;
  38. $q = "";
  39. $mode = SPH_MATCH_ALL;
  40. $host = "localhost";
  41. $port = 3312;
  42. $index = "*";
  43. $groupby = "";
  44. $groupsort = "@group desc";
  45. $filter = "group_id";
  46. $filtervals = array();
  47. $distinct = "";
  48. $sortby = "";
  49. $limit = 20;
  50. $ranker = SPH_RANK_PROXIMITY_BM25;
  51. for ( $i=0; $i<count($args); $i++ )
  52. {
  53. $arg = $args[$i];
  54. if ( $arg=="-h" || $arg=="--host" ) $host = $args[++$i];
  55. else if ( $arg=="-p" || $arg=="--port" ) $port = (int)$args[++$i];
  56. else if ( $arg=="-i" || $arg=="--index" ) $index = $args[++$i];
  57. else if ( $arg=="-s" || $arg=="--sortby" ) { $sortby = $args[++$i]; $sortexpr = ""; }
  58. else if ( $arg=="-S" || $arg=="--sortexpr" ) { $sortexpr = $args[++$i]; $sortby = ""; }
  59. else if ( $arg=="-a" || $arg=="--any" ) $mode = SPH_MATCH_ANY;
  60. else if ( $arg=="-b" || $arg=="--boolean" ) $mode = SPH_MATCH_BOOLEAN;
  61. else if ( $arg=="-e" || $arg=="--extended" ) $mode = SPH_MATCH_EXTENDED;
  62. else if ( $arg=="-e2" ) $mode = SPH_MATCH_EXTENDED2;
  63. else if ( $arg=="-ph"|| $arg=="--phrase" ) $mode = SPH_MATCH_PHRASE;
  64. else if ( $arg=="-f" || $arg=="--filter" ) $filter = $args[++$i];
  65. else if ( $arg=="-v" || $arg=="--value" ) $filtervals[] = $args[++$i];
  66. else if ( $arg=="-g" || $arg=="--groupby" ) $groupby = $args[++$i];
  67. else if ( $arg=="-gs"|| $arg=="--groupsort" ) $groupsort = $args[++$i];
  68. else if ( $arg=="-d" || $arg=="--distinct" ) $distinct = $args[++$i];
  69. else if ( $arg=="-l" || $arg=="--limit" ) $limit = (int)$args[++$i];
  70. else if ( $arg=="-r" )
  71. {
  72. $arg = strtolower($args[++$i]);
  73. if ( $arg=="bm25" ) $ranker = SPH_RANK_BM25;
  74. if ( $arg=="none" ) $ranker = SPH_RANK_NONE;
  75. if ( $arg=="wordcount" )$ranker = SPH_RANK_WORDCOUNT;
  76. }
  77. else
  78. $q .= $args[$i] . " ";
  79. }
  80. ////////////
  81. // do query
  82. ////////////
  83. $cl = new SphinxClient ();
  84. $cl->SetServer ( $host, $port );
  85. $cl->SetWeights ( array ( 100, 1 ) );
  86. $cl->SetMatchMode ( $mode );
  87. if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  88. if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  89. if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  90. if ( $sortexpr ) $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
  91. if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  92. if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  93. $cl->SetRankingMode ( $ranker );
  94. $cl->SetArrayResult ( true );
  95. $res = $cl->Query ( $q, $index );
  96. ////////////////
  97. // print me out
  98. ////////////////
  99. if ( $res===false )
  100. {
  101. print "Query failed: " . $cl->GetLastError() . ".\n";
  102. } else
  103. {
  104. if ( $cl->GetLastWarning() )
  105. print "WARNING: " . $cl->GetLastWarning() . "\n\n";
  106. print "Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.\n";
  107. print "Query stats:\n";
  108. if ( is_array($res["words"]) )
  109. foreach ( $res["words"] as $word => $info )
  110. print " '$word' found $info[hits] times in $info[docs] documents\n";
  111. print "\n";
  112. if ( is_array($res["matches"]) )
  113. {
  114. $n = 1;
  115. print "Matches:\n";
  116. foreach ( $res["matches"] as $docinfo )
  117. {
  118. print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";
  119. foreach ( $res["attrs"] as $attrname => $attrtype )
  120. {
  121. $value = $docinfo["attrs"][$attrname];
  122. if ( $attrtype & SPH_ATTR_MULTI )
  123. {
  124. $value = "(" . join ( ",", $value ) .")";
  125. } else
  126. {
  127. if ( $attrtype==SPH_ATTR_TIMESTAMP )
  128. $value = date ( "Y-m-d H:i:s", $value );
  129. }
  130. print ", $attrname=$value";
  131. }
  132. print "\n";
  133. $n++;
  134. }
  135. }
  136. }
  137. //
  138. // $Id$
  139. //
  140. ?>