function search($string, $index_name = NULL)
{
$string = urldecode($string);
//用PHPCWS中文分词扩展对输入的关键字进行切分
if (mb_strlen($string, "UTF-8") > 4) {
//大于4个汉字的关键词才调用中文分词接口
$string = mb_convert_encoding($string, "GBK", "UTF-8");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->httpcws_url . urlencode($string));
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
//3秒超时时间
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//返回内容不是输出
$string_tmp = curl_exec($ch);
curl_close($ch);
if ($string_tmp != "") {
$string = mb_convert_encoding($string_tmp, "UTF-8", "GBK");
}
}
$sphinx_query_oldarr = explode(" ", $string);
foreach ($sphinx_query_oldarr as $key => $value) {
if (substr($value, 0, 1) == "@" || $value == "|") {
$sphinx_query_newarr[$key] = $value;
//不加引号
} else {
if ($this->is_chinese($value, "GBK") == 1 || $this->is_chinese($value, "GBK") == 2) {
//如果该字符串全部是中文、中英文混合
$sphinx_query_newarr[$key] = "\"" . $value . "\"";
//关键字加上引号
} else {
$sphinx_query_newarr[$key] = $value;
//如果不是汉字则不加引号
}
}
}
$this->search_string = implode(" ", $sphinx_query_newarr);
unset($sphinx_query_oldarr, $sphinx_query_newarr);
if (NULL !== $index_name) {
//多个索引查询
if (is_array($index_name)) {
$index_names = '';
foreach ($index_name as &$label) {
if (isset($this->indexes[$label])) {
$index_names .= $this->indexes[$label] . ' ';
}
}
$this->index_name = $index_names;
} else {
if (isset($this->indexes[$index_name])) {
$this->index_name = $this->indexes[$index_name];
}
}
}
if (empty($this->index_name)) {
exit('没有设置该' . $this->index_name . '索引' . PHP_EOL);
}
//清除当前设置的过滤器
$this->sphinx->ResetFilters();
return $this;
}