search.php.in 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // input encoding
  30. if (!empty($ie) && !empty($q) && strcasecmp($ie, '@charset@'))
  31. {
  32. $q = XS::convert($q, $cs, $ie);
  33. $eu .= '&ie=' . $ie;
  34. }
  35. // output encoding
  36. if (!empty($oe) && strcasecmp($oe, '@charset@'))
  37. {
  38. function xs_output_encoding($buf)
  39. {
  40. return XS::convert($buf, $GLOBALS['oe'], '@charset@');
  41. }
  42. ob_start('xs_output_encoding');
  43. $eu .= '&oe=' . $oe;
  44. }
  45. else
  46. {
  47. $oe = '@charset@';
  48. }
  49. // recheck request parameters
  50. $q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
  51. $f = empty($f) ? '_all' : $f;
  52. ${'m_check'} = ($m == 'yes' ? ' checked' : '');
  53. ${'syn_check'} = ($syn == 'yes' ? ' checked' : '');
  54. ${'f_' . $f} = ' checked';
  55. ${'s_' . $s} = ' selected';
  56. // base url
  57. $bu = $_SERVER['SCRIPT_NAME'] . '?q=' . urlencode($_GET['q']) . '&m=' . $m . '&f=' . $f . '&s=' . $s . $eu;
  58. // other variable maybe used in tpl
  59. $count = $total = $search_cost = 0;
  60. $docs = $related = $corrected = $hot = array();
  61. $error = $pager = '';
  62. $total_begin = microtime(true);
  63. // perform the search
  64. try
  65. {
  66. $xs = new XS('@project@');
  67. $search = $xs->search;
  68. $search->setCharset('@charset@');
  69. if (empty($q))
  70. {
  71. // just show hot query
  72. $hot = $search->getHotQuery();
  73. }
  74. else
  75. {
  76. // fuzzy search
  77. $search->setFuzzy($m === 'yes');
  78. // synonym search
  79. $search->setAutoSynonyms($syn === 'yes');
  80. // set query
  81. if (!empty($f) && $f != '_all')
  82. {
  83. $search->setQuery($f . ':(' . $q . ')');
  84. }
  85. else
  86. {
  87. $search->setQuery($q);
  88. }
  89. // set sort
  90. if (($pos = strrpos($s, '_')) !== false)
  91. {
  92. $sf = substr($s, 0, $pos);
  93. $st = substr($s, $pos + 1);
  94. $search->setSort($sf, $st === 'ASC');
  95. }
  96. // set offset, limit
  97. $p = max(1, intval($p));
  98. $n = XSSearch::PAGE_SIZE;
  99. $search->setLimit($n, ($p - 1) * $n);
  100. // get the result
  101. $search_begin = microtime(true);
  102. $docs = $search->search();
  103. $search_cost = microtime(true) - $search_begin;
  104. // get other result
  105. $count = $search->getLastCount();
  106. $total = $search->getDbTotal();
  107. if ($xml !== 'yes')
  108. {
  109. // try to corrected, if resul too few
  110. if ($count < 1 || $count < ceil(0.001 * $total))
  111. $corrected = $search->getCorrectedQuery();
  112. // get related query
  113. $related = $search->getRelatedQuery();
  114. }
  115. // gen pager
  116. if ($count > $n)
  117. {
  118. $pb = max($p - 5, 1);
  119. $pe = min($pb + 10, ceil($count / $n) + 1);
  120. $pager = '';
  121. do
  122. {
  123. $pager .= ($pb == $p) ? '<strong>' . $p . '</strong>' : '<a href="' . $bu . '&p=' . $pb . '">[' . $pb . ']</a>';
  124. }
  125. while (++$pb < $pe);
  126. }
  127. }
  128. }
  129. catch (XSException $e)
  130. {
  131. $error = strval($e);
  132. }
  133. // calculate total time cost
  134. $total_cost = microtime(true) - $total_begin;
  135. // XML OUPUT
  136. if ($xml === 'yes' && !empty($q))
  137. {
  138. header("Content-Type: text/xml; charset=$oe");
  139. echo "<?xml version=\"1.0\" encoding=\"$oe\" ?>\n";
  140. echo "<xs:result count=\"$count\" total=\"$total\" cost=\"$total_cost\" xmlns:xs=\"http://www.xunsearch.com\">\n";
  141. if ($error !== '')
  142. echo " <error><![CDATA[" . $error . "]]></error>\n";
  143. foreach ($docs as $doc)
  144. {
  145. echo " <doc index=\"" . $doc->rank() . "\" percent=\"" . $doc->percent() . "%\">\n";
  146. foreach ($doc as $k => $v)
  147. {
  148. echo " <$k>";
  149. if (is_numeric($v))
  150. echo $v;
  151. else
  152. echo "\n <![CDATA[" . $v . "]]>\n ";
  153. echo "</$k>\n";
  154. }
  155. echo " </doc>\n";
  156. }
  157. echo "</xs:result>\n";
  158. exit(0);
  159. }
  160. // output the data
  161. include dirname(__FILE__) . '/search.tpl';