您现在的位置是:首页 > 个人博客日记 个人博客日记

php使用sphinx

2017-04-27 00:00:00

1.安装sphinx
sphinx的安装和使用

2.使用sphinx php api测试:

  1. <?php
  2. require ( "sphinxapi.php" ); //此文件在sphinx源代码的api文件夹中可以找到
  3. $cl = new SphinxClient ();
  4. $q = "weibo";
  5. $sql = "test1";
  6. $mode = SPH_MATCH_ALL;
  7. $host = "localhost";
  8. $port = 9312;
  9. $index = "*";
  10. // $groupby = "";
  11. // $groupsort = "@group desc";
  12. // $filter = "group_id";
  13. // $filtervals = array();
  14. // $distinct = "";
  15. // $sortby = "";
  16. // $sortexpr = "";
  17. // $limit = 20;
  18. // $ranker = SPH_RANK_PROXIMITY_BM25;
  19. // $select = "";
  20. $cl->SetServer ( $host, $port );
  21. $cl->SetConnectTimeout ( 1 );
  22. $cl->SetArrayResult ( true );
  23. $cl->SetMatchMode ( $mode );
  24. // if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  25. // if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  26. // if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  27. // if ( $sortexpr ) $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
  28. // if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  29. // if ( $select ) $cl->SetSelect ( $select );
  30. // if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  31. //$cl->SetRankingMode ( $ranker );
  32. $res = $cl->Query ( $q, $index );
  33. ////////////////
  34. // print me out
  35. ////////////////
  36. if ( $res===false )
  37. {
  38. print "Query failed: " . $cl->GetLastError() . ".<br/>";
  39. } else
  40. {
  41. if ( $cl->GetLastWarning() )
  42. print "WARNING: " . $cl->GetLastWarning() . "<br/><br/>";
  43. print "Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.<br/>";
  44. print "Query stats:<br/>";
  45. if ( is_array($res["words"]) )
  46. foreach ( $res["words"] as $word => $info )
  47. print " '$word' found $info[hits] times in $info[docs] documents\n";
  48. print "<br/>";
  49. if ( is_array($res["matches"]) )
  50. {
  51. $n = 1;
  52. print "Matches:<br/>";
  53. foreach ( $res["matches"] as $docinfo )
  54. {
  55. print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";
  56. foreach ( $res["attrs"] as $attrname => $attrtype )
  57. {
  58. $value = $docinfo["attrs"][$attrname];
  59. if ( $attrtype==SPH_ATTR_MULTI || $attrtype==SPH_ATTR_MULTI64 )
  60. {
  61. $value = "(" . join ( ",", $value ) .")";
  62. } else
  63. {
  64. if ( $attrtype==SPH_ATTR_TIMESTAMP )
  65. $value = date ( "Y-m-d H:i:s", $value );
  66. }
  67. print ", $attrname=$value";
  68. }
  69. print "<br/>";
  70. $n++;
  71. }
  72. }
  73. }
  74. ?>

打印的结果:

  1. Query 'weibo' retrieved 1000 of 3873 matches in 0.001 sec.
  2. Query stats:
  3. 'weibo' found 3873 times in 3873 documents
  4. Matches:
  5. 1. doc_id=1, weight=1, import_timestamp=2015-06-26 16:25:41
  6. 2. doc_id=4, weight=1, import_timestamp=2015-06-26 16:25:41
  7. 3. doc_id=5, weight=1, import_timestamp=2015-06-26 16:25:41
  8. 4. doc_id=6, weight=1, import_timestamp=2015-06-26 16:25:41
  9. 5. doc_id=8, weight=1, import_timestamp=2015-06-26 16:25:41
  10. 6. doc_id=9, weight=1, import_timestamp=2015-06-26 16:25:41
  11. 7. doc_id=10, weight=1, import_timestamp=2015-06-26 16:25:41
  12. 8. doc_id=11, weight=1, import_timestamp=2015-06-26 16:25:41
  13. 9. doc_id=12, weight=1, import_timestamp=2015-06-26 16:25:41
  14. 10. doc_id=13, weight=1, import_timestamp=2015-06-26 16:25:41
  15. 11. doc_id=14, weight=1, import_timestamp=2015-06-26 16:25:41
  16. 12. doc_id=15, weight=1, import_timestamp=2015-06-26 16:25:41
  17. 13. doc_id=16, weight=1, import_timestamp=2015-06-26 16:25:41
  18. 14. doc_id=17, weight=1, import_timestamp=2015-06-26 16:25:41
  19. 15. doc_id=18, weight=1, import_timestamp=2015-06-26 16:25:41
  20. 16. doc_id=19, weight=1, import_timestamp=2015-06-26 16:25:41
  21. 17. doc_id=20, weight=1, import_timestamp=2015-06-26 16:25:41
  22. 18. doc_id=21, weight=1, import_timestamp=2015-06-26 16:25:41
  23. 19. doc_id=22, weight=1, import_timestamp=2015-06-26 16:25:41
  24. 20. doc_id=23, weight=1, import_timestamp=2015-06-26 16:25:41


关注TinyMeng博客,更多精彩分享,敬请期待!
 

站点信息