<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$questionid = 123;
$tagids = array(11,22,33);
foreach( $tagids as $sort=>$tagid){
$redis->zAdd('question_tag:'.$questionid,$sort,$tagid);
}
$val = $redis->zRange('question_tag:123',0,-1);
print_r($val);
print_r("\n");
[root@localhost redis]# php db2.php
Array
(
[0] => 11
[1] => 22
[2] => 33
)
$questionTitle = ‘搜索技术’;
$questionId = 123;
$words = fenci($questionTitle); // array(‘搜索’, ‘技术’);
foreach ($words as $word) {
$redis->zAdd(‘w:’ . md5($word), 1, $questionId);
}
// 索引完成
$keywords = ‘怎样实现搜索技术’;
$words = fenci($keywords); // array(‘怎样’, ‘实现’, ‘搜索’, ‘技术’);
$indexes = array_map(function ($word) {
return ‘w:’ . md5($word);
}, $words);
$redis->zUnion(‘result’, $indexes, array_fill(0, count($indexes), 1),
‘sum’);
print_r($redis->zRevRange(‘result’, 0, -1));