ubuntu安装xunsearch搜索引擎 # 安装文档: http://www.xunsearch.com/doc/php/guide/start.installation ## 常见问题: 1:configure: error: zlib.h not found 安装zlib。sudo apt-get install zlib1g-dev 2:如何启停服务 /var/xunsearch/bin/xs-ctl.sh restart 3:数据存在哪 所有的索引数据将被保存在下面这个目录中:/var/xunsearch/data 如需要转移到其它目录,请使用软链接。 4:非本地访问 如果您的 SDK 调用和 xunsearch 服务端不在同一服务器,请使用 -b inet 方式启动脚本, 并注意借助类似 iptables 的防火墙来控制 xunsearch 的 8383/8384 两个端口的访问权限。 启动脚本用法举例如下,以下均为合法使用方式: bin/xs-ctl.sh -b local start // 监听在本地回环地址 127.0.0.1 上 bin/xs-ctl.sh -b inet start // 监听在所有本地 IP 地址上 bin/xs-ctl.sh -b a.b.c.d start // 监听在指定 IP 上 bin/xs-ctl.sh -b unix start // 分别监听在 tmp/indexd.sock 和 tmp/searchd.sock # thinkphp如何集成: 1:引入sdk ``` composer require hightman/xunsearch ``` 2:根据需求创建配置文件 ``` project.name = teanet project.default_charset = utf-8 server.index = 123.206.121.171:8383 server.search = 123.206.121.171:8384 [id] type = id [name] type = title [subname] index = mixed [content] type = body [create_time] type = numeric [type] type = numeric ``` 3:集成sdk ``` true, //立即刷新索引 'setFuzzy' => true, //开启模糊搜索 'autoSynonyms' => true, //开启自动同义词搜索功能 ]; public function __construct($file, array $config = []) { parent::__construct($file); if (!empty($config)) { $this->config = array_merge($this->config, $config); } } /** * 设置配置属性 * $data string 设置配置属性键名 * $value bool 设置是否开启 * @return mixed */ public function setConfig($attr, $value) { if (isset($this->config[$attr])) { $this->config[$attr] = $value; return $this; } else { return false; } } /** * 添加索引数据 * $data array 一维||二维 * @return mixed */ public function addData(array $data) { if (!array($data)) { die('参数错误!'); } if (count($data) == count($data, 1)) { // 一维数组 $this->getIndex()->add(new \XSDocument($data)); } else { // 多维数组 foreach ($data as $v) { $this->getIndex()->add(new \XSDocument($v)); } } $this->refresh(); return $this->getIndex(); } /** * 清除数据 */ public function clear(){ $this->getIndex()->clean(); $this->refresh(); } /** * 搜索方法 * * $string field * @return mixed */ public function searchAll($field) { if (!is_string($field)) { die('参数错误!'); } // other variable maybe used in tpl $count = $total = $search_cost = 0; $doc = $related = $corrected = $hot = []; $total_begin = microtime(true); $search = $this->getSearch(); //热门词汇 $hot = $search->getHotQuery(); // fuzzy search 模糊搜索 $search->setFuzzy($this->config['setFuzzy']); // synonym search $search->setAutoSynonyms($this->config['autoSynonyms']); // set query $search->setQuery($field); // get the result $search_begin = microtime(true); $doc = $search->search(); $search_cost = microtime(true) - $search_begin; // get other result $count = $search->getLastCount(); //最近一次搜索结果数 $total = $search->getDbTotal(); //数据库总数 $corrected = $this->getSearch()->getCorrectedQuery(); //模糊词搜索 if (count($doc) < 10) { foreach ($corrected as $v) { $doc = array_merge($doc, $this->getSearch()->search($v)); } } // try to corrected, if resul too few if ($count < 1 || $count < ceil(0.001 * $total)) { $corrected = $search->getCorrectedQuery(); } // get related query $related = $search->getRelatedQuery(); $total_cost = microtime(true) - $total_begin; //doc内对象转换为普通数据 foreach ($doc as $val) { $newDoc[] = $val->getFields(); } return [ 'doc' => $newDoc, //搜索数据结果文档 'hot' => $hot, //热门词汇 'count' => $count, //搜索结果统计 'total' => $total, //数据库总数据 'corrected' => $corrected, //搜索提示 'related' => $related, //相关搜索 'search_cost' => $search_cost, //搜索所用时间 'total_cost' => $total_cost, //页面所用时间 ]; } /** * 刷新 */ private function refresh() { if ($this->config['flushIndex']) { $this->getIndex()->flushIndex(); } } } ``` 4:引用方法 ``` $xs_root = CONF_PATH . '/xunsearch/teanet.ini'; $xs = new Search($xs_root); $xs->getIndex()->beginRebuild(); $xs->addData($xunData); $xs->getIndex()->endRebuild(); ```