打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
PHP读取sphinx实例
丶平上
>《php》
2016.12.15
关注
php调用sphinx示例:
[php]
view plain
copy
<?php
$s
=
new
SphinxClient;
$s
->setServer(
"127.0.0.1"
, 9312);
$s
->setMatchMode(SPH_MATCH_PHRASE);
$s
->setMaxQueryTime(30);
$res
=
$s
->query(
"宝马"
,
'main'
); #[宝马]关键字,[main]数据源source
$err
=
$s
->GetLastError();
var_dump(
array_keys
(
$res
[
'matches'
]));
echo
"<br>"
.
"通过获取的ID来读取数据库中的值即可。"
.
"<br>"
;
echo
'<pre>'
;
var_dump(
$res
);
var_dump(
$err
);
echo
'</pre>'
;
调用示例二:支持分页
[php]
view plain
copy
<?php
header(
"Content-type: text/html; charset=utf-8"
);
require
(
"./sphinxapi.php"
);
$s
=
new
SphinxClient;
$s
->setServer(
"192.168.252.132"
, 9312);
//SPH_MATCH_ALL, 匹配所有查询词(默认模式); SPH_MATCH_ANY, 匹配查询词中的任意一个; SPH_MATCH_EXTENDED2, 支持特殊运算符查询
$s
->setMatchMode(SPH_MATCH_ALL);
$s
->setMaxQueryTime(30);
//设置最大搜索时间
$s
->SetArrayResult(false);
//是否将Matches的key用ID代替
$s
->SetSelect (
"*"
);
//设置返回信息的内容,等同于SQL
$s
->SetRankingMode(SPH_RANK_BM25);
//设置评分模式,SPH_RANK_BM25可能使包含多个词的查询的结果质量下降。
//$s->SetSortMode(SPH_SORT_EXTENDED); //发现增加此参数会使结果不准确
//$s->SetSortMode(SPH_SORT_EXTENDED,"from_id asc,id desc"); //设置匹配项的排序模式, SPH_SORT_EXTENDED按一种类似SQL的方式将列组合起来,升序或降序排列。
$weights
=
array
(
'company_name'
=> 20);
$s
->SetFieldWeights(
$weights
);
//设置字段权重
$s
->SetLimits ( 0, 30, 1000, 0 );
//设置结果集偏移量 SetLimits (便宜量,匹配项数目,查询的结果集数默认1000,阀值达到后停止)
//$s->SetFilter ( $attribute, $values, $exclude=false ); //设置属性过滤
//$s->SetGroupBy ( $attribute, $func, $groupsort="@group desc" ); //设置分组的属性
$res
=
$s
->query(
'@* "汽车"'
,
'main'
,
'--single-0-query--'
); #[宝马]关键字,[news]数据源source
//代码高亮
$tags
=
array
();
$tags_name
=
array
();
foreach
(
$res
[
'matches'
]
as
$key
=>
$value
){
$tags
[] =
$value
[
'attrs'
];
$company_name
[] =
$value
[
'attrs'
][
'company_name'
];
$description
[] =
$value
[
'attrs'
][
'description'
];
}
$company_name
=
$s
->BuildExcerpts (
$company_name
,
'main'
,
'汽车'
,
$opts
=
array
() );
//执行高亮,这里索引名字千万不能用*
$description
=
$s
->BuildExcerpts (
$description
,
'main'
,
'汽车'
,
$opts
=
array
() );
//执行高亮,这里索引名字千万不能用*
foreach
(
$tags
as
$k
=>
$v
)
{
$tags
[
$k
][
'company_name'
] =
$company_name
[
$k
];
//高亮后覆盖
$tags
[
$k
][
'description'
] =
$description
[
$k
];
//高亮后覆盖
}
// 高亮后覆盖
$i
= 0;
foreach
(
$res
[
'matches'
]
as
$key
=>
$value
){
$res
[
'matches'
][
$key
] =
$tags
[
$i
];
$i
++;
}
$err
=
$s
->GetLastError();
echo
'<pre>'
;
var_export(
$res
);
var_export(
$err
);
echo
'</pre>'
;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
PHP 正则表达式
老赛棍寒假复习计划——反序列化篇(一)
PHP正则表达式基础入门
Sphinx速成指南
PHP正则替换函数preg
自己架设一个监控宝
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×