SearchModel::search PHP Method

    public function search($query, $limit = 20)
    {
        // 搜索值为空,返回结果
        if (empty($query)) {
            return false;
        }
        $query .= ' limit ' . $this->getLimit($limit);
        // limit处理
        //return $query;exit;
        $datas = D()->query($query);
        // 执行SphinxQL查询
        if (!$datas) {
            return false;
        }
        // 获取关键词信息
        $metas = $this->sdb->query('SHOW META');
        if (!$metas) {
            return false;
        }
        // 处理数据
        foreach ($metas as $v) {
            if ($v['Variable_name'] == 'total_found') {
                $data['count'] = $v['Value'];
            }
            if ($v['Variable_name'] == 'time') {
                $data['time'] = $v['Value'];
            }
            if (is_numeric($k = str_replace(array('keyword', '[', ']'), '', $v['Variable_name']))) {
                $data['matchwords'][$k]['keyword'] = $v['Value'];
                $data['keywords'][] = $v['Value'];
            }
            if (is_numeric($k = str_replace(array('docs', '[', ']'), '', $v['Variable_name']))) {
                $data['matchwords'][$k]['docs'] = $v['Value'];
            }
            if (is_numeric($k = str_replace(array('hits', '[', ']'), '', $v['Variable_name']))) {
                $data['matchwords'][$k]['hits'] = $v['Value'];
            }
        }
        $p = new Page($data['count'], $limit);
        $data['totalPages'] = $p->totalPages;
        $data['html'] = $p->show();
        $data['data'] = $datas;
        return $data;
    }

Usage Example

Example #1
0
 /**
  * Default search functionality.
  *
  * @since 2.0.0
  * @access public
  * @param int $Page Page number.
  */
 public function index($Page = '')
 {
     $this->addJsFile('search.js');
     $this->title(t('Search'));
     saveToConfig('Garden.Format.EmbedSize', '160x90', false);
     Gdn_Theme::section('SearchResults');
     list($Offset, $Limit) = offsetLimit($Page, c('Garden.Search.PerPage', 20));
     $this->setData('_Limit', $Limit);
     $Search = $this->Form->getFormValue('Search');
     $Mode = $this->Form->getFormValue('Mode');
     if ($Mode) {
         $this->SearchModel->ForceSearchMode = $Mode;
     }
     try {
         $ResultSet = $this->SearchModel->search($Search, $Offset, $Limit);
     } catch (Gdn_UserException $Ex) {
         $this->Form->addError($Ex);
         $ResultSet = array();
     } catch (Exception $Ex) {
         LogException($Ex);
         $this->Form->addError($Ex);
         $ResultSet = array();
     }
     Gdn::userModel()->joinUsers($ResultSet, array('UserID'));
     // Fix up the summaries.
     $SearchTerms = explode(' ', Gdn_Format::text($Search));
     foreach ($ResultSet as &$Row) {
         $Row['Summary'] = searchExcerpt(htmlspecialchars(Gdn_Format::plainText($Row['Summary'], $Row['Format'])), $SearchTerms);
         $Row['Summary'] = Emoji::instance()->translateToHtml($Row['Summary']);
         $Row['Format'] = 'Html';
     }
     $this->setData('SearchResults', $ResultSet, true);
     $this->setData('SearchTerm', Gdn_Format::text($Search), true);
     if ($ResultSet) {
         $NumResults = count($ResultSet);
     } else {
         $NumResults = 0;
     }
     if ($NumResults == $Offset + $Limit) {
         $NumResults++;
     }
     // Build a pager
     $PagerFactory = new Gdn_PagerFactory();
     $this->Pager = $PagerFactory->getPager('MorePager', $this);
     $this->Pager->MoreCode = 'More Results';
     $this->Pager->LessCode = 'Previous Results';
     $this->Pager->ClientID = 'Pager';
     $this->Pager->configure($Offset, $Limit, $NumResults, 'dashboard/search/%1$s/%2$s/?Search=' . Gdn_Format::url($Search));
     //		if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
     //         $this->setJson('LessRow', $this->Pager->toString('less'));
     //         $this->setJson('MoreRow', $this->Pager->toString('more'));
     //         $this->View = 'results';
     //      }
     $this->canonicalUrl(url('search', true));
     $this->render();
 }
All Usage Examples Of SearchModel::search