#217, 初步实现xunsearch的集成

This commit is contained in:
wlx 2012-12-16 08:40:08 +00:00
parent 486326e87e
commit bf1a0d61c5
4 changed files with 658 additions and 7 deletions

View File

@ -0,0 +1,38 @@
<?php
class SearchController extends Zend_Controller_Action
{
function preDispatch()
{
$this->db=Zend_Registry::get('db');
$this->view->config = Zend_Registry::get('config');
$this->messenger=$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $this->messenger->getMessages();
}
function indexAction()
{
$search=new Search();
$search->dosearch();
$this->view->hot=$search->hot;
$this->view->total_cost=$search->total_cost;
$this->view->count=$search->count;
$this->view->base_url=$search->base_url;
$this->view->error=$search->error;
$this->view->docs=$search->docs;
$this->view->search=$search->search;
$this->view->related=$search->related;
$this->view->pager=$search->pager;
$this->view->total=$search->total;
$this->view->expanded=$search->expanded;
}
function suggestAction()
{
$q = isset($_GET['term']) ? trim($_GET['term']) : '';
$q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
$search=new Search();
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
header("Content-Type: application/json; charset=utf-8");
echo $search->suggest($q);
}
}

View File

@ -0,0 +1,209 @@
<?php
$__ = array('q', 'm', 'f', 's', 'p', 'ie', 'oe', 'syn', 'xml');
foreach ($__ as $_)
$$_ = isset($_GET[$_]) ? $_GET[$_] : '';
// input encoding
if (!empty($ie) && !empty($q) && strcasecmp($ie, 'UTF-8'))
{
$q = XS::convert($q, $cs, $ie);
$eu .= '&ie=' . $ie;
}
// output encoding
if (!empty($oe) && strcasecmp($oe, 'UTF-8'))
{
function xs_output_encoding($buf)
{
return XS::convert($buf, $GLOBALS['oe'], 'UTF-8');
}
ob_start('xs_output_encoding');
$eu .= '&oe=' . $oe;
}
else
{
$oe = 'UTF-8';
}
// recheck request parameters
$q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
$f = empty($f) ? '_all' : $f;
${'m_check'} = ($m == 'yes' ? ' checked' : '');
${'syn_check'} = ($syn == 'yes' ? ' checked' : '');
${'f_' . $f} = ' checked';
${'s_' . $s} = ' selected';
$search=$this->search;
$this->headTitle($this->config->title->site);
$this->headTitle('首页');
$this->headTitle('搜索');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/search.css');
?>
<div class="outer">
<!-- search form -->
<form id="q-form" method="get">
<div id="q-input">
<input class="text" type="text" name="q" size="40" title="输入任意关键词皆可搜索" value="<?php echo htmlspecialchars($q); ?>" />
<input class="button" type="submit" value=" 搜索! " />
</div>
<div id="q-options">
<h4>选项</h4>
<ul>
<li><input type="radio" name="f" value="title" <?php if (!empty($f_title)) echo $f_title; ?> />Title</li>
<li><input type="radio" name="f" value="_all" <?php if (!empty($f__all)) echo $f__all; ?> />全文</li>
<li><input type="checkbox" name="m" value="yes" <?php echo $m_check; ?> />模糊搜索</li>
<li><input type="checkbox" name="syn" value="yes" <?php echo $syn_check; ?> />同义词</li>
<li>
<select name="s" size="1">
<option value="relevance">相关性</option>
</select>
排序
</li>
</ul>
</div>
</form>
<!-- hot search -->
<?php if (count($this->hot) > 0): ?>
<div id="hot-search" class="res_div2">
<h4>热门搜索</h4>
<ul>
<?php foreach($this->hot as $word => $freq): ?>
<li><a href="/search?q=<?php echo urlencode($word); ?>"><?php echo $word; ?></a><small>(<?php echo $freq; ?>)</small></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- begin search result -->
<?php if (!empty($q)): ?>
<!-- neck bar -->
<div id="res-neck">
大约有 <strong><?php echo number_format($this->count); ?></strong> 项符合查询结果,
库内数据总量为 <strong><?php echo number_format($this->total); ?></strong> 项。
(搜索耗时:<?php printf('%.4f', $this->search_cost); ?>秒 - 页面处理时间:<?php printf('%.4f', $this->total_cost); ?>秒)
[<a href="<?php echo "$this->base_url&xml=yes&p=$p" ;?> " target="_blank">XML</a>]
</div>
<!-- error -->
<?php if (!empty($this->error)): ?>
<div id="res-error"><strong>错误:</strong><?php echo $this->error; ?></div>
<?php endif; ?>
<!-- fixed query -->
<?php if (count($this->corrected) > 0): ?>
<div id="res-fixed" class="res_div2">
<h4>您是不是要找:</h4>
<ul>
<?php foreach ($this->corrected as $word): ?>
<li><a href="<?php echo $_SERVER['SCRIPT_NAME'] . '?q=' . urlencode($word); ?>"><?php echo $word; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (count($this->expanded) > 0): ?>
<div id="res-fixed" class="res_div2">
<h4>您是不是要找:</h4>
<ul>
<?php foreach ($this->expanded as $word): ?>
<li><a href="<?php echo $_SERVER['SCRIPT_NAME'] . '?q=' . urlencode($word); ?>"><?php echo $word; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- empty result -->
<?php if ($this->count === 0 && empty($this->error)): ?>
<div id="res-empty">
<p>找不到和 <strong><?php echo htmlspecialchars($q); ?></strong> 相符的内容或信息。建议您:</p>
<ul>
<li>请检查输入字词有无错误。</li>
<li>请换用另外的查询字词。</li>
<li>请改用较短、较为常见的字词。</li>
</ul>
</div>
<?php endif; ?>
<!-- result doc list -->
<div id="res-list">
<?php foreach ($this->docs as $doc): ?>
<div class="res-doc">
<h2>
<a href="/data/<?php echo $doc->uuid; ?>"><?php echo $doc->rank(); ?>. <?php echo $this->search->highlight(htmlspecialchars($doc->title)); ?></a>
<small>[<?php echo $doc->percent(); ?>%]</small>
</h2>
<p><?php echo $search->highlight(htmlspecialchars($doc->description)); ?></p>
<ul>
<li><span>metadata:</span> <?php echo $search->highlight(htmlspecialchars($doc->data)); ?></li>
</ul>
</div>
<?php endforeach; ?>
</div>
<!-- pager -->
<?php if (!empty($this->pager)): ?>
<div id="res-pager" class="res_div2">
<strong>分页:</strong>
<?php echo $this->pager; ?>
</div>
<?php endif; ?>
<!-- related query -->
<?php if (count($this->related) > 0): ?>
<div id="res-related" class="res_div2">
<h4>相关搜索</h4>
<ul>
<?php foreach ($this->related as $word): ?>
<li><a href="<?php echo '/search?q=' . urlencode($word); ?>"><?php echo $word; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- end search result -->
<?php endif; ?>
</div><!-- outer -->
<!-- load jquery from google -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/redmond/jquery-ui.css" type="text/css" media="all" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<!-- ready script -->
<script language="javascript">
$(function(){
// input tips
$('#q-input .text').focus(function(){
if ($(this).val() == $(this).attr('title')) {
$(this).val('').removeClass('tips');
}
}).blur(function(){
if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
$(this).addClass('tips').val($(this).attr('title'));
}
}).blur().autocomplete({
'source':'/search/suggest',
'select':function(ev,ui) {
$('#q-input .text').val(ui.item.label);
$('#q-form').submit();
}
});
// submit check
$('#q-form').submit(function(){
var $input = $('#q-input .text');
if ($input.val() == $input.attr('title')) {
alert('请先输入关键词');
$input.focus();
return false;
}
});
});
</script>

