xunsearch实战经验总结
一、定义好配置文件(非常关键)
a):如果需要做精确搜索建议对字段设定index=self,tokenizer = full,不然xunsearch会对字段做分词处理;
b):数字区间搜索需设定 type=numerice;
c):如需对字段进行模糊匹配tokenizer 可不设定,默认做scws分词,如字段存储的值为“,6,7,8,”想搜索包含6或8的数据;
d):配置文件必须包含一个type=id的主键字段
配置文件参考手册:http://www.xunsearch.com/doc/php/guide/ini.guide
生成配置文件:http://www.xunsearch.com/tools/iniconfig
二、搜索 addQueryString()
a):字段搜索
$this->_xs_search->addQueryString('style_sn'.':"'.$condition['style_sn'].'"');
b):IN搜索
foreach ($list as $value){ $query.="{$key}:{$value} OR "; } $this->_xs_search->addQueryString(rtrim($query,"OR "));
c):区间搜索
$this->_xs_search->addRange('shoucun',$condition['shoucun_min'],null); //大于最小值 $this->_xs_search->addRange('goods_price',$condition['price_min'],$condition['price_max']);
d):like搜索
不设定tokenizer默认支持模糊搜索
$this->_xs_search->addQueryString('style_sn'.':"'.$condition['style_sn'].'"');
e):排序
排序字段建议采用数字类型(非必须)
$order=array("goods_click"=>1); if (count($order) > 1) { $this->_xs_search->setMultiSort($order); //多个排序字段 } else { $this->_xs_search->setSort($order); }
d):分组 (setCollapse)
$this->_xs_search->setCollapse($this->_collapse,1)->setLimit($page_size,$begin);
f):返回总数不正确
//解决总条数(getLastCount()/count())统计不准确问题 //原理:重新查询一次并设定Limit,如果超过实际总数将返回正确的记录条数 $this->_xs_search->setCollapse($this->_collapse,1)->search(); $count = $this->_xs_search->setCollapse($this->_collapse,1)->getLastCount(); $total_page = ceil($count/$page_size); $begin = ($total_page-1)*$page_size; $this->_xs_search->setCollapse($this->_collapse,1)->setLimit($page_size,$begin); $this->_xs_search->setCollapse($this->_collapse,1)->search(); $count = $this->_xs_search->setCollapse($this->_collapse,1)->getLastCount(); $this->_indexer_count = $count;
注意:
如果出现10061错误或连接被拒绝,请在安装目录bin文件夹下执行此句命令:./xs-ctl.sh -b inet start