test.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <EXPR>\tsort matches by 'EXPR'\n" );
  22. print ( "-a, --any\t\tuse 'match any word' matching mode\n" );
  23. print ( "-b, --boolean\t\tuse 'boolean query' matching mode\n" );
  24. print ( "-e, --extended\t\tuse 'extended query' matching mode\n" );
  25. print ( "-ph,--phrase\t\tuse 'exact phrase' matching mode\n" );
  26. print ( "-f, --filter <ATTR>\tfilter by attribute 'ATTR' (default is 'group_id')\n" );
  27. print ( "-v, --value <VAL>\tadd VAL to allowed 'group_id' values list\n" );
  28. print ( "-g, --groupby <EXPR>\tgroup matches by 'EXPR'\n" );
  29. print ( "-gs,--groupsort <EXPR>\tsort groups by 'EXPR'\n" );
  30. print ( "-d, --distinct <ATTR>\tcount distinct values of 'ATTR''\n" );
  31. print ( "-l, --limit <COUNT>\tretrieve COUNT matches (default: 20)\n" );
  32. exit;
  33. }
  34. $args = array();
  35. foreach ( $_SERVER["argv"] as $arg )
  36. $args[] = $arg;
  37. $q = "";
  38. $mode = SPH_MATCH_ALL;
  39. $host = "localhost";
  40. $port = 3312;
  41. $index = "*";
  42. $groupby = "";
  43. $groupsort = "@group desc";
  44. $filter = "group_id";
  45. $filtervals = array();
  46. $distinct = "";
  47. $sortby = "";
  48. $limit = 20;
  49. $ranker = SPH_RANK_PROXIMITY_BM25;
  50. for ( $i=0; $i<count($args); $i++ )
  51. {
  52. $arg = $args[$i];
  53. if ( $arg=="-h" || $arg=="--host" ) $host = $args[++$i];
  54. else if ( $arg=="-p" || $arg=="--port" ) $port = (int)$args[++$i];
  55. else if ( $arg=="-i" || $arg=="--index" ) $index = $args[++$i];
  56. else if ( $arg=="-s" || $arg=="--sortby" ) $sortby = $args[++$i];
  57. else if ( $arg=="-a" || $arg=="--any" ) $mode = SPH_MATCH_ANY;
  58. else if ( $arg=="-b" || $arg=="--boolean" ) $mode = SPH_MATCH_BOOLEAN;
  59. else if ( $arg=="-e" || $arg=="--extended" ) $mode = SPH_MATCH_EXTENDED;
  60. else if ( $arg=="-e2" ) $mode = SPH_MATCH_EXTENDED2;
  61. else if ( $arg=="-ph"|| $arg=="--phrase" ) $mode = SPH_MATCH_PHRASE;
  62. else if ( $arg=="-f" || $arg=="--filter" ) $filter = $args[++$i];
  63. else if ( $arg=="-v" || $arg=="--value" ) $filtervals[] = $args[++$i];
  64. else if ( $arg=="-g" || $arg=="--groupby" ) $groupby = $args[++$i];
  65. else if ( $arg=="-gs"|| $arg=="--groupsort" ) $groupsort = $args[++$i];
  66. else if ( $arg=="-d" || $arg=="--distinct" ) $distinct = $args[++$i];
  67. else if ( $arg=="-l" || $arg=="--limit" ) $limit = $args[++$i];
  68. else if ( $arg=="-r" )
  69. {
  70. $arg = strtolower($args[++$i]);
  71. if ( $arg=="bm25" ) $ranker = SPH_RANK_BM25;
  72. if ( $arg=="none" ) $ranker = SPH_RANK_NONE;
  73. }
  74. else
  75. $q .= $args[$i] . " ";
  76. }
  77. ////////////
  78. // do query
  79. ////////////
  80. $cl = new SphinxClient ();
  81. $cl->SetServer ( $host, $port );
  82. $cl->SetWeights ( array ( 100, 1 ) );
  83. $cl->SetMatchMode ( $mode );
  84. if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  85. if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  86. if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  87. if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  88. if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  89. $cl->SetRankingMode ( $ranker );
  90. $res = $cl->Query ( $q, $index );
  91. ////////////////
  92. // print me out
  93. ////////////////
  94. if ( $res===false )
  95. {
  96. print "Query failed: " . $cl->GetLastError() . ".\n";
  97. } else
  98. {
  99. if ( $cl->GetLastWarning() )
  100. print "WARNING: " . $cl->GetLastWarning() . "\n\n";
  101. print "Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.\n";
  102. print "Query stats:\n";
  103. if ( is_array($res["words"]) )
  104. foreach ( $res["words"] as $word => $info )
  105. print " '$word' found $info[hits] times in $info[docs] documents\n";
  106. print "\n";
  107. if ( is_array($res["matches"]) )
  108. {
  109. $n = 1;
  110. print "Matches:\n";
  111. foreach ( $res["matches"] as $doc => $docinfo )
  112. {
  113. print "$n. doc_id=$doc, weight=$docinfo[weight]";
  114. foreach ( $res["attrs"] as $attrname => $attrtype )
  115. {
  116. $value = $docinfo["attrs"][$attrname];
  117. if ( $attrtype & SPH_ATTR_MULTI )
  118. {
  119. $value = "(" . join ( ",", $value ) .")";
  120. } else
  121. {
  122. if ( $attrtype==SPH_ATTR_TIMESTAMP )
  123. $value = date ( "Y-m-d H:i:s", $value );
  124. }
  125. print ", $attrname=$value";
  126. }
  127. print "\n";
  128. $n++;
  129. }
  130. }
  131. }
  132. //
  133. // $Id$
  134. //
  135. ?>