yii 里sphinx使用
来源:互联网 发布:linq.js where 编辑:程序博客网 时间:2024/05/18 01:38
大家都知道sphinx自带的一些匹配模式。主要有setMatchMode:SPH_MATCH_ALL匹配所有查询词(默认模式)SPH_MATCH_ANY匹配查询词中的任意一个SPH_MATCH_PHRASE将整个查询看作一个词组,要求按顺序完整匹配SPH_MATCH_BOOLEAN将查询看作一个布尔表达式SPH_MATCH_EXTENDED将查询看作一个Sphinx内部查询语言的表达式SPH_MATCH_FULLSCAN使用完全扫描,忽略查询词汇SPH_MATCH_EXTENDED2类似 SPH_MATCH_EXTENDED ,并支持评分和权重.
控制器:
//下拉选项字段 搜索值
public function actionSearch_val()
{
$set = Yii::$app->request->get('set','');//接收搜索类型
$key = Yii::$app->request->get('key','');//接收值
if(yii::$app->request->isAjax){
$key1 = '';
require ( "sphinxapi.php" );//引入类
//echo $key.$set;die;
$cl = new \SphinxClient();
$cl->SetServer ( '127.0.0.1', 9312);
$cl->SetArrayResult ( true ); //果集(ResultSet)是数据中查询结果返回的一种对象,
if(empty($key)){
$cl->SetMatchMode ( SPH_MATCH_FULLSCAN ); //匹配模式使用完全扫描,忽略查询词汇
}else{
$cl->SetMatchMode ( SPH_MATCH_EXTENDED2 ); //使用多字段模式
if($set == 1){
$key1 = $key;
}else{
$key1 = '@'.$set.' ('.$key.')';
}
}
$res = $cl->Query ( $key1, "test2" );
if($res['total_found'] > 0){
$ids = [];
foreach ( $res['matches'] as $k => $row ) {
$ids[] = $row['id'];
}
$query = new Query();
$list = $query->from('position')->where(['in', 'id', $ids])->all();
//注意:from("自己的表名")
foreach($list as $k=>$v){
$list[$k]=str_replace($key,"<font color='red'>{$key}</font>",$v);
}
} else {
$list = [];
}
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; //yii 返回值为json 格式
return ['list' => $list ];
}
return $this->render('search2');
}
视图:
<?php
use yii\helpers\Html;
use yii\helpers\Url;
?>
<form action="" method="get">
<input type="hidden" name="_csrf" value="">
<div class="col-md-1">
<select id="set">
<option value="1">全部</option>
<option value="position">职位</option>
<option value="biaoqian">标签</option>
<option value="xueli">学历</option>
</select>
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder="keyword" id="key">
</div>
<button type="button" class="btn btn-default">Search</button>
</form>
<p>
<div id="content"></div>
<?php $this->beginBlock('index') ?>
$(function(){
$('.btn-default').click(function(){
var set = $('#set').val();
var key = $('#key').val();
var url = '<?php echo Url::toRoute(['hello/search_val'])?>';
$.getJSON(url, {'key':key, 'set':set}, function(data){
//alert(data);
console.log(data);
var lists= data.list;
//console.log(data.length)
var html = '<table class="table">';//这里用的是拼接
for(var i = 0; i < lists.length; i++ ) {
html += '<tr>';
//注意以下为数据表相对应的字段
html += '<td>'+lists[i].id+'</td>';
html += '<td>'+lists[i].position+'</td>';
html += '<td>'+lists[i].biaoqian+'</td>';
html += '<td>'+lists[i].xueli+'</td>';
html += '</tr>';
}
html += '</table>';
$('#content').html(html);
});
});
});
<?php $this->endBlock('index') ?>
<?php $this->registerJs($this->blocks['index'], \yii\web\View::POS_END);?>
- yii 里sphinx使用
- yii sphinx
- win下如何在yii框架下面使用sphinx
- Yii框架中使用sphinx搜索及关键字标红
- Sphinx的安装和yii框架的使用
- sphinx的简单使用及yii中应用
- 如何在yii引入sphinx
- sphinx 使用
- Sphinx使用
- yii 框架 的sphinx类引入 使用 及 关键字标红
- yii框架中sphinx单条件搜索
- sphinx在php文件里使用及返回数据的格式
- 使用sphinx写文档
- Coreseek SPHINX使用
- sphinx的使用
- sphinx 安装使用
- sphinx使用心得
- sphinx使用初级
- 信号和槽
- ZOJ 3747 Attack on Titans【带限制条件的递推dp+计数技巧】
- 【笔记】NEXUS 3.* 下载及安装, 与2.*稍有不同
- H3C笔试题:C语言旋转双向链表
- testng.xml文件结构
- yii 里sphinx使用
- Qt的元对象系统
- #ifdef #endif
- GoF 的23个经典设计模式
- 计算机磁盘是如何实现存储的?
- [题解]uoj228 基础数据结构练习题
- [科普向] 一些问题的解决办法 即算法思路。
- qmake
- 《Drools7.0.0.Final规则引擎教程》番外实例篇——activation-group的多FACT对象