利用phpanalysis中文分词做全文检索

  • Post author:
  • Post category:php


下载phpanalysis2.0  安装在你项目的第三方插件库中

放在插件目录



放在插件目录
第一步配置分词索引库:
public function participle($str){
  ini_set('display_errors', 'On');
  ini_set('memory_limit', '64M');
  error_reporting(E_ALL);
  require_once CMF_ROOT.'simplewind/extend/phpanalysis2.0/phpanalysis.class.php';
  $loadtime = $endtime1  = $endtime2 = $slen = 0;
  $do_fork = $do_unit = true;
  $do_multi = $do_prop = $pri_dict = false;

  if($str != '')
  {
      //初始化类
      \PhpAnalysis::$loadInit = false;
      $pa = new \PhpAnalysis('utf-8', 'utf-8', $pri_dict);
      //载入词典
      $pa->LoadDict();

      //执行分词
      $pa->SetSource($str);
      $pa->differMax = $do_multi;
      $pa->unitWord = $do_unit;

      $pa->StartAnalysis( $do_fork );
      //分词结果
      $okresult = $pa->GetFinallyResult(' ', $do_prop);
      $okresult = explode(" ",$okresult);
      return $okresult;
  }
}

public function search($keyword)
{
$keywords = $this->participle($keyword);


$whereor = [];
if (!empty($keyword)) {
    $whereor = "p.post_title like '%$keyword%'" ;
    $whereor .=" or m.jijin like  '%$keyword%'";
    $whereor .=" or m.author like  '%$keyword%'";
    $whereor .=" or m.keywords like  '%$keyword%'";
    $whereor .=" or m.post_excerpt like  '%$keyword%'";
}

$whereKeyword[] = ['like','%'.$keyword.'%'];

if($keywords){
  foreach ($keywords as $key => $value) {
    if($value){
      $whereKeyword[]= ['like','%'.$value.'%'];
      $whereor .= "or p.post_title like '%$value%'" ;
      // $whereor .=" or m.jijin like  '%$value%'";
      // $whereor .=" or m.author like  '%$value%'";
      // $whereor .=" or m.keywords like  '%$value%'";
      // $whereor .=" or m.post_excerpt like  '%$value%'";
    }
  }
  $whereKeyword[]= 'or';
}else{
  $whereKeyword = ['like', '%'.$keyword.'%'];
}


$where = [
    'm.delete_time' => 0
];
$where['m.title'] = $whereKeyword;
//$where['m.title'] = ['like', "%$keyword%"];
$join = [
    ['cmf_portal_post p','m.post_id = p.id'],
    ['cmf_portal_category_post pc','pc.post_id = p.id']
];
$order = [];

$field = 'm.*,p.post_title,p.id AS magazine_id';
if (!empty($cat_id)) {
    $where['c.id'] = ['eq',  $cat_id];
    array_push($join, [
        'cmf_portal_category c', 'c.id = pc.category_id'
    ]);
    $field = 'm.*,p.post_title,pc.category_id,c.name AS category_name,p.id AS magazine_id';
}



if (!empty($year)) {
    $where['m.year'] = $year;
}
$startTime = empty($filter['start_time']) ? 0 : $filter['start_time'];
$endTime   = empty($filter['end_time']) ? 0 : $filter['end_time'];
if (!empty($startTime) && !empty($endTime)) {
    $where['m.year'] =[['>=',$startTime],['<=',$endTime]];
} else {
    if (!empty($startTime)) {
        $where['m.year'] = ['>=',$startTime];
    }
    if (!empty($endTime)) {
        $where['m.year'] = ['<=',$endTime];
    }
}


if (!empty($month)) {
    $where['p.month'] =$month;
}
if ($order_time == 1){
    $order['year']="ASC";
}
if (!empty($order_diqu)){
    $order['published_time']="ASC";
}
if (!empty($order_hist)){
    $order['hist']="ASC";
}
$lists = Db::name('magazine')
    ->group('m.id')
    ->field($field)
    ->alias('m')
    ->join($join)
    ->where($where)
    ->whereOr($whereor)
    ->order($order)
    ->paginate(10,false,[
        'query' =>[
          'type' => $type,
          'keyword' => $keyword
        ]
    ]);
$years = $this->get_magazine_years();
$this->assign('startTime',$startTime);
$this->assign('endTime',$endTime);
$this->assign('years',$years);
$this->assign("type",$type);
$this -> assign("keyword", $keyword);
$this->assign('order_time',$order_time);
$this->assign('order_diqu',$order_diqu);
$this->assign('order_hist',$order_hist);
$this->assign('lists',$lists);
$this->assign('page',$lists->render());
return $this->fetch('/search');
}

好了:出来的效果如下图:



好了:出来的效果如下图:



版权声明:本文为qq_38381772原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。