View File

@ -0,0 +1,198 @@
<?php
$__ = array('q', 'm', 'f', 's', 'p', 'ie', 'oe', 'syn', 'xml');
foreach ($__ as $_)
$$_ = isset($_GET[$_]) ? $_GET[$_] : '';
// input encoding
if (!empty($ie) && !empty($q) && strcasecmp($ie, 'UTF-8'))
{
$q = XS::convert($q, $cs, $ie);
$eu .= '&ie=' . $ie;
}
// output encoding
if (!empty($oe) && strcasecmp($oe, 'UTF-8'))
{
function xs_output_encoding($buf)
{
return XS::convert($buf, $GLOBALS['oe'], 'UTF-8');
}
ob_start('xs_output_encoding');
$eu .= '&oe=' . $oe;
}
else
{
$oe = 'UTF-8';
}
// recheck request parameters
$q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
$f = empty($f) ? '_all' : $f;
${'m_check'} = ($m == 'yes' ? ' checked' : '');
${'syn_check'} = ($syn == 'yes' ? ' checked' : '');
${'f_' . $f} = ' checked';
${'s_' . $s} = ' selected';
$search=$this->search;
$this->headTitle($this->config->title->site);
$this->headTitle('首页');
$this->headTitle('搜索');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/search.css');
?>
<div class="outer">
<!-- search form -->
<form id="q-form" method="get">
<div id="q-input">
<input class="text" type="text" name="q" size="40" title="输入任意关键词皆可搜索" value="<?php echo htmlspecialchars($q); ?>" />
<input class="button" type="submit" value=" 搜索! " />
</div>
<div id="q-options">
<h4>选项</h4>
<ul>
<li><input type="radio" name="f" value="title" <?php if (!empty($f_title)) echo $f_title; ?> />Title</li>
<li><input type="radio" name="f" value="_all" <?php if (!empty($f__all)) echo $f__all; ?> />全文</li>
<li><input type="checkbox" name="m" value="yes" <?php echo $m_check; ?> />模糊搜索</li>
<li><input type="checkbox" name="syn" value="yes" <?php echo $syn_check; ?> />同义词</li>
<li>
<select name="s" size="1">
<option value="relevance">相关性</option>
</select>
排序
</li>
</ul>
</div>
</form>
<!-- hot search -->
<?php if (count($this->hot) > 0): ?>
<div id="hot-search" class="res_div2">
<h4>热门搜索</h4>
<ul>
<?php foreach($this->hot as $word => $freq): ?>
<li><a href="/search?q=<?php echo urlencode($word); ?>"><?php echo $word; ?></a><small>(<?php echo $freq; ?>)</small></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- begin search result -->
<?php if (!empty($q)): ?>
<!-- neck bar -->
<div id="res-neck">
大约有 <strong><?php echo number_format($this->count); ?></strong> 项符合查询结果,
库内数据总量为 <strong><?php echo number_format($this->total); ?></strong> 项。
(搜索耗时:<?php printf('%.4f', $this->search_cost); ?>秒 - 页面处理时间:<?php printf('%.4f', $this->total_cost); ?>秒)
[<a href="<?php echo "$this->base_url&xml=yes&p=$p" ;?> " target="_blank">XML</a>]
</div>
<!-- error -->
<?php if (!empty($this->error)): ?>
<div id="res-error"><strong>错误:</strong><?php echo $this->error; ?></div>
<?php endif; ?>
<!-- fixed query -->
<?php if (count($this->corrected) > 0): ?>
<div id="res-fixed" class="res_div2">
<h4>您是不是要找:</h4>
<ul>
<?php foreach ($this->corrected as $word): ?>
<li><a href="<?php echo $_SERVER['SCRIPT_NAME'] . '?q=' . urlencode($word); ?>"><?php echo $word; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- empty result -->
<?php if ($this->count === 0 && empty($this->error)): ?>
<div id="res-empty">
<p>找不到和 <strong><?php echo htmlspecialchars($q); ?></strong> 相符的内容或信息。建议您:</p>
<ul>
<li>请检查输入字词有无错误。</li>
<li>请换用另外的查询字词。</li>
<li>请改用较短、较为常见的字词。</li>
</ul>
</div>
<?php endif; ?>
<!-- result doc list -->
<div id="res-list">
<?php foreach ($this->docs as $doc): ?>
<div class="res-doc">
<h2>
<a href="/data/<?php echo $doc->uuid; ?>"><?php echo $doc->rank(); ?>. <?php echo $this->search->highlight(htmlspecialchars($doc->title)); ?></a>
<small>[<?php echo $doc->percent(); ?>%]</small>
</h2>
<p><?php echo $search->highlight(htmlspecialchars($doc->description)); ?></p>
<ul>
<li><span>metadata:</span> <?php echo $search->highlight(htmlspecialchars($doc->data)); ?></li>
</ul>
</div>
<?php endforeach; ?>
</div>
<!-- pager -->
<?php if (!empty($this->pager)): ?>
<div id="res-pager" class="res_div2">
<strong>分页:</strong>
<?php echo $this->pager; ?>
</div>
<?php endif; ?>
<!-- related query -->
<?php if (count($this->related) > 0): ?>
<div id="res-related" class="res_div2">
<h4>相关搜索</h4>
<ul>
<?php foreach ($this->related as $word): ?>
<li><a href="<?php echo '/search?q=' . urlencode($word); ?>"><?php echo $word; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- end search result -->
<?php endif; ?>
</div><!-- outer -->
<!-- load jquery from google -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/redmond/jquery-ui.css" type="text/css" media="all" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<!-- ready script -->
<script language="javascript">
$(function(){
// input tips
$('#q-input .text').focus(function(){
if ($(this).val() == $(this).attr('title')) {
$(this).val('').removeClass('tips');
}
}).blur(function(){
if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
$(this).addClass('tips').val($(this).attr('title'));
}
}).blur().autocomplete({
'source':'suggest.php',
'select':function(ev,ui) {
$('#q-input .text').val(ui.item.label);
$('#q-form').submit();
}
});
// submit check
$('#q-form').submit(function(){
var $input = $('#q-input .text');
if ($input.val() == $input.attr('title')) {
alert('请先输入关键词');
$input.focus();
return false;
}
});
});
</script>

