sphinx xmlpipe2数据源配置

首先是一个工具类SphinxXMLFeed用于将数据写入xml:

此处是采用了hash 算法将数据写入10个xml,你可以根据自己需求改动,如果数据量不大只写入一个也行,只需写死文件名。

class SphinxXMLFeed extends XMLWriter
{
private $fields = array();
private $attributes = array();
public static $dir = “/var/log/”;
public function __construct($options = array())
{
$defaults = array(
‘indent’ => false,
);
$options = array_merge($defaults, $options);

// Store the xml tree in memory
$this->openMemory();

if($options[‘indent’]) {
$this->setIndent(true);
}
}

public function setFields($fields) {
$this->fields = $fields;
}

public function setAttributes($attributes) {
$this->attributes = $attributes;
}

public function addDocument($doc,$uid) {
$this->startElement(‘sphinx:document’);
$this->writeAttribute(‘id’, $doc[‘id’]);

foreach($doc as $key => $value) {
// Skip the id key since that is an element attribute
if($key == ‘id’) continue;

$this->startElement($key);
$this->text($value);
$this->endElement();
}

$this->endElement();
$this->save($this->outputMemory(),$uid);
}

public function beginOutput($uid) {

$this->startDocument(‘1.0′, ‘UTF-8′);
$this->startElement(‘sphinx:docset’);
$this->startElement(‘sphinx:schema’);

// add fields to the schema
foreach($this->fields as $field) {
$this->startElement(‘sphinx:field’);
$this->writeAttribute(‘name’, $field);
$this->endElement();
}

// add attributes to the schema
foreach($this->attributes as $attributes) {
$this->startElement(‘sphinx:attr’);
foreach($attributes as $key => $value) {
$this->writeAttribute($key, $value);
}
$this->endElement();
}

// end sphinx:schema
$this->endElement();
$this->save_first($this->outputMemory(),$uid);
}

public function endOutput($uid)
{
// end sphinx:docset
$this->endElement();
$this->save($this->outputMemory(),$uid);
}

public function save($data,$uid){
chmod(dirname(__FILE__), 0777); // 以最高操作权限操作当前目录
// 打开b.php文件,这里采用的是a+,也可以用a,a+为可读可写,a为只写,如果b.php不能存在则会创建它
$file = fopen(self::$dir.self::gethash($uid).”xml”, ‘a+’); //a模式就是一种追加模式,如果是w模式则会删除之前的内容再添加
// 获取需要写入的内容
// 写入追加的内容
fwrite($file, $data);
// 关闭b.php文件
fclose($file);
// 销毁文件资源句柄变量
unset($file);
}

public function save_first($data,$uid){
chmod(dirname(__FILE__), 0777); // 以最高操作权限操作当前目录
// 打开b.php文件,这里采用的是a+,也可以用a,a+为可读可写,a为只写,如果b.php不能存在则会创建它
$file = fopen(self::$dir.self::gethash($uid).”xml”, ‘w+’); //a模式就是一种追加模式,如果是w模式则会删除之前的内容再添加
// 获取需要写入的内容
// 写入追加的内容
fwrite($file, $data);
// 关闭b.php文件
fclose($file);
// 销毁文件资源句柄变量
unset($file);
}

public static function gethash($uid){
$count = 10;
$d =intval(base_convert(substr(md5($uid), 0, 5),16, 10)) %$count;
return “t_user”.’_’.$d;
}

 

 

然后是工具类的调用

 

$doc = new SphinxXMLFeed();
 
//配置sphinxfields字段 用于查询时过滤使用
$doc->setFields(array(
  'title',
  'teaser',
  'content',
));
 
//配置sphinx 的attr字段,搜索会返回该字段,这里q支持string的,大家可以试试
$doc->setAttributes(array(
  array('name' => 'blog_id', 'type' => 'int', 'bits' => '16', 'default' => '0'),
));
 
//以上2个配置必须在调用该方法之前,不在这里配置也行也可以在sphinx的配置文件里配置
//输出xml开头部分
$doc->beginOutput();  

//这里应该是从数据库取出的数据,为了方便我这里直接随机生成了1000条记录 foreach(range(1, 1000) as $id) { $doc->addDocument(array( 'id' => $id, 'blog_id' => rand(1, 10), 'title' => "Article Part {$id}", 'teaser' => "Article {$id} teaster", 'content' => "Article {$id} content", )); }   
//输出xml结尾部分
$doc->endOutput();

//最后是如何在sphinx里调用该 xml文件,

//大家上面看我保存的路径是/var/log/…(我这是Linux系统)

 

source xml_blog_posts
{
    type = xmlpipe
    xmlpipe_command = cat /var/log/你保存的文件名.xml
}

其他配置这里就不赘述了,相信大家也知道了。谢谢


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值