search.php.in 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * search.php
  4. * @project_name@ 搜索项目入口文件
  5. *
  6. * 该文件由 xunsearch PHP-SDK 工具自动生成,请根据实际需求进行修改
  7. * 创建时间:@date_time@
  8. * 默认编码:@charset@
  9. */
  10. // 加载 XS 入口文件
  11. require_once '@xs_lib_root@/XS.php';
  12. error_reporting(E_ALL ^ E_NOTICE);
  13. //
  14. // 支持的 GET 参数列表
  15. // q: 查询语句
  16. // m: 开启模糊搜索,其值为 yes/no
  17. // f: 只搜索某个字段,其值为字段名称,要求该字段的索引方式为 self/both
  18. // s: 排序字段名称及方式,其值形式为:xxx_ASC 或 xxx_DESC
  19. // p: 显示第几页,每页数量为 XSSearch::PAGE_SIZE 即 10 条
  20. // ie: 查询语句编码,默认为 @charset@
  21. // oe: 输出编码,默认为 @charset@
  22. // xml: 是否将搜索结果以 XML 格式输出,其值为 yes/no
  23. //
  24. // variables
  25. $eu = '';
  26. $__ = array('q', 'm', 'f', 's', 'p', 'ie', 'oe', 'syn', 'xml');
  27. foreach ($__ as $_) {
  28. $$_ = isset($_GET[$_]) ? $_GET[$_] : '';
  29. }
  30. // input encoding
  31. if (!empty($ie) && !empty($q) && strcasecmp($ie, '@charset@')) {
  32. $q = XS::convert($q, $cs, $ie);
  33. $eu .= '&ie=' . $ie;
  34. }
  35. // output encoding
  36. if (!empty($oe) && strcasecmp($oe, '@charset@')) {
  37. function xs_output_encoding($buf)
  38. {
  39. return XS::convert($buf, $GLOBALS['oe'], '@charset@');
  40. }
  41. ob_start('xs_output_encoding');
  42. $eu .= '&oe=' . $oe;
  43. } else {
  44. $oe = '@charset@';
  45. }
  46. // recheck request parameters
  47. $q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
  48. $f = empty($f) ? '_all' : $f;
  49. ${'m_check'} = ($m == 'yes' ? ' checked' : '');
  50. ${'syn_check'} = ($syn == 'yes' ? ' checked' : '');
  51. ${'f_' . $f} = ' checked';
  52. ${'s_' . $s} = ' selected';
  53. // base url
  54. $bu = $_SERVER['SCRIPT_NAME'] . '?q=' . urlencode($q) . '&m=' . $m . '&f=' . $f . '&s=' . $s . $eu;
  55. // other variable maybe used in tpl
  56. $count = $total = $search_cost = 0;
  57. $docs = $related = $corrected = $hot = array();
  58. $error = $pager = '';
  59. $total_begin = microtime(true);
  60. // perform the search
  61. try {
  62. $xs = new XS('@project@');
  63. $search = $xs->search;
  64. $search->setCharset('@charset@');
  65. if (empty($q)) {
  66. // just show hot query
  67. $hot = $search->getHotQuery();
  68. } else {
  69. // fuzzy search
  70. $search->setFuzzy($m === 'yes');
  71. // synonym search
  72. $search->setAutoSynonyms($syn === 'yes');
  73. // set query
  74. if (!empty($f) && $f != '_all') {
  75. $search->setQuery($f . ':(' . $q . ')');
  76. } else {
  77. $search->setQuery($q);
  78. }
  79. // set sort
  80. if (($pos = strrpos($s, '_')) !== false) {
  81. $sf = substr($s, 0, $pos);
  82. $st = substr($s, $pos + 1);
  83. $search->setSort($sf, $st === 'ASC');
  84. }
  85. // set offset, limit
  86. $p = max(1, intval($p));
  87. $n = XSSearch::PAGE_SIZE;
  88. $search->setLimit($n, ($p - 1) * $n);
  89. // get the result
  90. $search_begin = microtime(true);
  91. $docs = $search->search();
  92. $search_cost = microtime(true) - $search_begin;
  93. // get other result
  94. $count = $search->getLastCount();
  95. $total = $search->getDbTotal();
  96. if ($xml !== 'yes') {
  97. // try to corrected, if resul too few
  98. if ($count < 1 || $count < ceil(0.001 * $total)) {
  99. $corrected = $search->getCorrectedQuery();
  100. }
  101. // get related query
  102. $related = $search->getRelatedQuery();
  103. }
  104. // gen pager
  105. if ($count > $n) {
  106. $pb = max($p - 5, 1);
  107. $pe = min($pb + 10, ceil($count / $n) + 1);
  108. $pager = '';
  109. do {
  110. $pager .= ($pb == $p) ? '<li class="disabled"><a>' . $p . '</a></li>' : '<li><a href="' . $bu . '&p=' . $pb . '">' . $pb . '</a></li>';
  111. } while (++$pb < $pe);
  112. }
  113. }
  114. } catch (XSException $e) {
  115. $error = strval($e);
  116. }
  117. // calculate total time cost
  118. $total_cost = microtime(true) - $total_begin;
  119. // XML OUPUT
  120. if ($xml === 'yes' && !empty($q)) {
  121. header("Content-Type: text/xml; charset=$oe");
  122. echo "<?xml version=\"1.0\" encoding=\"$oe\" ?>\n";
  123. echo "<xs:result count=\"$count\" total=\"$total\" cost=\"$total_cost\" xmlns:xs=\"http://www.xunsearch.com\">\n";
  124. if ($error !== '') {
  125. echo " <error><![CDATA[" . $error . "]]></error>\n";
  126. }
  127. foreach ($docs as $doc) {
  128. echo " <doc index=\"" . $doc->rank() . "\" percent=\"" . $doc->percent() . "%\">\n";
  129. foreach ($doc as $k => $v) {
  130. echo " <$k>";
  131. echo is_numeric($v) ? $v : "\n <![CDATA[" . $v . "]]>\n ";
  132. echo "</$k>\n";
  133. }
  134. echo " </doc>\n";
  135. }
  136. echo "</xs:result>\n";
  137. exit(0);
  138. }
  139. // output the data
  140. include dirname(__FILE__) . '/search.tpl';