1*0Syaroslav@ivinco.com<?php 2*0Syaroslav@ivinco.com/* 3*0Syaroslav@ivinco.com * To change this template, choose Tools | Templates 4*0Syaroslav@ivinco.com * and open the template in the editor. 5*0Syaroslav@ivinco.com */ 6*0Syaroslav@ivinco.com 7*0Syaroslav@ivinco.comclass SphinxSearch 8*0Syaroslav@ivinco.com{ 9*0Syaroslav@ivinco.com private $_sphinx = null; 10*0Syaroslav@ivinco.com private $_result = array(); 11*0Syaroslav@ivinco.com private $_index = null; 12*0Syaroslav@ivinco.com public function __construct($host, $port, $index) 13*0Syaroslav@ivinco.com { 14*0Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 15*0Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 16*0Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 17*0Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => 10)); 18*0Syaroslav@ivinco.com 19*0Syaroslav@ivinco.com $this->_index = $index; 20*0Syaroslav@ivinco.com } 21*0Syaroslav@ivinco.com 22*0Syaroslav@ivinco.com public function search($query, $start, $resultsPerPage = 10) 23*0Syaroslav@ivinco.com { 24*0Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage); 25*0Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 26*0Syaroslav@ivinco.com $this->_result = $res; 27*0Syaroslav@ivinco.com if (empty($res['matches'])) { 28*0Syaroslav@ivinco.com return false; 29*0Syaroslav@ivinco.com } 30*0Syaroslav@ivinco.com 31*0Syaroslav@ivinco.com $pageMapper = new PageMapper(); 32*0Syaroslav@ivinco.com 33*0Syaroslav@ivinco.com $pageCrcList = array_keys($res['matches']); 34*0Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc($pageCrcList); 35*0Syaroslav@ivinco.com 36*0Syaroslav@ivinco.com $pagesList = array(); 37*0Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 38*0Syaroslav@ivinco.com $pagesList[] = $pagesIds[$crc]; 39*0Syaroslav@ivinco.com $data[$pagesIds[$crc]] = p_wiki_xhtml($pagesIds[$crc]); 40*0Syaroslav@ivinco.com } 41*0Syaroslav@ivinco.com 42*0Syaroslav@ivinco.com $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query); 43*0Syaroslav@ivinco.com $i = 0; 44*0Syaroslav@ivinco.com foreach($data as $key => $v){ 45*0Syaroslav@ivinco.com $data[$key] = $pagesExcerpt[$i++]; 46*0Syaroslav@ivinco.com } 47*0Syaroslav@ivinco.com return $data; 48*0Syaroslav@ivinco.com } 49*0Syaroslav@ivinco.com 50*0Syaroslav@ivinco.com public function getTotalFound() 51*0Syaroslav@ivinco.com { 52*0Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 53*0Syaroslav@ivinco.com } 54*0Syaroslav@ivinco.com} 55