View File

@ -3,12 +3,21 @@ class Search
{
var $terms;
var $text;
function Search($text)
{
$this->terms = array();
$this->text=$text;
}
var $xs;
var $search;
var $error;
var $pager;
var $count;
var $related;
var $corrected;
var $total;
var $search_cost;
var $docs;
var $hot;
var $base_url;
var $expanded;
//require_once '/home/wlx/xunsearch/sdk/php/lib/XS.php';
private function safe_query($search)
{
@ -54,4 +63,201 @@ class Search
}
return $sql;
}
}
function __construct($text='')
{
require_once '/home/wlx/xunsearch/sdk/php/lib/XS.php';
$this->xs=new XS('westdc');
$this->search=$this->xs->search;
$this->terms = array();
$this->text=$text;
// other variable maybe used
$this->count = $this->total = $this->search_cost = 0;
$this->docs = $this->related = $this->corrected =$this->expanded = $this->hot = array();
$this->error = $this->pager = $this->base_url='';
}
function dosearch()
{
//
// 支持的 GET 参数列表
// q: 查询语句
// m: 开启模糊搜索,其值为 yes/no
// f: 只搜索某个字段,其值为字段名称,要求该字段的索引方式为 self/both
// s: 排序字段名称及方式其值形式为xxx_ASC 或 xxx_DESC
// p: 显示第几页,每页数量为 XSSearch::PAGE_SIZE 即 10 条
// ie: 查询语句编码,默认为 UTF-8
// oe: 输出编码,默认为 UTF-8
// xml: 是否将搜索结果以 XML 格式输出,其值为 yes/no
//
// variables
$eu = '';
$__ = array('q', 'm', 'f', 's', 'p', 'ie', 'oe', 'syn', 'xml');
foreach ($__ as $_)
$$_ = isset($_GET[$_]) ? $_GET[$_] : '';
// recheck request parameters
$q = get_magic_quotes_gpc() ? stripslashes($q) : $q;
$f = empty($f) ? '_all' : $f;
${'m_check'} = ($m == 'yes' ? ' checked' : '');
${'syn_check'} = ($syn == 'yes' ? ' checked' : '');
${'f_' . $f} = ' checked';
${'s_' . $s} = ' selected';
if (!isset($q)) $q='';
// base url
$this->base_url = '/search?q=' . urlencode($q) . '&m=' . $m . '&f=' . $f . '&s=' . $s . $eu;
$total_begin = microtime(true);
// perform the search
try
{
$this->search->setCharset('UTF-8');
if (empty($q))
{
// just show hot query
$this->hot = $this->search->getHotQuery();
}
else
{
// fuzzy search
$this->search->setFuzzy($m === 'yes');
// synonym search
$this->search->setAutoSynonyms($syn === 'yes');
// set query
if (!empty($f) && $f != '_all')
{
$this->search->setQuery($f . ':(' . $q . ')');
}
else
{
$this->search->setQuery($q);
}
// set sort
if (($pos = strrpos($s, '_')) !== false)
{
$sf = substr($s, 0, $pos);
$st = substr($s, $pos + 1);
$this->search->setSort($sf, $st === 'ASC');
}
// set offset, limit
$p = max(1, intval($p));
$n = XSSearch::PAGE_SIZE;
$this->search->setLimit($n, ($p - 1) * $n);
// get the result
$search_begin = microtime(true);
$this->docs = $this->search->search();
$this->search_cost = microtime(true) - $search_begin;
// get other result
$this->count = $this->search->getLastCount();
$this->total = $this->search->getDbTotal();
if ($xml !== 'yes')
{
if ($this->count<1)
$this->expanded=$this->search->getExpandedQuery($q);
// try to corrected, if resul too few
if ($this->count < 1 || $this->count < ceil(0.001 * $this->total))
$this->corrected = $this->search->getCorrectedQuery();
// get related query
$this->related = $this->search->getRelatedQuery();
}
// gen pager
if ($this->count > $n)
{
$pb = max($p - 5, 1);
$pe = min($pb + 10, ceil($this->count / $n) + 1);
$this->pager = '';
do
{
$this->pager .= ($pb == $p) ? '<strong>' . $p . '</strong>' : '<a href="' . $this->base_url . '&p=' . $pb . '">[' . $pb . ']</a>';
}
while (++$pb < $pe);
}
}
}
catch (XSException $e)
{
$this->error = strval($e);
}
// calculate total time cost
$this->total_cost = microtime(true) - $total_begin;
// XML OUPUT
if ($xml === 'yes' && !empty($q))
{
header("Content-Type: text/xml; charset=$oe");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
echo "<xs:result count=\"$this->count\" total=\"$this->total\" cost=\"$this->total_cost\" xmlns:xs=\"http://www.xunsearch.com\">\n";
if ($this->error !== '')
echo " <error><![CDATA[" . $this->error . "]]></error>\n";
foreach ($this->docs as $doc)
{
echo " <doc index=\"" . $doc->rank() . "\" percent=\"" . $doc->percent() . "%\">\n";
foreach ($doc as $k => $v)
{
echo " <$k>";
if (is_numeric($v))
echo $v;
else
echo "\n <![CDATA[" . $v . "]]>\n ";
echo "</$k>\n";
}
echo " </doc>\n";
}
echo "</xs:result>\n";
exit(0);
}
}
//搜索建议
function suggest($q)
{
$terms = array();
if (!empty($q) && strpos($q, ':') === false)
{
try {
$terms = $this->search->setCharset('UTF-8')->getExpandedQuery($q);
} catch (XSException $e) { }
}
return json_encode($terms);
}
//添加新文档
//$data: 数组
function add($data)
{
$doc=new XSDocument;
$index=$this->xs->index;
$doc->setFields($data);
$index->add($doc);
}
//更新已有文档
//$data: 数组
function update($data)
{
$doc=new XSDocument;
$index=$this->xs->index;
$doc->setFields($data);
$index->update($doc);
}
//根据主键删除对应的索引
function del($data,$field='')
{
$index=$this->xs->index;
if (empty($field))
$index->del($data);
else {
$index->del($data,$field);
}
}
}