搜索服务器xunsearch实现

时间:2023-03-09 04:10:39
搜索服务器xunsearch实现

安装方法:

  centos 6.6 64位

  histroy:

  12  cd /srv/
  13  wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2
  14  tar -xjf xunsearch-full-latest.tar.bz2
  15  cd cd xunsearch-full-1.3.0/
  16  sh setup.sh
  17  cd xunsearch-full-1.4.9/
  18  sh setup.sh
  19  +=================================================+
  20  | Installation completed successfully, Thanks you |
  21  | 安装成功,感谢选择和使用 xunsearch              |
  22  +-------------------------------------------------+
  23  | 说明和注意事项:                                |
  24  | 1. 开启/重新开启 xunsearch 服务程序,命令如下: |
  25  |    /home/bcenter/xunsearch/bin/xs-ctl.sh restart
  26  |    强烈建议将此命令写入服务器开机脚本中         |
  27  |                                                 |
  28  | 2. 所有的索引数据将被保存在下面这个目录中:     |
  29  |    /home/bcenter/xunsearch/data
  30  |    如需要转移到其它目录,请使用软链接。         |
  31  |                                                 |
  32  | 3. 您现在就可以在我们提供的开发包(SDK)基础上    |
  33  |    开发您自己的搜索了。                         |
  34  |    目前只支持 PHP 语言,参见下面文档:          |
  35  |    /home/bcenter/xunsearch/sdk/php/README

 

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
date_default_timezone_set('prc');

$prefix = "/home/bcenter/xunsearch";  //xunsearch安装的位置
 
require_once "$prefix/sdk/php/lib/XS.php";
$xs = new XS('demo');   // 自动使用 $prefix/sdk/php/app/demo.ini 作项目配置文件
$xs = new XS('/home/bcenter/xunsearch/sdk/php/app/demo.ini');  // 使用 /path/to/demo.ini
$index = $xs->index; // 获取 索引对象
/*

$data = array(
    'pid' => 234, // 此字段为主键,是进行文档替换的唯一标识
    'subject' => '测试文档的标题',
    'message' => '测试文档的内容部分',
    'chrono' => time()
);
 
// 创建文档对象
$doc = new XSDocument;
$doc->setFields($data);
 
// 更新到索引数据库中
$index->update($doc);
*/

$docs = $xs->search->search('测试');
foreach ($docs as $doc)
{
   // 其中常用魔术方法:percent() 表示匹配度百分比, rank() 表示匹配结果序号
   echo $doc->rank() . '. ' . $doc->subject . " [" . $doc->percent() . "%] - ";
   echo   $doc->pid . "\n" . $doc->message . "\n";
}

 
?>

=====

屏幕输出:

1. 测试文档的标题 [100%] - 234 测试文档的内容部分

 

 

\home\bcenter\xunsearch\sdk\php\app\demo.ini

code:

project.name = demo
project.default_charset = utf-8
server.index = 127.0.0.1:8383  #先配置地址,不然运行php报错。
server.search = 127.0.0.1:8384

[pid]
type = id

[subject]
type = title

[message]
type = body

[chrono]
type = numeric

 

说明: 此练习是简单使用demo。链接见:

http://www.xunsearch.com/doc/php/guide/start.changelog