jquery plugin-Live Search 2.0 使用心得
office websiet: http://andreaslagerkvist.com/jquery/live-search/
1.install
download jquery.liveSearch.js jquery.liveSearch.css
3.seaver handler:
对于大的数据库,将数据装载到一个没有 FULLTEXT 索引的表中,然后再使用 ALTER TABLE (或 CREATE INDEX) 创建索引,这将是非常快的。将数据装载到一个已经有 FULLTEXT 索引的表中,将是非常慢的。
1.使用Mysql全文检索fulltext的先决条件
表的类型必须是MyISAM
建立全文检索的字段类型必须是char,varchar,text
2建立全文检索
在建表中用FullText关键字标识字段,已存在的表用 ALTER TABLE (或 CREATE INDEX) 创建索引
CREATE fulltext INDEX index_name ON table_name(colum_name);
3.使用全文检索
在SELECT的WHERE字句中用MATCH函数,索引的关键词用AGAINST标识,IN BOOLEAN MODE是只有含有关键字就行,不用在乎位置,是不是起启位置.
SELECT * FROM articles WHERE MATCH (tags) AGAINST ('旅游' IN BOOLEAN MODE);
php in server:
<?php
require_once 'JSON.php';
# Your site URL
$site = 'exscale.se';
# Make sure a search-query was entered
if(isset($_GET['q'])) {
# Are we on the first page?
$start = isset($_GET['start']) ? $_GET['start'] : 0;
# The URL to the Google search API
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' .urlencode($_GET['q']) .'%20site:' .$site .'&rsz=large&start=' .$start;
# Initiate CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://' .$site);
$body = curl_exec($ch);
curl_close($ch);
# Decode the JSON returned (you need the JSON-class if json_decode is undefined)
$body = json_decode($body);
# Build array
$i = 0;
$search_results = array();
# Loop through all results
foreach($body->responseData->results as $r) {
$search_results['results'][$i]['title'] = $r->title;
$search_results['results'][$i]['url'] = $r->url;
$search_results['results'][$i]['content'] = $r->content;
$i++;
}
# Loop through all the pages of results
if(isset($body->responseData->cursor->pages)) {
foreach($body->responseData->cursor->pages as $p) {
$search_results['pages'][] = $p->start;
}
}
# Make sure some results were returned
if(isset($search_results['results'])) {
?>
<h2>Search results for "<?php echo @$_GET['q']; ?>"</h2>
<ol<?php echo @$_GET['start'] > 0 ? ' start="' .($_GET['start'] + 1) .'"' : '' ?>>
<?php foreach($search_results['results'] as $sr) { ?>
<li>
<h3><a href="<?php echo $sr['url']; ?>"><?php echo $sr['title']; ?></a></h3>
<p><?php echo $sr['content']; ?><br /><a href="<?php echo $sr['url']; ?>">Read more</a></p>
</li>
<?php } ?>
</ol>
<?php if(isset($search_results['pages'])) { ?>
<ul>
<?php $i = 0; foreach($search_results['pages'] as $p) { $i++; ?>
<li>
<?php if((!isset($_GET['start']) && $p == 0) || (@$_GET['start'] == $p)) { ?>
<strong><?php echo $i; ?></strong>
<?php } else { ?>
<a href="/search/?q=<?php echo @$_GET['q']; ?>&start=<?php echo $p; ?>"><?php echo $i; ?></a>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php
}
else {
?>
<p><strong>Sorry, there were no results</strong></p>
<?php
}
}
?>
error:#1191 - Can't find FULLTEXT index matching the column list:
fix:add fulltext index. if your have more than 1 column,you need set all column in where.
No comments:
Post a Comment